Skip to content

Commit 7db3c50

Browse files
committed
clarify compatability paths through TryReadPlpBytes
use as instead of casts for safety
1 parent 0fbae86 commit 7db3c50

File tree

4 files changed

+33
-22
lines changed

4 files changed

+33
-22
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6493,7 +6493,7 @@ private TdsOperationStatus TryReadByteArrayWithContinue(TdsParserStateObject sta
64936493
{
64946494
if (isContinuing || isStarting)
64956495
{
6496-
temp = (byte[])stateObj.TryTakeSnapshotStorage();
6496+
temp = stateObj.TryTakeSnapshotStorage() as byte[];
64976497
Debug.Assert(bytes == null || bytes.Length == length, "stored buffer length must be null or must have been created with the correct length");
64986498
}
64996499
if (temp != null)
@@ -12915,7 +12915,7 @@ internal TdsOperationStatus TryReadPlpUnicodeCharsWithContinue(TdsParserStateObj
1291512915
{
1291612916
if (isContinuing || isStarting)
1291712917
{
12918-
temp = (char[])stateObj.TryTakeSnapshotStorage();
12918+
temp = stateObj.TryTakeSnapshotStorage() as char[];
1291912919
Debug.Assert(temp == null || length == int.MaxValue || temp.Length == length, "stored buffer length must be null or must have been created with the correct length");
1292012920
}
1292112921
if (temp != null)

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6970,7 +6970,7 @@ private TdsOperationStatus TryReadByteArrayWithContinue(TdsParserStateObject sta
69706970
{
69716971
if (isContinuing || isStarting)
69726972
{
6973-
temp = (byte[])stateObj.TryTakeSnapshotStorage();
6973+
temp = stateObj.TryTakeSnapshotStorage() as byte[];
69746974
Debug.Assert(bytes == null || bytes.Length == length, "stored buffer length must be null or must have been created with the correct length");
69756975
}
69766976
if (temp != null)
@@ -13458,7 +13458,7 @@ internal TdsOperationStatus TryReadPlpUnicodeCharsWithContinue(TdsParserStateObj
1345813458
{
1345913459
if (isContinuing || isStarting)
1346013460
{
13461-
temp = (char[])stateObj.TryTakeSnapshotStorage();
13461+
temp = stateObj.TryTakeSnapshotStorage() as char[];
1346213462
Debug.Assert(temp == null || length == int.MaxValue || temp.Length == length, "stored buffer length must be null or must have been created with the correct length");
1346313463
}
1346413464
if (temp != null)

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlCachedBuffer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ internal static TdsOperationStatus TryCreate(SqlMetaDataPriv metadata, TdsParser
8080
byte[] byteArr = new byte[cb];
8181
// pass false for the writeDataSizeToSnapshot parameter because we want to only take data
8282
// from the current packet and not try to do a continue-capable multi packet read
83-
result = stateObj.TryReadPlpBytes(ref byteArr, 0, cb, out cb, writeDataSizeToSnapshot: false);
83+
result = stateObj.TryReadPlpBytes(ref byteArr, 0, cb, out cb, writeDataSizeToSnapshot: false, compatibilityMode: false);
8484
if (result != TdsOperationStatus.Done)
8585
{
8686
if (result == TdsOperationStatus.NeedMoreData && isAvailable && cb == byteArr.Length)

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

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1706,7 +1706,7 @@ internal TdsOperationStatus TryReadStringWithEncoding(int length, System.Text.En
17061706
{
17071707
if (isContinuing || isStarting)
17081708
{
1709-
buf = (byte[])TryTakeSnapshotStorage();
1709+
buf = TryTakeSnapshotStorage() as byte[];
17101710
Debug.Assert(buf == null || buf.Length == length, "stored buffer length must be null or must have been created with the correct length");
17111711
}
17121712
if (buf != null)
@@ -1850,19 +1850,25 @@ internal int ReadPlpBytesChunk(byte[] buff, int offset, int len)
18501850

18511851
internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len, out int totalBytesRead)
18521852
{
1853-
( _, bool isStarting, bool isContinuing) = GetSnapshotStatuses();
1854-
return TryReadPlpBytes(ref buff, offset, len, out totalBytesRead, isStarting || isContinuing);
1853+
bool isStarting = false;
1854+
bool isContinuing = false;
1855+
bool compatibilityMode = LocalAppContextSwitches.UseCompatibilityAsyncBehaviour;
1856+
if (!compatibilityMode)
1857+
{
1858+
(_, isStarting, isContinuing) = GetSnapshotStatuses();
1859+
}
1860+
return TryReadPlpBytes(ref buff, offset, len, out totalBytesRead, isStarting || isContinuing, compatibilityMode);
18551861
}
18561862
// Reads the requested number of bytes from a plp data stream, or the entire data if
18571863
// requested length is -1 or larger than the actual length of data. First call to this method
18581864
// should be preceeded by a call to ReadPlpLength or ReadDataLength.
18591865
// Returns the actual bytes read.
18601866
// NOTE: This method must be retriable WITHOUT replaying a snapshot
18611867
// Every time you call this method increment the offset and decrease len by the value of totalBytesRead
1862-
internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len, out int totalBytesRead, bool writeDataSizeToSnapshot)
1868+
internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len, out int totalBytesRead, bool writeDataSizeToSnapshot, bool compatibilityMode)
18631869
{
18641870
totalBytesRead = 0;
1865-
1871+
18661872
if (_longlen == 0)
18671873
{
18681874
Debug.Assert(_longlenleft == 0);
@@ -1888,17 +1894,19 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
18881894
{
18891895
// if there is a snapshot and it contains a stored plp buffer take it
18901896
// and try to use it if it is the right length
1891-
buff = (byte[])TryTakeSnapshotStorage();
1897+
buff = TryTakeSnapshotStorage() as byte[];
18921898
if (buff != null)
18931899
{
18941900
offset = _snapshot.GetPacketDataOffset();
18951901
totalBytesRead = offset;
18961902
}
18971903
}
1898-
else if (_snapshot != null)
1904+
else if (compatibilityMode && _snapshot != null && _snapshotStatus != SnapshotStatus.NotActive)
18991905
{
19001906
// legacy replay path perf optimization
1901-
buff = (byte[])TryTakeSnapshotStorage();
1907+
// if there is a snapshot and it contains a stored plp buffer take it
1908+
// and try to use it if it is the right length
1909+
buff = TryTakeSnapshotStorage() as byte[];
19021910
}
19031911

19041912
if ((ulong)(buff?.Length ?? 0) != _longlen)
@@ -1957,11 +1965,13 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
19571965
// so it can be re-used when another packet arrives and we read again
19581966
SetSnapshotStorage(buff);
19591967
SetSnapshotDataSize(bytesRead);
1960-
1968+
19611969
}
1962-
else if (_snapshot != null)
1970+
else if (compatibilityMode && _snapshot != null)
19631971
{
19641972
// legacy replay path perf optimization
1973+
// a partial read has happened so store the target buffer in the snapshot
1974+
// so it can be re-used when another packet arrives and we read again
19651975
SetSnapshotStorage(buff);
19661976
}
19671977
return result;
@@ -1973,18 +1983,19 @@ internal TdsOperationStatus TryReadPlpBytes(ref byte[] buff, int offset, int len
19731983
result = TryReadPlpLength(false, out _);
19741984
if (result != TdsOperationStatus.Done)
19751985
{
1976-
if (result == TdsOperationStatus.NeedMoreData)
1986+
if (writeDataSizeToSnapshot)
19771987
{
1978-
if (writeDataSizeToSnapshot)
1988+
if (result == TdsOperationStatus.NeedMoreData)
19791989
{
19801990
SetSnapshotStorage(buff);
19811991
SetSnapshotDataSize(bytesRead);
19821992
}
1983-
else if (_snapshot != null)
1984-
{
1985-
// legacy replay path perf optimization
1986-
SetSnapshotStorage(buff);
1987-
}
1993+
}
1994+
else if (compatibilityMode && _snapshot != null)
1995+
{
1996+
// a partial read has happened so store the target buffer in the snapshot
1997+
// so it can be re-used when another packet arrives and we read again
1998+
SetSnapshotStorage(buff);
19881999
}
19892000
return result;
19902001
}

0 commit comments

Comments
 (0)