@@ -215,8 +215,10 @@ private uint GetSniPacket(PacketHandle packet, ref uint dataSize)
215215 return SniPacketGetData ( packet , _inBuff , ref dataSize ) ;
216216 }
217217
218- private void SetBufferSecureStrings ( )
218+ private bool TrySetBufferSecureStrings ( )
219219 {
220+ bool mustClearBuffer = false ;
221+
220222 if ( _securePasswords != null )
221223 {
222224 for ( int i = 0 ; i < _securePasswords . Length ; i ++ )
@@ -240,6 +242,8 @@ private void SetBufferSecureStrings()
240242 }
241243 TdsParserStaticMethods . ObfuscatePassword ( data ) ;
242244 data . CopyTo ( _outBuff , _securePasswordOffsetsInBuffer [ i ] ) ;
245+
246+ mustClearBuffer = true ;
243247 }
244248 finally
245249 {
@@ -248,6 +252,8 @@ private void SetBufferSecureStrings()
248252 }
249253 }
250254 }
255+
256+ return mustClearBuffer ;
251257 }
252258
253259 public void ReadAsyncCallback ( PacketHandle packet , uint error ) =>
@@ -738,9 +744,13 @@ private Task WriteSni(bool canAccumulate)
738744 {
739745 // Prepare packet, and write to packet.
740746 PacketHandle packet = GetResetWritePacket ( _outBytesUsed ) ;
747+ bool mustClearBuffer = TrySetBufferSecureStrings ( ) ;
741748
742- SetBufferSecureStrings ( ) ;
743749 SetPacketData ( packet , _outBuff , _outBytesUsed ) ;
750+ if ( mustClearBuffer )
751+ {
752+ _outBuff . AsSpan ( 0 , _outBytesUsed ) . Clear ( ) ;
753+ }
744754
745755 Debug . Assert ( Parser . Connection . _parserLock . ThreadMayHaveLock ( ) , "Thread is writing without taking the connection lock" ) ;
746756 Task task = SNIWritePacket ( packet , out _ , canAccumulate , callerHasConnectionLock : true ) ;
0 commit comments