Skip to content

Commit 951da45

Browse files
Wraith2Wraith2
andauthored
Fix TryReadPlpBytes throws ArgumentException (#3470)
* add clearing of data sizes when a multi-part result is returned * remove asserts detecting overwrite of snapshot storage which are now no longer accurate * remove empty statement block --------- Co-authored-by: Wraith2 <Wraith2@gmaill.com>
1 parent a93ff40 commit 951da45

File tree

3 files changed

+47
-22
lines changed

3 files changed

+47
-22
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13405,6 +13405,13 @@ bool writeDataSizeToSnapshot
1340513405
break;
1340613406
}
1340713407
}
13408+
13409+
if (writeDataSizeToSnapshot)
13410+
{
13411+
stateObj.SetSnapshotStorage(null);
13412+
stateObj.ClearSnapshotDataSize();
13413+
}
13414+
1340813415
return TdsOperationStatus.Done;
1340913416

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

13427-
stateObj.SetSnapshotDataSize(current + value);
13434+
stateObj.AddSnapshotDataSize(current + value);
1342813435

1342913436
// return new packetid so next time we see this packet we know it isn't new
1343013437
return currentPacketId;
1343113438
}
1343213439
else
1343313440
{
1343413441
current = stateObj.GetSnapshotDataSize();
13435-
stateObj.SetSnapshotDataSize(current + value);
13442+
stateObj.AddSnapshotDataSize(current + value);
1343613443
return previousPacketId;
1343713444
}
1343813445
}

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13596,6 +13596,13 @@ bool writeDataSizeToSnapshot
1359613596
break;
1359713597
}
1359813598
}
13599+
13600+
if (writeDataSizeToSnapshot)
13601+
{
13602+
stateObj.SetSnapshotStorage(null);
13603+
stateObj.ClearSnapshotDataSize();
13604+
}
13605+
1359913606
return TdsOperationStatus.Done;
1360013607

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

13618-
stateObj.SetSnapshotDataSize(current + value);
13625+
stateObj.AddSnapshotDataSize(current + value);
1361913626

1362013627
// return new packetid so next time we see this packet we know it isn't new
1362113628
return currentPacketId;
1362213629
}
1362313630
else
1362413631
{
1362513632
current = stateObj.GetSnapshotDataSize();
13626-
stateObj.SetSnapshotDataSize(current + value);
13633+
stateObj.AddSnapshotDataSize(current + value);
1362713634
return previousPacketId;
1362813635
}
1362913636
}

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/TdsParserStateObject.cs

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1512,7 +1512,7 @@ public TdsOperationStatus TryReadByteArray(Span<byte> buff, int len, out int tot
15121512

15131513
if (writeDataSizeToSnapshot)
15141514
{
1515-
SetSnapshotDataSize(bytesToRead);
1515+
AddSnapshotDataSize(bytesToRead);
15161516
}
15171517

15181518
AssertValidState();
@@ -2058,7 +2058,7 @@ internal TdsOperationStatus TryReadPlpLength(bool returnPlpNullIfNull, out ulong
20582058
// bool firstchunk = false;
20592059
bool isNull = false;
20602060

2061-
Debug.Assert(_longlenleft == 0, "Out of synch length read request");
2061+
Debug.Assert(_longlenleft == 0, "Out of sync length read request");
20622062
if (_longlen == 0)
20632063
{
20642064
// First chunk is being read. Find out what type of chunk it is
@@ -2139,6 +2139,7 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
21392139
}
21402140
return TryReadPlpBytes(ref buff, offset, len, out totalBytesRead, canContinue, canContinue, compatibilityMode);
21412141
}
2142+
21422143
// Reads the requested number of bytes from a plp data stream, or the entire data if
21432144
// requested length is -1 or larger than the actual length of data. First call to this method
21442145
// should be preceeded by a call to ReadPlpLength or ReadDataLength.
@@ -2254,14 +2255,14 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
22542255
SetSnapshotStorage(buff);
22552256
if (writeDataSizeToSnapshot)
22562257
{
2257-
SetSnapshotDataSize(bytesRead);
2258+
AddSnapshotDataSize(bytesRead);
22582259
}
22592260
}
22602261
return result;
22612262
}
22622263
if (writeDataSizeToSnapshot && canContinue)
22632264
{
2264-
SetSnapshotDataSize(bytesRead);
2265+
AddSnapshotDataSize(bytesRead);
22652266
}
22662267

