Bug 1621251 Replace NS_ADDREF(*var = other) with *var = do_AddRef(other).take() in /netwerk r=valentin
authoranjali1903 <anjali.jha1903@gmail.com>
Tue, 17 Mar 2020 15:05:26 +0000
changeset 519152 a1586e76eba614dbfae0055a0c754f22066aaa7e
parent 519151 6a82a7622c92a5b79a05420868eae66d76b7d7a6
child 519153 1d20f201aaad9bf5fa87dae9a208e3f9cf36fd09
push id37223
push userbtara@mozilla.com
push dateTue, 17 Mar 2020 21:49:18 +0000
treeherdermozilla-central@7c83f04c82e9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvalentin
bugs1621251
milestone76.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 1621251 Replace NS_ADDREF(*var = other) with *var = do_AddRef(other).take() in /netwerk r=valentin Differential Revision: https://phabricator.services.mozilla.com/D66818
netwerk/base/DefaultURI.cpp
netwerk/base/EventTokenBucket.cpp
netwerk/base/LoadInfo.cpp
netwerk/base/nsBaseChannel.cpp
netwerk/base/nsInputStreamChannel.cpp
netwerk/base/nsLoadGroup.cpp
netwerk/base/nsProtocolProxyService.cpp
netwerk/base/nsSocketTransport2.cpp
netwerk/base/nsStreamTransportService.cpp
netwerk/base/nsSyncStreamListener.cpp
netwerk/base/nsTransportUtils.cpp
netwerk/cache/nsCacheEntry.cpp
netwerk/cache/nsCacheService.cpp
netwerk/cache/nsCacheSession.cpp
netwerk/cache/nsDiskCacheDeviceSQL.cpp
netwerk/cache2/CacheFile.cpp
netwerk/dns/DNSRequestChild.cpp
netwerk/dns/nsDNSService2.cpp
netwerk/protocol/http/HttpBaseChannel.cpp
netwerk/protocol/http/HttpTransactionParent.cpp
netwerk/protocol/http/nsHttpHandler.cpp
netwerk/protocol/http/nsHttpNTLMAuth.cpp
netwerk/protocol/viewsource/nsViewSourceChannel.cpp
netwerk/protocol/websocket/BaseWebSocketChannel.cpp
netwerk/streamconv/converters/mozTXTToHTMLConv.cpp
netwerk/streamconv/converters/nsFTPDirListingConv.cpp
netwerk/streamconv/converters/nsMultiMixedConv.cpp
netwerk/streamconv/nsStreamConverterService.cpp
--- a/netwerk/base/DefaultURI.cpp
+++ b/netwerk/base/DefaultURI.cpp
@@ -11,33 +11,33 @@ namespace net {
 
 #define NS_DEFAULTURI_CID                            \
   { /* 04445aa0-fd27-4c99-bd41-6be6318ae92c */       \
     0x04445aa0, 0xfd27, 0x4c99, {                    \
       0xbd, 0x41, 0x6b, 0xe6, 0x31, 0x8a, 0xe9, 0x2c \
     }                                                \
   }
 
-#define ASSIGN_AND_ADDREF_THIS(ptrToMutator) \
-  do {                                       \
-    if (ptrToMutator) {                      \
-      NS_ADDREF(*ptrToMutator = this);       \
-    }                                        \
+#define ASSIGN_AND_ADDREF_THIS(ptrToMutator)    \
+  do {                                          \
+    if (ptrToMutator) {                         \
+      *(ptrToMutator) = do_AddRef(this).take(); \
+    }                                           \
   } while (0)
 
 static NS_DEFINE_CID(kDefaultURICID, NS_DEFAULTURI_CID);
 
 NS_IMPL_ADDREF(DefaultURI)
 NS_IMPL_RELEASE(DefaultURI)
 NS_INTERFACE_TABLE_HEAD(DefaultURI)
   NS_INTERFACE_TABLE(DefaultURI, nsIURI, nsISerializable, nsIClassInfo)
   NS_INTERFACE_TABLE_TO_MAP_SEGUE
-  if (aIID.Equals(kDefaultURICID))
+  if (aIID.Equals(kDefaultURICID)) {
     foundInterface = static_cast<nsIURI*>(this);
-  else
+  } else
     NS_INTERFACE_MAP_ENTRY(nsISizeOf)
 NS_INTERFACE_MAP_END
 
 //----------------------------------------------------------------------------
 // nsISerializable
 //----------------------------------------------------------------------------
 
 NS_IMETHODIMP DefaultURI::Read(nsIObjectInputStream* aInputStream) {
--- a/netwerk/base/EventTokenBucket.cpp
+++ b/netwerk/base/EventTokenBucket.cpp
@@ -211,17 +211,17 @@ nsresult EventTokenBucket::SubmitEvent(A
   if (mStopped || !mTimer) return NS_ERROR_FAILURE;
 
   UpdateCredits();
 
   RefPtr<TokenBucketCancelable> cancelEvent = new TokenBucketCancelable(event);
   // When this function exits the cancelEvent needs 2 references, one for the
   // mEvents queue and one for the caller of SubmitEvent()
 
-  NS_ADDREF(*cancelable = cancelEvent.get());
+  *cancelable = do_AddRef(cancelEvent).take();
 
   if (mPaused || !TryImmediateDispatch(cancelEvent.get())) {
     // queue it
     SOCKET_LOG(("   queued\n"));
     mEvents.Push(cancelEvent.forget().take());
     UpdateTimer();
   } else {
     SOCKET_LOG(("   dispatched synchronously\n"));
--- a/netwerk/base/LoadInfo.cpp
+++ b/netwerk/base/LoadInfo.cpp
@@ -715,17 +715,17 @@ LoadInfo::GetLoadingPrincipal(nsIPrincip
   NS_IF_ADDREF(*aLoadingPrincipal = mLoadingPrincipal);
   return NS_OK;
 }
 
 nsIPrincipal* LoadInfo::LoadingPrincipal() { return mLoadingPrincipal; }
 
 NS_IMETHODIMP
 LoadInfo::GetTriggeringPrincipal(nsIPrincipal** aTriggeringPrincipal) {
-  NS_ADDREF(*aTriggeringPrincipal = mTriggeringPrincipal);
+  *aTriggeringPrincipal = do_AddRef(mTriggeringPrincipal).take();
   return NS_OK;
 }
 
 nsIPrincipal* LoadInfo::TriggeringPrincipal() { return mTriggeringPrincipal; }
 
 NS_IMETHODIMP
 LoadInfo::GetPrincipalToInherit(nsIPrincipal** aPrincipalToInherit) {
   NS_IF_ADDREF(*aPrincipalToInherit = mPrincipalToInherit);
--- a/netwerk/base/nsBaseChannel.cpp
+++ b/netwerk/base/nsBaseChannel.cpp
@@ -461,18 +461,18 @@ nsBaseChannel::SetLoadGroup(nsILoadGroup
   return NS_OK;
 }
 
 //-----------------------------------------------------------------------------
 // nsBaseChannel::nsIChannel
 
 NS_IMETHODIMP
 nsBaseChannel::GetOriginalURI(nsIURI** aURI) {
-  *aURI = OriginalURI();
-  NS_ADDREF(*aURI);
+  RefPtr<nsIURI> uri = OriginalURI();
+  uri.forget(aURI);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsBaseChannel::SetOriginalURI(nsIURI* aURI) {
   NS_ENSURE_ARG_POINTER(aURI);
   mOriginalURI = aURI;
   return NS_OK;
--- a/netwerk/base/nsInputStreamChannel.cpp
+++ b/netwerk/base/nsInputStreamChannel.cpp
@@ -28,17 +28,17 @@ nsresult nsInputStreamChannel::OpenConte
     } else if (NS_FAILED(rv)) {
       return rv;
     }
     mContentLength = avail;
   }
 
   EnableSynthesizedProgressEvents(true);
 
-  NS_ADDREF(*result = mContentStream);
+  *result = do_AddRef(mContentStream).take();
   return NS_OK;
 }
 
 //-----------------------------------------------------------------------------
 // nsInputStreamChannel::nsISupports
 
 NS_IMPL_ISUPPORTS_INHERITED(nsInputStreamChannel, nsBaseChannel,
                             nsIInputStreamChannel)
--- a/netwerk/base/nsLoadGroup.cpp
+++ b/netwerk/base/nsLoadGroup.cpp
@@ -688,32 +688,32 @@ nsLoadGroup::GetParentLoadGroup(nsILoadG
 NS_IMETHODIMP
 nsLoadGroup::SetParentLoadGroup(nsILoadGroup* aParentLoadGroup) {
   mParentLoadGroup = do_GetWeakReference(aParentLoadGroup);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsLoadGroup::GetChildLoadGroup(nsILoadGroup** aChildLoadGroup) {
-  NS_ADDREF(*aChildLoadGroup = this);
+  *aChildLoadGroup = do_AddRef(this).take();
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsLoadGroup::GetRootLoadGroup(nsILoadGroup** aRootLoadGroup) {
   // first recursively try the root load group of our parent
   nsCOMPtr<nsILoadGroupChild> ancestor = do_QueryReferent(mParentLoadGroup);
   if (ancestor) return ancestor->GetRootLoadGroup(aRootLoadGroup);
 
   // next recursively try the root load group of our own load grop
   ancestor = do_QueryInterface(mLoadGroup);
   if (ancestor) return ancestor->GetRootLoadGroup(aRootLoadGroup);
 
   // finally just return this
-  NS_ADDREF(*aRootLoadGroup = this);
+  *aRootLoadGroup = do_AddRef(this).take();
   return NS_OK;
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 // nsISupportsPriority methods:
 
 NS_IMETHODIMP
 nsLoadGroup::GetPriority(int32_t* aValue) {
--- a/netwerk/base/nsProtocolProxyService.cpp
+++ b/netwerk/base/nsProtocolProxyService.cpp
@@ -1234,17 +1234,17 @@ const char* nsProtocolProxyService::Extr
     if (type == kProxyType_HTTP) {
       port = 80;
     } else if (type == kProxyType_HTTPS) {
       port = 443;
     } else {
       port = 1080;
     }
 
-    nsProxyInfo* pi = new nsProxyInfo();
+    RefPtr<nsProxyInfo> pi = new nsProxyInfo();
     pi->mType = type;
     pi->mFlags = flags;
     pi->mResolveFlags = aResolveFlags;
     pi->mTimeout = mFailedProxyTimeout;
 
     // www.foo.com:8080 and http://www.foo.com:8080
     nsDependentCSubstring maybeURL(start, end - start);
     nsCOMPtr<nsIURI> pacURI;
@@ -1266,17 +1266,17 @@ const char* nsProtocolProxyService::Extr
 
       int32_t tPort;
       if (NS_SUCCEEDED(pacURI->GetPort(&tPort)) && tPort != -1) {
         port = tPort;
       }
       pi->mPort = port;
     }
 
-    NS_ADDREF(*result = pi);
+    pi.forget(result);
   }
 
   while (*end == ';' || *end == ' ' || *end == '\t') ++end;
   return end;
 }
 
 void nsProtocolProxyService::GetProxyKey(nsProxyInfo* pi, nsCString& key) {
   key.AssignASCII(pi->mType);
@@ -1671,17 +1671,17 @@ nsProtocolProxyService::GetFailoverForPr
   //       not already tried going DIRECT.  This is something that the
   //       classic codebase supported; however, IE6 does not prompt.
 
   if (!pi->mNext) return NS_ERROR_NOT_AVAILABLE;
 
   LOG(("PAC failover from %s %s:%d to %s %s:%d\n", pi->mType, pi->mHost.get(),
        pi->mPort, pi->mNext->mType, pi->mNext->mHost.get(), pi->mNext->mPort));
 
-  NS_ADDREF(*aResult = pi->mNext);
+  *aResult = do_AddRef(pi->mNext).take();
   return NS_OK;
 }
 
 namespace {  // anon
 
 class ProxyFilterPositionComparator {
   typedef RefPtr<nsProtocolProxyService::FilterLink> FilterLinkRef;
 
@@ -2000,33 +2000,32 @@ nsresult nsProtocolProxyService::NewProx
   if (aPort <= 0) aPort = -1;
 
   nsCOMPtr<nsProxyInfo> failover;
   if (aFailoverProxy) {
     failover = do_QueryInterface(aFailoverProxy);
     NS_ENSURE_ARG(failover);
   }
 
-  nsProxyInfo* proxyInfo = new nsProxyInfo();
-  if (!proxyInfo) return NS_ERROR_OUT_OF_MEMORY;
+  RefPtr<nsProxyInfo> proxyInfo = new nsProxyInfo();
 
   proxyInfo->mType = aType;
   proxyInfo->mHost = aHost;
   proxyInfo->mPort = aPort;
   proxyInfo->mUsername = aUsername;
   proxyInfo->mPassword = aPassword;
   proxyInfo->mFlags = aFlags;
   proxyInfo->mResolveFlags = aResolveFlags;
   proxyInfo->mTimeout =
       aFailoverTimeout == UINT32_MAX ? mFailedProxyTimeout : aFailoverTimeout;
   proxyInfo->mProxyAuthorizationHeader = aProxyAuthorizationHeader;
   proxyInfo->mConnectionIsolationKey = aConnectionIsolationKey;
   failover.swap(proxyInfo->mNext);
 
-  NS_ADDREF(*aResult = proxyInfo);
+  proxyInfo.forget(aResult);
   return NS_OK;
 }
 
 nsresult nsProtocolProxyService::Resolve_Internal(nsIChannel* channel,
                                                   const nsProtocolInfo& info,
                                                   uint32_t flags,
                                                   bool* usePACThread,
                                                   nsIProxyInfo** result) {
--- a/netwerk/base/nsSocketTransport2.cpp
+++ b/netwerk/base/nsSocketTransport2.cpp
@@ -2849,30 +2849,31 @@ nsSocketTransport::Bind(NetAddr* aLocalA
 NS_IMETHODIMP
 nsSocketTransport::GetScriptablePeerAddr(nsINetAddr** addr) {
   NetAddr rawAddr;
 
   nsresult rv;
   rv = GetPeerAddr(&rawAddr);
   if (NS_FAILED(rv)) return rv;
 
-  NS_ADDREF(*addr = new nsNetAddr(&rawAddr));
-
+  RefPtr<nsNetAddr> netaddr = new nsNetAddr(&rawAddr);
+  netaddr.forget(addr);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsSocketTransport::GetScriptableSelfAddr(nsINetAddr** addr) {
   NetAddr rawAddr;
 
   nsresult rv;
   rv = GetSelfAddr(&rawAddr);
   if (NS_FAILED(rv)) return rv;
 
-  NS_ADDREF(*addr = new nsNetAddr(&rawAddr));
+  RefPtr<nsNetAddr> netaddr = new nsNetAddr(&rawAddr);
+  netaddr.forget(addr);
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsSocketTransport::GetTimeout(uint32_t type, uint32_t* value) {
   NS_ENSURE_ARG_MAX(type, nsISocketTransport::TIMEOUT_READ_WRITE);
   *value = (uint32_t)mTimeouts[type];
--- a/netwerk/base/nsStreamTransportService.cpp
+++ b/netwerk/base/nsStreamTransportService.cpp
@@ -86,19 +86,21 @@ nsInputStreamTransport::OpenInputStream(
                    nonblocking, true, segsize, segcount);
   if (NS_FAILED(rv)) return rv;
 
   mInProgress = true;
 
   // startup async copy process...
   rv = NS_AsyncCopy(this, pipeOut, target, NS_ASYNCCOPY_VIA_WRITESEGMENTS,
                     segsize);
-  if (NS_SUCCEEDED(rv)) NS_ADDREF(*result = mPipeIn);
-
-  return rv;
+  if (NS_FAILED(rv)) {
+    return rv;
+  }
+  *result = do_AddRef(mPipeIn).take();
+  return NS_OK;
 }
 
 NS_IMETHODIMP
 nsInputStreamTransport::OpenOutputStream(uint32_t flags, uint32_t segsize,
                                          uint32_t segcount,
                                          nsIOutputStream** result) {
   // this transport only supports reading!
   MOZ_ASSERT_UNREACHABLE("nsInputStreamTransport::OpenOutputStream");
@@ -246,20 +248,19 @@ nsStreamTransportService::IsOnCurrentThr
   NS_ENSURE_TRUE(pool, NS_ERROR_NOT_INITIALIZED);
   return pool->IsOnCurrentThread(result);
 }
 
 NS_IMETHODIMP
 nsStreamTransportService::CreateInputTransport(nsIInputStream* stream,
                                                bool closeWhenDone,
                                                nsITransport** result) {
-  nsInputStreamTransport* trans =
+  RefPtr<nsInputStreamTransport> trans =
       new nsInputStreamTransport(stream, closeWhenDone);
-  if (!trans) return NS_ERROR_OUT_OF_MEMORY;
-  NS_ADDREF(*result = trans);
+  trans.forget(result);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsStreamTransportService::Observe(nsISupports* subject, const char* topic,
                                   const char16_t* data) {
   NS_ASSERTION(strcmp(topic, "xpcom-shutdown-threads") == 0, "oops");
 
--- a/netwerk/base/nsSyncStreamListener.cpp
+++ b/netwerk/base/nsSyncStreamListener.cpp
@@ -45,17 +45,17 @@ NS_IMPL_ISUPPORTS(nsSyncStreamListener, 
                   nsIInputStream, nsISyncStreamListener)
 
 //-----------------------------------------------------------------------------
 // nsSyncStreamListener::nsISyncStreamListener
 //-----------------------------------------------------------------------------
 
 NS_IMETHODIMP
 nsSyncStreamListener::GetInputStream(nsIInputStream** result) {
-  NS_ADDREF(*result = this);
+  *result = do_AddRef(this).take();
   return NS_OK;
 }
 
 //-----------------------------------------------------------------------------
 // nsSyncStreamListener::nsIStreamListener
 //-----------------------------------------------------------------------------
 
 NS_IMETHODIMP
--- a/netwerk/base/nsTransportUtils.cpp
+++ b/netwerk/base/nsTransportUtils.cpp
@@ -114,13 +114,13 @@ nsTransportEventSinkProxy::OnTransportSt
   return rv;
 }
 
 //-----------------------------------------------------------------------------
 
 nsresult net_NewTransportEventSinkProxy(nsITransportEventSink** result,
                                         nsITransportEventSink* sink,
                                         nsIEventTarget* target) {
-  *result = new nsTransportEventSinkProxy(sink, target);
-  if (!*result) return NS_ERROR_OUT_OF_MEMORY;
-  NS_ADDREF(*result);
+  RefPtr<nsTransportEventSinkProxy> res =
+      new nsTransportEventSinkProxy(sink, target);
+  res.forget(result);
   return NS_OK;
 }
--- a/netwerk/cache/nsCacheEntry.cpp
+++ b/netwerk/cache/nsCacheEntry.cpp
@@ -157,17 +157,17 @@ nsresult nsCacheEntry::CreateDescriptor(
 
   if (descriptor == nullptr) return NS_ERROR_OUT_OF_MEMORY;
 
   PR_APPEND_LINK(descriptor, &mDescriptorQ);
 
   CACHE_LOG_DEBUG(("  descriptor %p created for request %p on entry %p\n",
                    descriptor, request, this));
 
-  NS_ADDREF(*result = descriptor);
+  *result = do_AddRef(descriptor).take();
   return NS_OK;
 }
 
 bool nsCacheEntry::RemoveRequest(nsCacheRequest* request) {
   // XXX if debug: verify this request belongs to this entry
   PR_REMOVE_AND_INIT_LINK(request);
 
   // return true if this entry should stay active
--- a/netwerk/cache/nsCacheService.cpp
+++ b/netwerk/cache/nsCacheService.cpp
@@ -777,17 +777,17 @@ NS_IMETHODIMP nsCacheService::GetCacheIO
   // read from the main thread without the lock. This is useful to prevent
   // blocking the main thread on other cache operations.
   if (!NS_IsMainThread()) {
     Lock(LOCK_TELEM(NSCACHESERVICE_GETCACHEIOTARGET));
   }
 
   nsresult rv;
   if (mCacheIOThread) {
-    NS_ADDREF(*aCacheIOTarget = mCacheIOThread);
+    *aCacheIOTarget = do_AddRef(mCacheIOThread).take();
     rv = NS_OK;
   } else {
     *aCacheIOTarget = nullptr;
     rv = NS_ERROR_NOT_AVAILABLE;
   }
 
   if (!NS_IsMainThread()) {
     Unlock();
@@ -813,17 +813,17 @@ NS_IMETHODIMP nsCacheService::GetLockHel
  * Internal Methods
  */
 nsresult nsCacheService::GetOfflineDevice(nsOfflineCacheDevice** aDevice) {
   if (!mOfflineDevice) {
     nsresult rv = CreateOfflineDevice();
     NS_ENSURE_SUCCESS(rv, rv);
   }
 
-  NS_ADDREF(*aDevice = mOfflineDevice);
+  *aDevice = do_AddRef(mOfflineDevice).take();
   return NS_OK;
 }
 
 nsresult nsCacheService::GetCustomOfflineDevice(
     nsIFile* aProfileDir, int32_t aQuota, nsOfflineCacheDevice** aDevice) {
   nsresult rv;
 
   nsAutoString profilePath;
--- a/netwerk/cache/nsCacheSession.cpp
+++ b/netwerk/cache/nsCacheSession.cpp
@@ -47,21 +47,17 @@ NS_IMETHODIMP nsCacheSession::SetProfile
     return NS_ERROR_UNEXPECTED;
   }
 
   mProfileDir = profileDir;
   return NS_OK;
 }
 
 NS_IMETHODIMP nsCacheSession::GetProfileDirectory(nsIFile** profileDir) {
-  if (mProfileDir)
-    NS_ADDREF(*profileDir = mProfileDir);
-  else
-    *profileDir = nullptr;
-
+  *profileDir = do_AddRef(mProfileDir).take();
   return NS_OK;
 }
 
 NS_IMETHODIMP nsCacheSession::SetDoomEntriesIfExpired(
     bool doomEntriesIfExpired) {
   if (doomEntriesIfExpired)
     MarkDoomEntriesIfExpired();
   else
--- a/netwerk/cache/nsDiskCacheDeviceSQL.cpp
+++ b/netwerk/cache/nsDiskCacheDeviceSQL.cpp
@@ -677,21 +677,17 @@ nsApplicationCache::GetGroupID(nsACStrin
 NS_IMETHODIMP
 nsApplicationCache::GetClientID(nsACString& out) {
   out = mClientID;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsApplicationCache::GetProfileDirectory(nsIFile** out) {
-  if (mDevice->BaseDirectory())
-    NS_ADDREF(*out = mDevice->BaseDirectory());
-  else
-    *out = nullptr;
-
+  *out = do_AddRef(mDevice->BaseDirectory()).take();
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsApplicationCache::GetActive(bool* out) {
   NS_ENSURE_TRUE(mDevice, NS_ERROR_NOT_AVAILABLE);
 
   *out = mDevice->IsActiveCache(mGroup, mClientID);
--- a/netwerk/cache2/CacheFile.cpp
+++ b/netwerk/cache2/CacheFile.cpp
@@ -741,17 +741,17 @@ nsresult CacheFile::OpenInputStream(nsIC
       new CacheFileInputStream(this, aEntryHandle, false);
   LOG(("CacheFile::OpenInputStream() - Creating new input stream %p [this=%p]",
        input, this));
 
   mInputs.AppendElement(input);
   NS_ADDREF(input);
 
   mDataAccessed = true;
-  NS_ADDREF(*_retval = input);
+  *_retval = do_AddRef(input).take();
   return NS_OK;
 }
 
 nsresult CacheFile::OpenAlternativeInputStream(nsICacheEntry* aEntryHandle,
                                                const char* aAltDataType,
                                                nsIInputStream** _retval) {
   CacheFileAutoLock lock(this);
 
@@ -809,17 +809,18 @@ nsresult CacheFile::OpenAlternativeInput
       ("CacheFile::OpenAlternativeInputStream() - Creating new input stream %p "
        "[this=%p]",
        input, this));
 
   mInputs.AppendElement(input);
   NS_ADDREF(input);
 
   mDataAccessed = true;
-  NS_ADDREF(*_retval = input);
+  *_retval = do_AddRef(input).take();
+
   return NS_OK;
 }
 
 nsresult CacheFile::OpenOutputStream(CacheOutputCloseListener* aCloseListener,
                                      nsIOutputStream** _retval) {
   CacheFileAutoLock lock(this);
 
   MOZ_ASSERT(mHandle || mMemoryOnly || mOpeningFile);
@@ -884,17 +885,17 @@ nsresult CacheFile::OpenOutputStream(Cac
   mOutput = new CacheFileOutputStream(this, aCloseListener, false);
 
   LOG(
       ("CacheFile::OpenOutputStream() - Creating new output stream %p "
        "[this=%p]",
        mOutput, this));
 
   mDataAccessed = true;
-  NS_ADDREF(*_retval = mOutput);
+  *_retval = do_AddRef(mOutput).take();
   return NS_OK;
 }
 
 nsresult CacheFile::OpenAlternativeOutputStream(
     CacheOutputCloseListener* aCloseListener, const char* aAltDataType,
     nsIAsyncOutputStream** _retval) {
   CacheFileAutoLock lock(this);
 
@@ -974,17 +975,17 @@ nsresult CacheFile::OpenAlternativeOutpu
 
   LOG(
       ("CacheFile::OpenAlternativeOutputStream() - Creating new output stream "
        "%p [this=%p]",
        mOutput, this));
 
   mDataAccessed = true;
   mAltDataType = aAltDataType;
-  NS_ADDREF(*_retval = mOutput);
+  *_retval = do_AddRef(mOutput).take();
   return NS_OK;
 }
 
 nsresult CacheFile::SetMemoryOnly() {
   CacheFileAutoLock lock(this);
 
   LOG(("CacheFile::SetMemoryOnly() mMemoryOnly=%d [this=%p]", mMemoryOnly,
        this));
--- a/netwerk/dns/DNSRequestChild.cpp
+++ b/netwerk/dns/DNSRequestChild.cpp
@@ -114,17 +114,18 @@ ChildDNSRecord::GetAddresses(nsTArray<Ne
 
 // shamelessly copied from nsDNSRecord
 NS_IMETHODIMP
 ChildDNSRecord::GetScriptableNextAddr(uint16_t port, nsINetAddr** result) {
   NetAddr addr;
   nsresult rv = GetNextAddr(port, &addr);
   if (NS_FAILED(rv)) return rv;
 
-  NS_ADDREF(*result = new nsNetAddr(&addr));
+  RefPtr<nsNetAddr> netaddr = new nsNetAddr(&addr);
+  netaddr.forget(result);
 
   return NS_OK;
 }
 
 // also copied from nsDNSRecord
 NS_IMETHODIMP
 ChildDNSRecord::GetNextAddrAsString(nsACString& result) {
   NetAddr addr;
--- a/netwerk/dns/nsDNSService2.cpp
+++ b/netwerk/dns/nsDNSService2.cpp
@@ -246,17 +246,18 @@ nsDNSRecord::GetAddresses(nsTArray<NetAd
 }
 
 NS_IMETHODIMP
 nsDNSRecord::GetScriptableNextAddr(uint16_t port, nsINetAddr** result) {
   NetAddr addr;
   nsresult rv = GetNextAddr(port, &addr);
   if (NS_FAILED(rv)) return rv;
 
-  NS_ADDREF(*result = new nsNetAddr(&addr));
+  RefPtr<nsNetAddr> netaddr = new nsNetAddr(&addr);
+  netaddr.forget(result);
 
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDNSRecord::GetNextAddrAsString(nsACString& result) {
   NetAddr addr;
   nsresult rv = GetNextAddr(0, &addr);
--- a/netwerk/protocol/http/HttpBaseChannel.cpp
+++ b/netwerk/protocol/http/HttpBaseChannel.cpp
@@ -509,35 +509,33 @@ HttpBaseChannel::SetDocshellUserAgentOve
 
 //-----------------------------------------------------------------------------
 // HttpBaseChannel::nsIChannel
 //-----------------------------------------------------------------------------
 
 NS_IMETHODIMP
 HttpBaseChannel::GetOriginalURI(nsIURI** aOriginalURI) {
   NS_ENSURE_ARG_POINTER(aOriginalURI);
-  *aOriginalURI = mOriginalURI;
-  NS_ADDREF(*aOriginalURI);
+  *aOriginalURI = do_AddRef(mOriginalURI).take();
   return NS_OK;
 }
 
 NS_IMETHODIMP
 HttpBaseChannel::SetOriginalURI(nsIURI* aOriginalURI) {
   ENSURE_CALLED_BEFORE_CONNECT();
 
   NS_ENSURE_ARG_POINTER(aOriginalURI);
   mOriginalURI = aOriginalURI;
   return NS_OK;
 }
 
 NS_IMETHODIMP
 HttpBaseChannel::GetURI(nsIURI** aURI) {
   NS_ENSURE_ARG_POINTER(aURI);
-  *aURI = mURI;
-  NS_ADDREF(*aURI);
+  *aURI = do_AddRef(mURI).take();
   return NS_OK;
 }
 
 NS_IMETHODIMP
 HttpBaseChannel::GetOwner(nsISupports** aOwner) {
   NS_ENSURE_ARG_POINTER(aOwner);
   *aOwner = mOwner;
   NS_IF_ADDREF(*aOwner);
@@ -1264,18 +1262,19 @@ HttpBaseChannel::GetContentEncodings(nsI
   }
 
   nsAutoCString encoding;
   Unused << mResponseHead->GetHeader(nsHttp::Content_Encoding, encoding);
   if (encoding.IsEmpty()) {
     *aEncodings = nullptr;
     return NS_OK;
   }
-  nsContentEncodings* enumerator = new nsContentEncodings(this, encoding.get());
-  NS_ADDREF(*aEncodings = enumerator);
+  RefPtr<nsContentEncodings> enumerator =
+      new nsContentEncodings(this, encoding.get());
+  enumerator.forget(aEncodings);
   return NS_OK;
 }
 
 //-----------------------------------------------------------------------------
 // HttpBaseChannel::nsContentEncodings <public>
 //-----------------------------------------------------------------------------
 
 HttpBaseChannel::nsContentEncodings::nsContentEncodings(
--- a/netwerk/protocol/http/HttpTransactionParent.cpp
+++ b/netwerk/protocol/http/HttpTransactionParent.cpp
@@ -175,17 +175,17 @@ nsresult HttpTransactionParent::Init(
 
   return NS_OK;
 }
 
 nsresult HttpTransactionParent::AsyncRead(nsIStreamListener* listener,
                                           nsIRequest** pump) {
   MOZ_ASSERT(pump);
 
-  NS_ADDREF(*pump = this);
+  *pump = do_AddRef(this).take();
   mChannel = listener;
   return NS_OK;
 }
 
 void HttpTransactionParent::SetClassOfService(uint32_t classOfService) {
   Unused << SendUpdateClassOfService(classOfService);
 }
 
--- a/netwerk/protocol/http/nsHttpHandler.cpp
+++ b/netwerk/protocol/http/nsHttpHandler.cpp
@@ -764,18 +764,17 @@ nsresult nsHttpHandler::GetStreamConvert
   if (!mStreamConvSvc) {
     nsresult rv;
     nsCOMPtr<nsIStreamConverterService> service =
         do_GetService(NS_STREAMCONVERTERSERVICE_CONTRACTID, &rv);
     if (NS_FAILED(rv)) return rv;
     mStreamConvSvc = new nsMainThreadPtrHolder<nsIStreamConverterService>(
         "nsHttpHandler::mStreamConvSvc", service);
   }
-  *result = mStreamConvSvc;
-  NS_ADDREF(*result);
+  *result = do_AddRef(mStreamConvSvc.get()).take();
   return NS_OK;
 }
 
 nsISiteSecurityService* nsHttpHandler::GetSSService() {
   if (!mSSService) {
     nsCOMPtr<nsISiteSecurityService> service =
         do_GetService(NS_SSSERVICE_CONTRACTID);
     mSSService = new nsMainThreadPtrHolder<nsISiteSecurityService>(
@@ -792,17 +791,17 @@ nsICookieService* nsHttpHandler::GetCook
         "nsHttpHandler::mCookieService", service);
   }
   return mCookieService;
 }
 
 nsresult nsHttpHandler::GetIOService(nsIIOService** result) {
   NS_ENSURE_ARG_POINTER(result);
 
-  NS_ADDREF(*result = mIOService);
+  *result = do_AddRef(mIOService.get()).take();
   return NS_OK;
 }
 
 uint32_t nsHttpHandler::Get32BitsOfPseudoRandom() {
   // only confirm rand seeding on socket thread
   MOZ_ASSERT(OnSocketThread(), "not on socket thread");
 
   // rand() provides different amounts of PRNG on different platforms.
--- a/netwerk/protocol/http/nsHttpNTLMAuth.cpp
+++ b/netwerk/protocol/http/nsHttpNTLMAuth.cpp
@@ -212,19 +212,18 @@ nsHttpNTLMAuth::ChallengeReceived(nsIHtt
 #endif
 
     // If no native support was available. Fall back on our internal NTLM
     // implementation.
     if (!module) {
       if (!*sessionState) {
         // Remember the fact that we cannot use the "sys-ntlm" module,
         // so we don't ever bother trying again for this auth domain.
-        *sessionState = new nsNTLMSessionState();
-        if (!*sessionState) return NS_ERROR_OUT_OF_MEMORY;
-        NS_ADDREF(*sessionState);
+        RefPtr<nsNTLMSessionState> state = new nsNTLMSessionState();
+        state.forget(sessionState);
       }
 
       // Use our internal NTLM implementation. Note, this is less secure,
       // see bug 520607 for details.
       LOG(("Trying to fall back on internal ntlm auth.\n"));
       module = nsIAuthModule::CreateInstance("ntlm");
 
       mUseNative = false;
--- a/netwerk/protocol/viewsource/nsViewSourceChannel.cpp
+++ b/netwerk/protocol/viewsource/nsViewSourceChannel.cpp
@@ -266,19 +266,17 @@ nsViewSourceChannel::Resume(void) {
   return mChannel->Resume();
 }
 
 ////////////////////////////////////////////////////////////////////////////////
 // nsIChannel methods:
 
 NS_IMETHODIMP
 nsViewSourceChannel::GetOriginalURI(nsIURI** aURI) {
-  NS_ASSERTION(aURI, "Null out param!");
-  *aURI = mOriginalURI;
-  NS_ADDREF(*aURI);
+  *aURI = do_AddRef(mOriginalURI).take();
   return NS_OK;
 }
 
 NS_IMETHODIMP
 nsViewSourceChannel::SetOriginalURI(nsIURI* aURI) {
   NS_ENSURE_ARG_POINTER(aURI);
   mOriginalURI = aURI;
   return NS_OK;
--- a/netwerk/protocol/websocket/BaseWebSocketChannel.cpp
+++ b/netwerk/protocol/websocket/BaseWebSocketChannel.cpp
@@ -68,29 +68,30 @@ BaseWebSocketChannel::BaseWebSocketChann
 // BaseWebSocketChannel::nsIWebSocketChannel
 //-----------------------------------------------------------------------------
 
 NS_IMETHODIMP
 BaseWebSocketChannel::GetOriginalURI(nsIURI** aOriginalURI) {
   LOG(("BaseWebSocketChannel::GetOriginalURI() %p\n", this));
 
   if (!mOriginalURI) return NS_ERROR_NOT_INITIALIZED;
-  NS_ADDREF(*aOriginalURI = mOriginalURI);
+  *aOriginalURI = do_AddRef(mOriginalURI).take();
   return NS_OK;
 }
 
 NS_IMETHODIMP
 BaseWebSocketChannel::GetURI(nsIURI** aURI) {
   LOG(("BaseWebSocketChannel::GetURI() %p\n", this));
 
   if (!mOriginalURI) return NS_ERROR_NOT_INITIALIZED;
-  if (mURI)
-    NS_ADDREF(*aURI = mURI);
-  else
-    NS_ADDREF(*aURI = mOriginalURI);
+  if (mURI) {
+    *aURI = do_AddRef(mURI).take();
+  } else {
+    *aURI = do_AddRef(mOriginalURI).take();
+  }
   return NS_OK;
 }
 
 NS_IMETHODIMP
 BaseWebSocketChannel::GetNotificationCallbacks(
     nsIInterfaceRequestor** aNotificationCallbacks) {
   LOG(("BaseWebSocketChannel::GetNotificationCallbacks() %p\n", this));
   NS_IF_ADDREF(*aNotificationCallbacks = mCallbacks);
--- a/netwerk/streamconv/converters/mozTXTToHTMLConv.cpp
+++ b/netwerk/streamconv/converters/mozTXTToHTMLConv.cpp
@@ -1246,15 +1246,13 @@ mozTXTToHTMLConv::CiteLevelTXT(const cha
   *_retval = CiteLevelTXT(line, *logLineStart);
   return NS_OK;
 }
 
 nsresult MOZ_NewTXTToHTMLConv(mozTXTToHTMLConv** aConv) {
   MOZ_ASSERT(aConv != nullptr, "null ptr");
   if (!aConv) return NS_ERROR_NULL_POINTER;
 
-  *aConv = new mozTXTToHTMLConv();
-  if (!*aConv) return NS_ERROR_OUT_OF_MEMORY;
-
-  NS_ADDREF(*aConv);
+  RefPtr<mozTXTToHTMLConv> conv = new mozTXTToHTMLConv();
+  conv.forget(aConv);
   //    return (*aConv)->Init();
   return NS_OK;
 }
--- a/netwerk/streamconv/converters/nsFTPDirListingConv.cpp
+++ b/netwerk/streamconv/converters/nsFTPDirListingConv.cpp
@@ -330,14 +330,12 @@ char* nsFTPDirListingConv::DigestBufferL
 
   return line;
 }
 
 nsresult NS_NewFTPDirListingConv(nsFTPDirListingConv** aFTPDirListingConv) {
   MOZ_ASSERT(aFTPDirListingConv != nullptr, "null ptr");
   if (!aFTPDirListingConv) return NS_ERROR_NULL_POINTER;
 
-  *aFTPDirListingConv = new nsFTPDirListingConv();
-  if (!*aFTPDirListingConv) return NS_ERROR_OUT_OF_MEMORY;
-
-  NS_ADDREF(*aFTPDirListingConv);
+  RefPtr<nsFTPDirListingConv> conv = new nsFTPDirListingConv();
+  conv.forget(aFTPDirListingConv);
   return NS_OK;
 }
--- a/netwerk/streamconv/converters/nsMultiMixedConv.cpp
+++ b/netwerk/streamconv/converters/nsMultiMixedConv.cpp
@@ -1032,15 +1032,13 @@ nsresult nsMultiMixedConv::ProcessHeader
       break;
   }
 
   return NS_OK;
 }
 
 nsresult NS_NewMultiMixedConv(nsMultiMixedConv** aMultiMixedConv) {
   MOZ_ASSERT(aMultiMixedConv != nullptr, "null ptr");
-  if (!aMultiMixedConv) return NS_ERROR_NULL_POINTER;
 
-  *aMultiMixedConv = new nsMultiMixedConv();
-
-  NS_ADDREF(*aMultiMixedConv);
+  RefPtr<nsMultiMixedConv> conv = new nsMultiMixedConv();
+  conv.forget(aMultiMixedConv);
   return NS_OK;
 }
--- a/netwerk/streamconv/nsStreamConverterService.cpp
+++ b/netwerk/streamconv/nsStreamConverterService.cpp
@@ -537,13 +537,13 @@ nsStreamConverterService::AsyncConvertDa
 
   return rv;
 }
 
 nsresult NS_NewStreamConv(nsStreamConverterService** aStreamConv) {
   MOZ_ASSERT(aStreamConv != nullptr, "null ptr");
   if (!aStreamConv) return NS_ERROR_NULL_POINTER;
 
-  *aStreamConv = new nsStreamConverterService();
-  NS_ADDREF(*aStreamConv);
+  RefPtr<nsStreamConverterService> conv = new nsStreamConverterService();
+  conv.forget(aStreamConv);
 
   return NS_OK;
 }