Skip to content

Commit 5c4ebcc

Browse files
authored
Lock over the same object to serialize access to the buffers (#117982)
1 parent 587f703 commit 5c4ebcc

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/libraries/System.Net.Security/src/System/Net/Security/Pal.OSX/SafeDeleteSslContext.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ internal sealed class SafeDeleteSslContext : SafeDeleteContext
1717
// mapped from OSX error codes
1818
private const int InitialBufferSize = 2048;
1919
private readonly SafeSslHandle _sslContext;
20+
private readonly object _lock = new object();
2021
private ArrayBuffer _inputBuffer = new ArrayBuffer(InitialBufferSize);
2122
private ArrayBuffer _outputBuffer = new ArrayBuffer(InitialBufferSize);
2223

@@ -202,7 +203,7 @@ protected override void Dispose(bool disposing)
202203
SafeSslHandle sslContext = _sslContext;
203204
if (null != sslContext)
204205
{
205-
lock (_sslContext)
206+
lock (_lock)
206207
{
207208
_inputBuffer.Dispose();
208209
_outputBuffer.Dispose();
@@ -225,7 +226,7 @@ private static unsafe int WriteToConnection(IntPtr connection, byte* data, void*
225226
// but if we were to pool the buffers we would have a potential use-after-free issue.
226227
try
227228
{
228-
lock (context)
229+
lock (context._lock)
229230
{
230231
ulong length = (ulong)*dataLength;
231232
Debug.Assert(length <= int.MaxValue);
@@ -257,7 +258,7 @@ private static unsafe int ReadFromConnection(IntPtr connection, byte* data, void
257258

258259
try
259260
{
260-
lock (context)
261+
lock (context._lock)
261262
{
262263
ulong toRead = (ulong)*dataLength;
263264

@@ -294,7 +295,7 @@ private static unsafe int ReadFromConnection(IntPtr connection, byte* data, void
294295

295296
internal void Write(ReadOnlySpan<byte> buf)
296297
{
297-
lock (_sslContext)
298+
lock (_lock)
298299
{
299300
_inputBuffer.EnsureAvailableSpace(buf.Length);
300301
buf.CopyTo(_inputBuffer.AvailableSpan);
@@ -306,7 +307,7 @@ internal void Write(ReadOnlySpan<byte> buf)
306307

307308
internal void ReadPendingWrites(ref ProtocolToken token)
308309
{
309-
lock (_sslContext)
310+
lock (_lock)
310311
{
311312
if (_outputBuffer.ActiveLength == 0)
312313
{
@@ -328,7 +329,7 @@ internal int ReadPendingWrites(byte[] buf, int offset, int count)
328329
Debug.Assert(count >= 0);
329330
Debug.Assert(count <= buf.Length - offset);
330331

331-
lock (_sslContext)
332+
lock (_lock)
332333
{
333334
int limit = Math.Min(count, _outputBuffer.ActiveLength);
334335

0 commit comments

Comments
 (0)