22672268
if (_longlenleft == 0)
@@ -2279,10 +2280,7 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
22792280
else if (canContinue && result == TdsOperationStatus.NeedMoreData)
22802281
{
22812282
SetSnapshotStorage(buff);
2282-
if (writeDataSizeToSnapshot)
2283-
{
2284-
SetSnapshotDataSize(bytesRead);
2285-
}
2283+
// data bytes read from the current packet must be 0 here so do not save the snapshot data size
22862284
}
22872285
return result;
22882286
}
@@ -2296,6 +2294,12 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
22962294
break;
22972295
}
22982296
}
2297+
2298+
if (canContinue)
2299+
{
2300+
SetSnapshotStorage(null);
2301+
ClearSnapshotDataSize();
2302+
}
22992303
return TdsOperationStatus.Done;
23002304
}
23012305

@@ -3531,9 +3535,6 @@ internal void ResetSnapshot()
35313535
{
35323536
StateSnapshot snapshot = _snapshot;
35333537
_snapshot = null;
3534-
// TODO(GH-3385): Not sure what this is trying to assert, but it
3535-
// currently fails the DataReader tests.
3536-
// Debug.Assert(snapshot._storage == null);
35373538
snapshot.Clear();
35383539
Interlocked.CompareExchange(ref _cachedSnapshot, snapshot, null);
35393540
}
@@ -3591,9 +3592,6 @@ internal object TryTakeSnapshotStorage()
35913592
internal void SetSnapshotStorage(object buffer)
35923593
{
35933594
Debug.Assert(_snapshot != null, "should not access snapshot accessor functions without first checking that the snapshot is available");
3594-
// TODO(GH-3385): Not sure what this is trying to assert, but it
3595-
// currently fails the DataReader tests.
3596-
// Debug.Assert(_snapshot._storage == null, "should not overwrite snapshot stored buffer");
35973595
if (_snapshot != null)
35983596
{
35993597
_snapshot._storage = buffer;
@@ -3605,12 +3603,18 @@ internal void SetSnapshotStorage(object buffer)
36053603
/// countOfBytesCopiedFromCurrentPacket to be calculated
36063604
/// </summary>
36073605
/// <param name="countOfBytesCopiedFromCurrentPacket"></param>
3608-
internal void SetSnapshotDataSize(int countOfBytesCopiedFromCurrentPacket)
3606+
internal void AddSnapshotDataSize(int countOfBytesCopiedFromCurrentPacket)
36093607
{
36103608
Debug.Assert(_snapshot != null && _snapshot.ContinueEnabled, "_snapshot must exist to store packet data size");
36113609
_snapshot.SetPacketDataSize(countOfBytesCopiedFromCurrentPacket);
36123610
}
36133611

3612+
internal void ClearSnapshotDataSize()
3613+
{
3614+
Debug.Assert(_snapshot != null, "_snapshot must exist to store packet data size");
3615+
_snapshot?.ClearPacketDataSize();
3616+
}
3617+
36143618
internal int GetSnapshotTotalSize()
36153619
{
36163620
Debug.Assert(_snapshot != null && _snapshot.ContinueEnabled, "_snapshot must exist to read total size");
@@ -4307,6 +4311,16 @@ internal void SetPacketDataSize(int size)
43074311
target.RunningDataSize = total + size;
43084312
}
43094313

4314+
internal void ClearPacketDataSize()
4315+
{
4316+
PacketData current = _firstPacket;
4317+
while (current != null)
4318+
{
4319+
current.RunningDataSize = 0;
4320+
current = current.NextPacket;
4321+
}
4322+
}
4323+
43104324
internal int GetPacketDataOffset()
43114325
{
43124326
int offset = 0;
@@ -4356,9 +4370,6 @@ private void ClearPackets()
43564370

43574371
private void ClearState()
43584372
{
4359-
// TODO(GH-3385): Not sure what this is trying to assert, but it
4360-
// currently fails the DataReader tests.
4361-
// Debug.Assert(_storage == null);
43624373
_storage = null;
43634374
_replayStateData.Clear(_stateObj);
43644375
_continueStateData?.Clear(_stateObj, trackStack: false);

0 commit comments

Comments
 (0)