Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 40b1f93

Browse files
committed
Fix X509CertificateCollection.SyncRoot
Not only is it allocating on every call, but it's returning null on the first call.
1 parent f66d9f3 commit 40b1f93

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/System.Security.Cryptography.X509Certificates/src/System/Security/Cryptography/X509Certificates/X509CertificateCollection.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Globalization;
1010
using System.Collections.Generic;
1111
using System.Runtime.InteropServices;
12+
using System.Threading;
1213

1314
using Interlocked = System.Threading.Interlocked;
1415

@@ -49,7 +50,11 @@ Object ICollection.SyncRoot
4950
{
5051
get
5152
{
52-
return Interlocked.CompareExchange(ref _syncRoot, new Object(), null);
53+
if (_syncRoot == null)
54+
{
55+
Interlocked.CompareExchange(ref _syncRoot, new object(), null);
56+
}
57+
return _syncRoot;
5358
}
5459
}
5560

src/System.Security.Cryptography.X509Certificates/tests/CollectionTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,14 @@ public static void ExportPkcs7()
317317
TestExportStore(X509ContentType.Pkcs7);
318318
}
319319

320+
[Fact]
321+
public static void X509CertificateCollectionSyncRoot()
322+
{
323+
var cc = new X509CertificateCollection();
324+
Assert.NotNull(((ICollection)cc).SyncRoot);
325+
Assert.Same(((ICollection)cc).SyncRoot, ((ICollection)cc).SyncRoot);
326+
}
327+
320328
private static void TestExportSingleCert(X509ContentType ct)
321329
{
322330
using (var msCer = new X509Certificate2(TestData.MsCertificate))

0 commit comments

Comments
 (0)