Skip to content

Commit c56f20e

Browse files
committed
try/finally
1 parent 66e55f7 commit c56f20e

File tree

1 file changed

+60
-49
lines changed
  • src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient

1 file changed

+60
-49
lines changed

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

Lines changed: 60 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,20 @@ internal void ProcessSSPI(int receivedLength)
2828
// allocate send buffer and initialize length
2929
var writer = SqlObjectPools.BufferWriter.Rent();
3030

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

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

37-
SqlObjectPools.BufferWriter.Return(writer);
38-
ArrayPool<byte>.Shared.Return(receivedBuff, clearArray: true);
39+
}
40+
finally
41+
{
42+
SqlObjectPools.BufferWriter.Return(writer);
43+
ArrayPool<byte>.Shared.Return(receivedBuff, clearArray: true);
44+
}
3945

4046
// set message type so server knows its a SSPI response
4147
_physicalStateObj._outputMessageType = TdsEnums.MT_SSPI;
@@ -139,60 +145,65 @@ internal void TdsLogin(
139145
// allocate memory for SSPI variables
140146
ArrayBufferWriter<byte> sspiWriter = null;
141147

142-
// only add lengths of password and username if not using SSPI or requesting federated authentication info
143-
if (!rec.useSSPI && !(_connHandler._federatedAuthenticationInfoRequested || _connHandler._federatedAuthenticationRequested))
148+
try
144149
{
145-
checked
150+
// only add lengths of password and username if not using SSPI or requesting federated authentication info
151+
if (!rec.useSSPI && !(_connHandler._federatedAuthenticationInfoRequested || _connHandler._federatedAuthenticationRequested))
146152
{
147-
length += (userName.Length * 2) + encryptedPasswordLengthInBytes
148-
+ encryptedChangePasswordLengthInBytes;
153+
checked
154+
{
155+
length += (userName.Length * 2) + encryptedPasswordLengthInBytes
156+
+ encryptedChangePasswordLengthInBytes;
157+
}
149158
}
150-
}
151-
else
152-
{
153-
if (rec.useSSPI)
159+
else
154160
{
155-
sspiWriter = SqlObjectPools.BufferWriter.Rent();
161+
if (rec.useSSPI)
162+
{
163+
sspiWriter = SqlObjectPools.BufferWriter.Rent();
156164

157-
// Call helper function for SSPI data and actual length.
158-
// Since we don't have SSPI data from the server, send null for the
159-
// byte[] buffer and 0 for the int length.
160-
Debug.Assert(SniContext.Snix_Login == _physicalStateObj.SniContext, $"Unexpected SniContext. Expecting Snix_Login, actual value is '{_physicalStateObj.SniContext}'");
161-
_physicalStateObj.SniContext = SniContext.Snix_LoginSspi;
162-
_authenticationProvider.SSPIData(ReadOnlySpan<byte>.Empty, sspiWriter, _serverSpn);
165+
// Call helper function for SSPI data and actual length.
166+
// Since we don't have SSPI data from the server, send null for the
167+
// byte[] buffer and 0 for the int length.
168+
Debug.Assert(SniContext.Snix_Login == _physicalStateObj.SniContext, $"Unexpected SniContext. Expecting Snix_Login, actual value is '{_physicalStateObj.SniContext}'");
169+
_physicalStateObj.SniContext = SniContext.Snix_LoginSspi;
170+
_authenticationProvider.SSPIData(ReadOnlySpan<byte>.Empty, sspiWriter, _serverSpn);
163171

164-
_physicalStateObj.SniContext = SniContext.Snix_Login;
172+
_physicalStateObj.SniContext = SniContext.Snix_Login;
165173

166-
checked
167-
{
168-
length += (int)sspiWriter.WrittenCount;
174+
checked
175+
{
176+
length += (int)sspiWriter.WrittenCount;
177+
}
169178
}
170179
}
171-
}
172180

173-
int feOffset = length;
174-
// calculate and reserve the required bytes for the featureEx
175-
length = ApplyFeatureExData(requestedFeatures, recoverySessionData, fedAuthFeatureExtensionData, useFeatureExt, length);
176-
177-
WriteLoginData(rec,
178-
requestedFeatures,
179-
recoverySessionData,
180-
fedAuthFeatureExtensionData,
181-
encrypt,
182-
encryptedPassword,
183-
encryptedChangePassword,
184-
encryptedPasswordLengthInBytes,
185-
encryptedChangePasswordLengthInBytes,
186-
useFeatureExt,
187-
userName,
188-
length,
189-
feOffset,
190-
clientInterfaceName,
191-
sspiWriter is { } ? sspiWriter.WrittenSpan : ReadOnlySpan<byte>.Empty);
192-
193-
if (sspiWriter is not null)
181+
int feOffset = length;
182+
// calculate and reserve the required bytes for the featureEx
183+
length = ApplyFeatureExData(requestedFeatures, recoverySessionData, fedAuthFeatureExtensionData, useFeatureExt, length);
184+
185+
WriteLoginData(rec,
186+
requestedFeatures,
187+
recoverySessionData,
188+
fedAuthFeatureExtensionData,
189+
encrypt,
190+
encryptedPassword,
191+
encryptedChangePassword,
192+
encryptedPasswordLengthInBytes,
193+
encryptedChangePasswordLengthInBytes,
194+
useFeatureExt,
195+
userName,
196+
length,
197+
feOffset,
198+
clientInterfaceName,
199+
sspiWriter is { } ? sspiWriter.WrittenSpan : ReadOnlySpan<byte>.Empty);
200+
}
201+
finally
194202
{
195-
SqlObjectPools.BufferWriter.Return(sspiWriter);
203+
if (sspiWriter is not null)
204+
{
205+
SqlObjectPools.BufferWriter.Return(sspiWriter);
206+
}
196207
}
197208

198209
_physicalStateObj.WritePacket(TdsEnums.HARDFLUSH);

0 commit comments

Comments
 (0)