[BTLS]: Fix a ref-counting leak in X509ChainImplBtls. Fixes #45687. (#3811)
authorMartin Baulig <martin.baulig@xamarin.com>
Tue, 25 Oct 2016 13:42:08 +0000 (15:42 +0200)
committerMartin Baulig <martin.baulig@xamarin.com>
Tue, 25 Oct 2016 13:42:38 +0000 (15:42 +0200)
* [BTLS]: Fix a ref-counting leak in X509ChainImplBtls.  Fixes #45687.

* Set 'untrusted = null' in Dispose().

(cherry picked from commit c9a67ffc5addabe74015f7d8f33977f4ab416075)

mcs/class/System/Mono.Btls/X509ChainImplBtls.cs

index 79a985ce6749562f1871b38961e1ebee4faeafe6..c75bec311ef0dd3d6ee519b10c4f738e10542950 100644 (file)
@@ -62,7 +62,7 @@ namespace Mono.Btls
                                untrusted = new X509Certificate2Collection ();
                                policy.ExtraStore = untrusted;
                                for (int i = 0; i < untrustedChain.Count; i++) {
-                                       using (var cert = untrustedChain.GetCertificate (i))
+                                       var cert = untrustedChain.GetCertificate (i);
                                        using (var impl = new X509CertificateImplBtls (cert))
                                                untrusted.Add (new X509Certificate2 (impl));
                                }
@@ -109,8 +109,8 @@ namespace Mono.Btls
 
                                for (int i = 0; i < certificates.Length; i++) {
                                        var cert = chain.GetCertificate (i);
-                                       var impl = new X509CertificateImplBtls (cert);
-                                       certificates [i] = new X509Certificate2 (impl);
+                                       using (var impl = new X509CertificateImplBtls (cert))
+                                               certificates [i] = new X509Certificate2 (impl);
                                        elements.Add (certificates [i]);
                                }
 
@@ -163,6 +163,12 @@ namespace Mono.Btls
                                if (untrusted != null) {
                                        foreach (var cert in untrusted)
                                                cert.Dispose ();
+                                       untrusted = null;
+                               }
+                               if (certificates != null) {
+                                       foreach (var cert in certificates)
+                                               cert.Dispose ();
+                                       certificates = null;
                                }
                        }
                        base.Dispose (disposing);