Skip to content

Fix TryReadPlpBytes throws ArgumentException #3470

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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 @@ -13405,6 +13405,13 @@ bool writeDataSizeToSnapshot
break;
}
}

if (writeDataSizeToSnapshot)
{
stateObj.SetSnapshotStorage(null);
stateObj.ClearSnapshotDataSize();
}

return TdsOperationStatus.Done;

static int IncrementSnapshotDataSize(TdsParserStateObject stateObj, bool resetting, int previousPacketId, int value)
Expand All @@ -13424,15 +13431,15 @@ static int IncrementSnapshotDataSize(TdsParserStateObject stateObj, bool resetti
current = 0;
}

stateObj.SetSnapshotDataSize(current + value);
stateObj.AddSnapshotDataSize(current + value);

// return new packetid so next time we see this packet we know it isn't new
return currentPacketId;
}
else
{
current = stateObj.GetSnapshotDataSize();
stateObj.SetSnapshotDataSize(current + value);
stateObj.AddSnapshotDataSize(current + value);
return previousPacketId;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13596,6 +13596,13 @@ bool writeDataSizeToSnapshot
break;
}
}

if (writeDataSizeToSnapshot)
{
stateObj.SetSnapshotStorage(null);
stateObj.ClearSnapshotDataSize();
}

return TdsOperationStatus.Done;

static int IncrementSnapshotDataSize(TdsParserStateObject stateObj, bool resetting, int previousPacketId, int value)
Expand All @@ -13615,15 +13622,15 @@ static int IncrementSnapshotDataSize(TdsParserStateObject stateObj, bool resetti
current = 0;
}

stateObj.SetSnapshotDataSize(current + value);
stateObj.AddSnapshotDataSize(current + value);

// return new packetid so next time we see this packet we know it isn't new
return currentPacketId;
}
else
{
current = stateObj.GetSnapshotDataSize();
stateObj.SetSnapshotDataSize(current + value);
stateObj.AddSnapshotDataSize(current + value);
return previousPacketId;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1512,7 +1512,7 @@ public TdsOperationStatus TryReadByteArray(Span<byte> buff, int len, out int tot

if (writeDataSizeToSnapshot)
{
SetSnapshotDataSize(bytesToRead);
AddSnapshotDataSize(bytesToRead);
}

AssertValidState();
Expand Down Expand Up @@ -2058,7 +2058,7 @@ internal TdsOperationStatus TryReadPlpLength(bool returnPlpNullIfNull, out ulong
// bool firstchunk = false;
bool isNull = false;

Debug.Assert(_longlenleft == 0, "Out of synch length read request");
Debug.Assert(_longlenleft == 0, "Out of sync length read request");
if (_longlen == 0)
{
// First chunk is being read. Find out what type of chunk it is
Expand Down Expand Up @@ -2139,6 +2139,7 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
}
return TryReadPlpBytes(ref buff, offset, len, out totalBytesRead, canContinue, canContinue, compatibilityMode);
}

// Reads the requested number of bytes from a plp data stream, or the entire data if
// requested length is -1 or larger than the actual length of data. First call to this method
// should be preceeded by a call to ReadPlpLength or ReadDataLength.
Expand Down Expand Up @@ -2254,14 +2255,14 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
SetSnapshotStorage(buff);
if (writeDataSizeToSnapshot)
{
SetSnapshotDataSize(bytesRead);
AddSnapshotDataSize(bytesRead);
}
}
return result;
}
if (writeDataSizeToSnapshot && canContinue)
{
SetSnapshotDataSize(bytesRead);
AddSnapshotDataSize(bytesRead);
}

if (_longlenleft == 0)
Expand All @@ -2279,10 +2280,7 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
else if (canContinue && result == TdsOperationStatus.NeedMoreData)
{
SetSnapshotStorage(buff);
if (writeDataSizeToSnapshot)
{
SetSnapshotDataSize(bytesRead);
}
// data bytes read from the current packet must be 0 here so do not save the snapshot data size
}
return result;
}
Expand All @@ -2296,6 +2294,12 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
break;
}
}

if (canContinue)
{
SetSnapshotStorage(null);
ClearSnapshotDataSize();
}
return TdsOperationStatus.Done;
}

Expand Down Expand Up @@ -3531,9 +3535,6 @@ internal void ResetSnapshot()
{
StateSnapshot snapshot = _snapshot;
_snapshot = null;
// TODO(GH-3385): Not sure what this is trying to assert, but it
// currently fails the DataReader tests.
// Debug.Assert(snapshot._storage == null);
snapshot.Clear();
Interlocked.CompareExchange(ref _cachedSnapshot, snapshot, null);
}
Expand Down Expand Up @@ -3591,9 +3592,6 @@ internal object TryTakeSnapshotStorage()
internal void SetSnapshotStorage(object buffer)
{
Debug.Assert(_snapshot != null, "should not access snapshot accessor functions without first checking that the snapshot is available");
// TODO(GH-3385): Not sure what this is trying to assert, but it
// currently fails the DataReader tests.
// Debug.Assert(_snapshot._storage == null, "should not overwrite snapshot stored buffer");
if (_snapshot != null)
{
_snapshot._storage = buffer;
Expand All @@ -3605,12 +3603,18 @@ internal void SetSnapshotStorage(object buffer)
/// countOfBytesCopiedFromCurrentPacket to be calculated
/// </summary>
/// <param name="countOfBytesCopiedFromCurrentPacket"></param>
internal void SetSnapshotDataSize(int countOfBytesCopiedFromCurrentPacket)
internal void AddSnapshotDataSize(int countOfBytesCopiedFromCurrentPacket)
{
Debug.Assert(_snapshot != null && _snapshot.ContinueEnabled, "_snapshot must exist to store packet data size");
_snapshot.SetPacketDataSize(countOfBytesCopiedFromCurrentPacket);
}

internal void ClearSnapshotDataSize()
{
Debug.Assert(_snapshot != null, "_snapshot must exist to store packet data size");
_snapshot?.ClearPacketDataSize();
}

internal int GetSnapshotTotalSize()
{
Debug.Assert(_snapshot != null && _snapshot.ContinueEnabled, "_snapshot must exist to read total size");
Expand Down Expand Up @@ -4307,6 +4311,16 @@ internal void SetPacketDataSize(int size)
target.RunningDataSize = total + size;
}

internal void ClearPacketDataSize()
{
PacketData current = _firstPacket;
while (current != null)
{
current.RunningDataSize = 0;
current = current.NextPacket;
}
}

internal int GetPacketDataOffset()
{
int offset = 0;
Expand Down Expand Up @@ -4356,9 +4370,6 @@ private void ClearPackets()

private void ClearState()
{
// TODO(GH-3385): Not sure what this is trying to assert, but it
// currently fails the DataReader tests.
// Debug.Assert(_storage == null);
_storage = null;
_replayStateData.Clear(_stateObj);
_continueStateData?.Clear(_stateObj, trackStack: false);
Expand Down
Loading