Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bad usage of ArrayPool in TdsParserStateObject #171

Merged
merged 1 commit into from
Aug 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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