Skip to content

Commit 0a55896

Browse files
mdaigleWraith2Wraith2
authored
Fix TryReadPlpBytes throws ArgumentException (#3470) (#3474)
* 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: Wraith <wraith2@gmail.com> Co-authored-by: Wraith2 <Wraith2@gmaill.com>
1 parent 6773a65 commit 0a55896

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
@@ -13362,6 +13362,13 @@ bool writeDataSizeToSnapshot
1336213362
break;
1336313363
}
1336413364
}
13365+
13366+
if (writeDataSizeToSnapshot)
13367+
{
13368+
stateObj.SetSnapshotStorage(null);
13369+
stateObj.ClearSnapshotDataSize();
13370+
}
13371+
1336513372
return TdsOperationStatus.Done;
1336613373

1336713374
static int IncrementSnapshotDataSize(TdsParserStateObject stateObj, bool resetting, int previousPacketId, int value)
@@ -13381,15 +13388,15 @@ static int IncrementSnapshotDataSize(TdsParserStateObject stateObj, bool resetti
1338113388
current = 0;
1338213389
}
1338313390

13384-
stateObj.SetSnapshotDataSize(current + value);
13391+
stateObj.AddSnapshotDataSize(current + value);
1338513392

1338613393
// return new packetid so next time we see this packet we know it isn't new
1338713394
return currentPacketId;
1338813395
}
1338913396
else
1339013397
{
1339113398
current = stateObj.GetSnapshotDataSize();
13392-
stateObj.SetSnapshotDataSize(current + value);
13399+
stateObj.AddSnapshotDataSize(current + value);
1339313400
return previousPacketId;
1339413401
}
1339513402
}

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
@@ -13544,6 +13544,13 @@ bool writeDataSizeToSnapshot
1354413544
break;
1354513545
}
1354613546
}
13547+
13548+
if (writeDataSizeToSnapshot)
13549+
{
13550+
stateObj.SetSnapshotStorage(null);
13551+
stateObj.ClearSnapshotDataSize();
13552+
}
13553+
1354713554
return TdsOperationStatus.Done;
1354813555

1354913556
static int IncrementSnapshotDataSize(TdsParserStateObject stateObj, bool resetting, int previousPacketId, int value)
@@ -13563,15 +13570,15 @@ static int IncrementSnapshotDataSize(TdsParserStateObject stateObj, bool resetti
1356313570
current = 0;
1356413571
}
1356513572

13566-
stateObj.SetSnapshotDataSize(current + value);
13573+
stateObj.AddSnapshotDataSize(current + value);
1356713574

1356813575
// return new packetid so next time we see this packet we know it isn't new
1356913576
return currentPacketId;
1357013577
}
1357113578
else
1357213579
{
1357313580
current = stateObj.GetSnapshotDataSize();
13574-
stateObj.SetSnapshotDataSize(current + value);
13581+
stateObj.AddSnapshotDataSize(current + value);
1357513582
return previousPacketId;
1357613583
}
1357713584
}

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

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

14891489
if (writeDataSizeToSnapshot)
14901490
{
1491-
SetSnapshotDataSize(bytesToRead);
1491+
AddSnapshotDataSize(bytesToRead);
14921492
}
14931493

14941494
AssertValidState();
@@ -2034,7 +2034,7 @@ internal TdsOperationStatus TryReadPlpLength(bool returnPlpNullIfNull, out ulong
20342034
// bool firstchunk = false;
20352035
bool isNull = false;
20362036

2037-
Debug.Assert(_longlenleft == 0, "Out of synch length read request");
2037+
Debug.Assert(_longlenleft == 0, "Out of sync length read request");
20382038
if (_longlen == 0)
20392039
{
20402040
// First chunk is being read. Find out what type of chunk it is
@@ -2115,6 +2115,7 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
21152115
}
21162116
return TryReadPlpBytes(ref buff, offset, len, out totalBytesRead, canContinue, canContinue, compatibilityMode);
21172117
}
2118+
21182119
// Reads the requested number of bytes from a plp data stream, or the entire data if
21192120
// requested length is -1 or larger than the actual length of data. First call to this method
21202121
// should be preceeded by a call to ReadPlpLength or ReadDataLength.
@@ -2230,14 +2231,14 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
22302231
SetSnapshotStorage(buff);
22312232
if (writeDataSizeToSnapshot)
22322233
{
2233-
SetSnapshotDataSize(bytesRead);
2234+
AddSnapshotDataSize(bytesRead);
22342235
}
22352236
}
22362237
return result;
22372238
}
22382239
if (writeDataSizeToSnapshot && canContinue)
22392240
{
2240-
SetSnapshotDataSize(bytesRead);
2241+
AddSnapshotDataSize(bytesRead);
22412242
}
22422243

