Skip to content

Commit

Permalink
Fix bad usage of ArrayPool in TdsParserStateObject (dotnet#171)
Browse files Browse the repository at this point in the history
Apply dotnet/corefx#37270 to M.D.SqlClient. Fixes dotnet#145.
  • Loading branch information
rmja authored and David-Engel committed Aug 30, 2019
1 parent 2010fd4 commit bf78793
Showing 1 changed file with 2 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Buffers;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
Expand Down Expand Up @@ -1650,14 +1649,12 @@ internal bool TryReadString(int length, out string value)
int cBytes = length << 1;
byte[] buf;
int offset = 0;
bool rentedBuffer = false;

if (((_inBytesUsed + cBytes) > _inBytesRead) || (_inBytesPacket < cBytes))
{
if (_bTmp == null || _bTmp.Length < cBytes)
{
_bTmp = ArrayPool<byte>.Shared.Rent(cBytes);
rentedBuffer = true;
_bTmp = new byte[cBytes];
}

if (!TryReadByteArray(_bTmp, cBytes))
Expand All @@ -1683,10 +1680,6 @@ internal bool TryReadString(int length, out string value)
}

value = System.Text.Encoding.Unicode.GetString(buf, offset, cBytes);
if (rentedBuffer)
{
ArrayPool<byte>.Shared.Return(_bTmp, clearArray: true);
}
return true;
}

Expand Down Expand Up @@ -1719,7 +1712,6 @@ internal bool TryReadStringWithEncoding(int length, System.Text.Encoding encodin
}
byte[] buf = null;
int offset = 0;
bool rentedBuffer = false;

if (isPlp)
{
Expand All @@ -1737,8 +1729,7 @@ internal bool TryReadStringWithEncoding(int length, System.Text.Encoding encodin
{
if (_bTmp == null || _bTmp.Length < length)
{
_bTmp = ArrayPool<byte>.Shared.Rent(length);
rentedBuffer = true;
_bTmp = new byte[length];
}

if (!TryReadByteArray(_bTmp, length))
Expand Down Expand Up @@ -1766,10 +1757,6 @@ internal bool TryReadStringWithEncoding(int length, System.Text.Encoding encodin

// BCL optimizes to not use char[] underneath
value = encoding.GetString(buf, offset, length);
if (rentedBuffer)
{
ArrayPool<byte>.Shared.Return(_bTmp, clearArray: true);
}
return true;
}

Expand Down

0 comments on commit bf78793

Please sign in to comment.