bug 1444532 - fix a leak in SHA256 in nsHttpConnectionInfo.cpp r?mcmanus draft
authorDavid Keeler <dkeeler@mozilla.com>
Fri, 09 Mar 2018 14:16:57 -0800
changeset 765618 219c4dcda73b1c0bf207d46587c9241615a38a98
parent 765246 f1965cf7425fe422c9e9c78018f11b97e0a0f229
push id102119
push userbmo:dkeeler@mozilla.com
push dateFri, 09 Mar 2018 22:20:36 +0000
reviewersmcmanus
bugs1444532, 1200802
milestone60.0a1
bug 1444532 - fix a leak in SHA256 in nsHttpConnectionInfo.cpp r?mcmanus The original code (from bug 1200802) declared an XPCOM object as a static bare pointer, which for future reference is probably never the right thing to do. It might have worked if it was cleared before shutdown but it never was. MozReview-Commit-ID: EMe7wgzm6zv
netwerk/protocol/http/nsHttpConnectionInfo.cpp
--- a/netwerk/protocol/http/nsHttpConnectionInfo.cpp
+++ b/netwerk/protocol/http/nsHttpConnectionInfo.cpp
@@ -9,43 +9,39 @@
 
 // Log on level :5, instead of default :4.
 #undef LOG
 #define LOG(args) LOG5(args)
 #undef LOG_ENABLED
 #define LOG_ENABLED() LOG5_ENABLED()
 
 #include "nsHttpConnectionInfo.h"
+
 #include "mozilla/net/DNS.h"
-#include "prnetdb.h"
+#include "nsComponentManagerUtils.h"
 #include "nsICryptoHash.h"
-#include "nsComponentManagerUtils.h"
 #include "nsIProtocolProxyService.h"
+#include "nsNetCID.h"
+#include "prnetdb.h"
 
 static nsresult
 SHA256(const char* aPlainText, nsAutoCString& aResult)
 {
-  static nsICryptoHash* hasher = nullptr;
-  nsresult rv;
-  if (!hasher) {
-    rv = CallCreateInstance("@mozilla.org/security/hash;1", &hasher);
+    nsresult rv;
+    nsCOMPtr<nsICryptoHash> hasher =
+      do_CreateInstance(NS_CRYPTO_HASH_CONTRACTID, &rv);
     if (NS_FAILED(rv)) {
       LOG(("nsHttpDigestAuth: no crypto hash!\n"));
       return rv;
     }
-  }
-
-  rv = hasher->Init(nsICryptoHash::SHA256);
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  rv = hasher->Update((unsigned char*) aPlainText, strlen(aPlainText));
-  NS_ENSURE_SUCCESS(rv, rv);
-
-  rv = hasher->Finish(false, aResult);
-  return rv;
+    rv = hasher->Init(nsICryptoHash::SHA256);
+    NS_ENSURE_SUCCESS(rv, rv);
+    rv = hasher->Update((unsigned char*) aPlainText, strlen(aPlainText));
+    NS_ENSURE_SUCCESS(rv, rv);
+    return hasher->Finish(false, aResult);
 }
 
 namespace mozilla {
 namespace net {
 
 nsHttpConnectionInfo::nsHttpConnectionInfo(const nsACString &originHost,
                                            int32_t originPort,
                                            const nsACString &npnToken,