22432244
if (_longlenleft == 0)
@@ -2255,10 +2256,7 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
22552256
else if (canContinue && result == TdsOperationStatus.NeedMoreData)
22562257
{
22572258
SetSnapshotStorage(buff);
2258-
if (writeDataSizeToSnapshot)
2259-
{
2260-
SetSnapshotDataSize(bytesRead);
2261-
}
2259+
// data bytes read from the current packet must be 0 here so do not save the snapshot data size
22622260
}
22632261
return result;
22642262
}
@@ -2272,6 +2270,12 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
22722270
break;
22732271
}
22742272
}
2273+
2274+
if (canContinue)
2275+
{
2276+
SetSnapshotStorage(null);
2277+
ClearSnapshotDataSize();
2278+
}
22752279
return TdsOperationStatus.Done;
22762280
}
22772281

@@ -3507,9 +3511,6 @@ internal void ResetSnapshot()
35073511
{
35083512
StateSnapshot snapshot = _snapshot;
35093513
_snapshot = null;
3510-
// TODO(GH-3385): Not sure what this is trying to assert, but it
3511-
// currently fails the DataReader tests.
3512-
// Debug.Assert(snapshot._storage == null);
35133514
snapshot.Clear();
35143515
Interlocked.CompareExchange(ref _cachedSnapshot, snapshot, null);
35153516
}
@@ -3567,9 +3568,6 @@ internal object TryTakeSnapshotStorage()
35673568
internal void SetSnapshotStorage(object buffer)
35683569
{
35693570
Debug.Assert(_snapshot != null, "should not access snapshot accessor functions without first checking that the snapshot is available");
3570-
// TODO(GH-3385): Not sure what this is trying to assert, but it
3571-
// currently fails the DataReader tests.
3572-
// Debug.Assert(_snapshot._storage == null, "should not overwrite snapshot stored buffer");
35733571
if (_snapshot != null)
35743572
{
35753573
_snapshot._storage = buffer;
@@ -3581,12 +3579,18 @@ internal void SetSnapshotStorage(object buffer)
35813579
/// countOfBytesCopiedFromCurrentPacket to be calculated
35823580
/// </summary>
35833581
/// <param name="countOfBytesCopiedFromCurrentPacket"></param>
3584-
internal void SetSnapshotDataSize(int countOfBytesCopiedFromCurrentPacket)
3582+
internal void AddSnapshotDataSize(int countOfBytesCopiedFromCurrentPacket)
35853583
{
35863584
Debug.Assert(_snapshot != null && _snapshot.ContinueEnabled, "_snapshot must exist to store packet data size");
35873585
_snapshot.SetPacketDataSize(countOfBytesCopiedFromCurrentPacket);
35883586
}
35893587

3588+
internal void ClearSnapshotDataSize()
3589+
{
3590+
Debug.Assert(_snapshot != null, "_snapshot must exist to store packet data size");
3591+
_snapshot?.ClearPacketDataSize();
3592+
}
3593+
35903594
internal int GetSnapshotTotalSize()
35913595
{
35923596
Debug.Assert(_snapshot != null && _snapshot.ContinueEnabled, "_snapshot must exist to read total size");
@@ -4283,6 +4287,16 @@ internal void SetPacketDataSize(int size)
42834287
target.RunningDataSize = total + size;
42844288
}
42854289

4290+
internal void ClearPacketDataSize()
4291+
{
4292+
PacketData current = _firstPacket;
4293+
while (current != null)
4294+
{
4295+
current.RunningDataSize = 0;
4296+
current = current.NextPacket;
4297+
}
4298+
}
4299+
42864300
internal int GetPacketDataOffset()
42874301
{
42884302
int offset = 0;
@@ -4332,9 +4346,6 @@ private void ClearPackets()
43324346

43334347
private void ClearState()
43344348
{
4335-
// TODO(GH-3385): Not sure what this is trying to assert, but it
4336-
// currently fails the DataReader tests.
4337-
// Debug.Assert(_storage == null);
43384349
_storage = null;
43394350
_replayStateData.Clear(_stateObj);
43404351
_continueStateData?.Clear(_stateObj, trackStack: false);

0 commit comments

Comments
 (0)