Skip to content

Commit 2881ce4

Browse files
committed
more try/finally
1 parent 657b1b2 commit 2881ce4

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

src/Microsoft.Data.SqlClient/src/Interop/Windows/Sni/SniNativeWrapper.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,12 +395,12 @@ internal static unsafe uint SNIOpenSyncEx(
395395
{
396396
var writer = SqlObjectPools.BufferWriter.Rent();
397397

398-
// Native SNI requires the Unicode encoding and any other encoding like UTF8 breaks the code.
399-
Encoding.Unicode.GetBytes(spn, writer);
400-
Trace.Assert(writer.WrittenCount <= SniMaxComposedSpnLength, "Length of the provided SPN exceeded the buffer size.");
401-
402398
try
403399
{
400+
// Native SNI requires the Unicode encoding and any other encoding like UTF8 breaks the code.
401+
Encoding.Unicode.GetBytes(spn, writer);
402+
Trace.Assert(writer.WrittenCount <= SniMaxComposedSpnLength, "Length of the provided SPN exceeded the buffer size.");
403+
404404
fixed (byte* pin_spnBuffer = writer.WrittenSpan)
405405
{
406406
clientConsumerInfo.szSPN = pin_spnBuffer;

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,39 @@ internal void ProcessSSPI(int receivedLength)
1414

1515
SniContext outerContext = _physicalStateObj.SniContext;
1616
_physicalStateObj.SniContext = SniContext.Snix_ProcessSspi;
17+
1718
// allocate received buffer based on length from SSPI message
1819
byte[] receivedBuff = ArrayPool<byte>.Shared.Rent(receivedLength);
1920

20-
// read SSPI data received from server
21-
Debug.Assert(_physicalStateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
22-
TdsOperationStatus result = _physicalStateObj.TryReadByteArray(receivedBuff, receivedLength);
23-
if (result != TdsOperationStatus.Done)
21+
try
2422
{
25-
throw SQL.SynchronousCallMayNotPend();
26-
}
23+
// read SSPI data received from server
24+
Debug.Assert(_physicalStateObj._syncOverAsync, "Should not attempt pends in a synchronous call");
25+
TdsOperationStatus result = _physicalStateObj.TryReadByteArray(receivedBuff, receivedLength);
26+
if (result != TdsOperationStatus.Done)
27+
{
28+
throw SQL.SynchronousCallMayNotPend();
29+
}
2730

28-
// allocate send buffer and initialize length
29-
var writer = SqlObjectPools.BufferWriter.Rent();
31+
// allocate send buffer and initialize length
32+
var writer = SqlObjectPools.BufferWriter.Rent();
3033

31-
try
32-
{
33-
// make call for SSPI data
34-
_authenticationProvider!.SSPIData(receivedBuff.AsSpan(0, receivedLength), writer, _serverSpn);
34+
try
35+
{
36+
// make call for SSPI data
37+
_authenticationProvider!.SSPIData(receivedBuff.AsSpan(0, receivedLength), writer, _serverSpn);
3538

36-
// DO NOT SEND LENGTH - TDS DOC INCORRECT! JUST SEND SSPI DATA!
37-
_physicalStateObj.WriteByteSpan(writer.WrittenSpan);
39+
// DO NOT SEND LENGTH - TDS DOC INCORRECT! JUST SEND SSPI DATA!
40+
_physicalStateObj.WriteByteSpan(writer.WrittenSpan);
3841

42+
}
43+
finally
44+
{
45+
SqlObjectPools.BufferWriter.Return(writer);
46+
}
3947
}
4048
finally
4149
{
42-
SqlObjectPools.BufferWriter.Return(writer);
4350
ArrayPool<byte>.Shared.Return(receivedBuff, clearArray: true);
4451
}
4552

0 commit comments

Comments
 (0)