Replacing Lock with Volatile.Read for SecureString Length (#16112)
authorMaryam Ariyan <maryam.ariyan@microsoft.com>
Wed, 31 Jan 2018 04:18:08 +0000 (23:18 -0500)
committerDan Moseley <danmose@microsoft.com>
Wed, 31 Jan 2018 04:18:08 +0000 (04:18 +0000)
* Replacing Lock on SecureString Length with Volatile Read

Fixes #26685

* Replacing Lock with Volatile Read/Write for IsReadOnly() and MakeReadOnly()

src/mscorlib/shared/System/Security/SecureString.cs

index 9059f90..22f15ac 100644 (file)
@@ -2,9 +2,9 @@
 // The .NET Foundation licenses this file to you under the MIT license.
 // See the LICENSE file in the project root for more information.
 
-using System;
 using System.Diagnostics;
 using System.Runtime.InteropServices;
+using System.Threading;
 
 namespace System.Security
 {
@@ -43,11 +43,8 @@ namespace System.Security
         {
             get
             {
-                lock (_methodLock)
-                {
-                    EnsureNotDisposed();
-                    return _decryptedLength;
-                }
+                EnsureNotDisposed();
+                return Volatile.Read(ref _decryptedLength);
             }
         }
 
@@ -108,20 +105,14 @@ namespace System.Security
 
         public bool IsReadOnly()
         {
-            lock (_methodLock)
-            {
-                EnsureNotDisposed();
-                return _readOnly;
-            }
+            EnsureNotDisposed();
+            return Volatile.Read(ref _readOnly);
         }
 
         public void MakeReadOnly()
         {
-            lock (_methodLock)
-            {
-                EnsureNotDisposed();
-                _readOnly = true;
-            }
+            EnsureNotDisposed();
+            Volatile.Write(ref _readOnly, true);
         }
 
         public void RemoveAt(int index)