Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
Fix bad usage of ArrayPool in TdsParserStateObject (#37270)
Browse files Browse the repository at this point in the history
If TdsParserStateObject.TryReadString or
TdsParserStateObject.TryReadStringWithEncoding hit the growth case they
would rent an array, save it into a field, use the array, return the array to the pool,
but keep it assigned to the field and continue using it.

Since other writes to _bTmp use fresh arrays in an instance-cached-growth
pattern, this change restores these two methods to that same approach, rather
than renting to a local buffer and not renting into a field.
  • Loading branch information
bartonjs authored and stephentoub committed Apr 30, 2019
1 parent 4225656 commit e89d17a
Showing 1 changed file with 2 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using System.Text;
using System.Security;
using System.Runtime.InteropServices;
using System.Buffers;

namespace System.Data.SqlClient
{
Expand Down Expand Up @@ -1644,14 +1643,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 @@ -1677,10 +1674,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 @@ -1713,7 +1706,6 @@ internal bool TryReadStringWithEncoding(int length, System.Text.Encoding encodin
}
byte[] buf = null;
int offset = 0;
bool rentedBuffer = false;

if (isPlp)
{
Expand All @@ -1731,8 +1723,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 @@ -1760,10 +1751,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 e89d17a

Please sign in to comment.