Bug 1535357 - Make nsHttpConnectionInfo::Clone() return already_AddRefed<>. r=valentin
authorAndrew McCreight <continuation@gmail.com>
Thu, 14 Mar 2019 19:42:23 +0000
changeset 524942 be4af23f4c1af764eb00603e07a01bea22c51266
parent 524941 9f7a585bb83aca4e3ac2ae3681451bc7f2a43372
child 524943 af5b991568da82059cd12ce23a5b2fddf925a032
push id2032
push userffxbld-merge
push dateMon, 13 May 2019 09:36:57 +0000
treeherdermozilla-release@455c1065dcbe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvalentin
bugs1535357
milestone67.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1535357 - Make nsHttpConnectionInfo::Clone() return already_AddRefed<>. r=valentin This will make it harder for callers of this method to accidentally leak the connection info. Differential Revision: https://phabricator.services.mozilla.com/D23563
netwerk/protocol/http/nsHttpConnectionInfo.cpp
netwerk/protocol/http/nsHttpConnectionInfo.h
--- a/netwerk/protocol/http/nsHttpConnectionInfo.cpp
+++ b/netwerk/protocol/http/nsHttpConnectionInfo.cpp
@@ -225,18 +225,18 @@ void nsHttpConnectionInfo::BuildHashKey(
 
 void nsHttpConnectionInfo::SetOriginServer(const nsACString &host,
                                            int32_t port) {
   mOrigin = host;
   mOriginPort = port == -1 ? DefaultPort() : port;
   BuildHashKey();
 }
 
-nsHttpConnectionInfo *nsHttpConnectionInfo::Clone() const {
-  nsHttpConnectionInfo *clone;
+already_AddRefed<nsHttpConnectionInfo> nsHttpConnectionInfo::Clone() const {
+  RefPtr<nsHttpConnectionInfo> clone;
   if (mRoutedHost.IsEmpty()) {
     clone =
         new nsHttpConnectionInfo(mOrigin, mOriginPort, mNPNToken, mUsername,
                                  mProxyInfo, mOriginAttributes, mEndToEndSSL);
   } else {
     MOZ_ASSERT(mEndToEndSSL);
     clone = new nsHttpConnectionInfo(mOrigin, mOriginPort, mNPNToken, mUsername,
                                      mProxyInfo, mOriginAttributes, mRoutedHost,
@@ -251,22 +251,23 @@ nsHttpConnectionInfo *nsHttpConnectionIn
   clone->SetBeConservative(GetBeConservative());
   clone->SetTlsFlags(GetTlsFlags());
   clone->SetTrrUsed(GetTrrUsed());
   clone->SetTrrDisabled(GetTrrDisabled());
   clone->SetIPv4Disabled(GetIPv4Disabled());
   clone->SetIPv6Disabled(GetIPv6Disabled());
   MOZ_ASSERT(clone->Equals(this));
 
-  return clone;
+  return clone.forget();
 }
 
 void nsHttpConnectionInfo::CloneAsDirectRoute(nsHttpConnectionInfo **outCI) {
   if (mRoutedHost.IsEmpty()) {
-    *outCI = Clone();
+    RefPtr<nsHttpConnectionInfo> clone = Clone();
+    clone.forget(outCI);
     return;
   }
 
   RefPtr<nsHttpConnectionInfo> clone =
       new nsHttpConnectionInfo(mOrigin, mOriginPort, EmptyCString(), mUsername,
                                mProxyInfo, mOriginAttributes, mEndToEndSSL);
   // Make sure the anonymous, insecure-scheme, and private flags are transferred
   clone->SetAnonymous(GetAnonymous());
--- a/netwerk/protocol/http/nsHttpConnectionInfo.h
+++ b/netwerk/protocol/http/nsHttpConnectionInfo.h
@@ -8,16 +8,17 @@
 #define nsHttpConnectionInfo_h__
 
 #include "nsHttp.h"
 #include "nsProxyInfo.h"
 #include "nsCOMPtr.h"
 #include "nsStringFwd.h"
 #include "mozilla/Logging.h"
 #include "mozilla/BasePrincipal.h"
+#include "mozilla/AlreadyAddRefed.h"
 #include "ARefBase.h"
 
 //-----------------------------------------------------------------------------
 // nsHttpConnectionInfo - holds the properties of a connection
 //-----------------------------------------------------------------------------
 
 // http:// uris through a proxy will all share the same CI, because they can
 // all use the same connection. (modulo pb and anonymous flags). They just use
@@ -65,17 +66,17 @@ class nsHttpConnectionInfo final : publi
   const char *Origin() const { return mOrigin.get(); }
   int32_t OriginPort() const { return mOriginPort; }
 
   const nsCString &GetRoutedHost() const { return mRoutedHost; }
   const char *RoutedHost() const { return mRoutedHost.get(); }
   int32_t RoutedPort() const { return mRoutedPort; }
 
   // OK to treat these as an infalible allocation
-  nsHttpConnectionInfo *Clone() const;
+  already_AddRefed<nsHttpConnectionInfo> Clone() const;
   void CloneAsDirectRoute(nsHttpConnectionInfo **outParam);
   MOZ_MUST_USE nsresult CreateWildCard(nsHttpConnectionInfo **outParam);
 
   const char *ProxyHost() const {
     return mProxyInfo ? mProxyInfo->Host().get() : nullptr;
   }
   int32_t ProxyPort() const { return mProxyInfo ? mProxyInfo->Port() : -1; }
   const char *ProxyType() const {