Bug 1417827: Convert internal char* to nsCString in DNS.h r=bagder
authorJeff Hemphill <jthemphill@gmail.com>
Wed, 29 Nov 2017 23:30:27 -0800
changeset 422598 535a93ad81a1c1d7d81205373b92c49191637f4b
parent 422597 f9641e4d0f4cbe01f15f7e0493030ac0a3699766
child 422599 cb84b0e19643f215290c367bee8ad63e4c6aef7b
push id65205
push uservalentin.gosu@gmail.com
push dateThu, 14 Jun 2018 23:59:38 +0000
treeherderautoland@cb84b0e19643 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbagder
bugs1417827
milestone62.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 1417827: Convert internal char* to nsCString in DNS.h r=bagder MozReview-Commit-ID: Js1mXiKaKnt
netwerk/dns/DNS.cpp
netwerk/dns/DNS.h
netwerk/dns/GetAddrInfo.cpp
netwerk/dns/TRR.cpp
netwerk/dns/TRRService.cpp
netwerk/dns/nsDNSService2.cpp
netwerk/dns/nsHostResolver.cpp
--- a/netwerk/dns/DNS.cpp
+++ b/netwerk/dns/DNS.cpp
@@ -4,16 +4,17 @@
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/net/DNS.h"
 
 #include "mozilla/Assertions.h"
 #include "mozilla/mozalloc.h"
 #include "mozilla/ArrayUtils.h"
+#include "nsString.h"
 #include <string.h>
 
 #ifdef XP_WIN
 #include "ws2tcpip.h"
 #endif
 
 namespace mozilla {
 namespace net {
@@ -288,120 +289,91 @@ NetAddrElement::NetAddrElement(const PRN
 
 NetAddrElement::NetAddrElement(const NetAddrElement& netAddr)
 {
   mAddress = netAddr.mAddress;
 }
 
 NetAddrElement::~NetAddrElement() = default;
 
-AddrInfo::AddrInfo(const char *host, const PRAddrInfo *prAddrInfo,
-                   bool disableIPv4, bool filterNameCollision, const char *cname)
-  : mHostName(nullptr)
-  , mCanonicalName(nullptr)
+AddrInfo::AddrInfo(const nsACString& host, const PRAddrInfo *prAddrInfo,
+                   bool disableIPv4, bool filterNameCollision,
+                   const nsACString& cname)
+  : mHostName(host)
+  , mCanonicalName(cname)
   , ttl(NO_TTL_DATA)
   , mFromTRR(false)
 {
   MOZ_ASSERT(prAddrInfo, "Cannot construct AddrInfo with a null prAddrInfo pointer!");
   const uint32_t nameCollisionAddr = htonl(0x7f003535); // 127.0.53.53
 
-  Init(host, cname);
   PRNetAddr tmpAddr;
   void *iter = nullptr;
   do {
     iter = PR_EnumerateAddrInfo(iter, prAddrInfo, 0, &tmpAddr);
     bool addIt = iter &&
         (!disableIPv4 || tmpAddr.raw.family != PR_AF_INET) &&
         (!filterNameCollision || tmpAddr.raw.family != PR_AF_INET || (tmpAddr.inet.ip != nameCollisionAddr));
     if (addIt) {
         auto *addrElement = new NetAddrElement(&tmpAddr);
         mAddresses.insertBack(addrElement);
     }
   } while (iter);
 }
 
-AddrInfo::AddrInfo(const char *host, const char *cname, unsigned int aTRR)
-  : mHostName(nullptr)
-  , mCanonicalName(nullptr)
+AddrInfo::AddrInfo(const nsACString& host, const nsACString& cname, unsigned int aTRR)
+  : mHostName(host)
+  , mCanonicalName(cname)
   , ttl(NO_TTL_DATA)
   , mFromTRR(aTRR)
 {
-  Init(host, cname);
 }
 
-AddrInfo::AddrInfo(const char *host, unsigned int aTRR)
-  : mHostName(nullptr)
-  , mCanonicalName(nullptr)
+AddrInfo::AddrInfo(const nsACString& host, unsigned int aTRR)
+  : mHostName(host)
+  , mCanonicalName(EmptyCString())
   , ttl(NO_TTL_DATA)
   , mFromTRR(aTRR)
 {
-  Init(host, nullptr);
 }
 
 // deep copy constructor
 AddrInfo::AddrInfo(const AddrInfo *src)
 {
-  mHostName = nullptr;
-  if (src->mHostName) {
-    mHostName = strdup(src->mHostName);
-  }
-  mCanonicalName = nullptr;
-  if (src->mCanonicalName) {
-    mCanonicalName = strdup(src->mCanonicalName);
-  }
+  mHostName = src->mHostName;
+  mCanonicalName = src->mCanonicalName;
   ttl = src->ttl;
   mFromTRR = src->mFromTRR;
 
   for (auto element = src->mAddresses.getFirst(); element;
        element = element->getNext()) {
     AddAddress(new NetAddrElement(*element));
   }
 }
 
 AddrInfo::~AddrInfo()
 {
   NetAddrElement *addrElement;
   while ((addrElement = mAddresses.popLast())) {
     delete addrElement;
   }
-  free(mHostName);
-  free(mCanonicalName);
-}
-
-void
-AddrInfo::Init(const char *host, const char *cname)
-{
-  MOZ_ASSERT(host, "Cannot initialize AddrInfo with a null host pointer!");
-
-  ttl = NO_TTL_DATA;
-  size_t hostlen = strlen(host);
-  mHostName = static_cast<char*>(moz_xmalloc(hostlen + 1));
-  memcpy(mHostName, host, hostlen + 1);
-  if (cname) {
-    size_t cnameLen = strlen(cname);
-    mCanonicalName = static_cast<char*>(moz_xmalloc(cnameLen + 1));
-    memcpy(mCanonicalName, cname, cnameLen + 1);
-  }
-  else {
-    mCanonicalName = nullptr;
-  }
 }
 
 void
 AddrInfo::AddAddress(NetAddrElement *address)
 {
   MOZ_ASSERT(address, "Cannot add the address to an uninitialized list");
 
   mAddresses.insertBack(address);
 }
 
 size_t
 AddrInfo::SizeOfIncludingThis(MallocSizeOf mallocSizeOf) const
 {
   size_t n = mallocSizeOf(this);
-  n += mallocSizeOf(mHostName);
-  n += mallocSizeOf(mCanonicalName);
+  n += mHostName.SizeOfExcludingThisIfUnshared(mallocSizeOf);
+  n += mCanonicalName.SizeOfExcludingThisIfUnshared(mallocSizeOf);
   n += mAddresses.sizeOfExcludingThis(mallocSizeOf);
   return n;
 }
 
 } // namespace net
 } // namespace mozilla
--- a/netwerk/dns/DNS.h
+++ b/netwerk/dns/DNS.h
@@ -3,16 +3,17 @@
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #ifndef DNS_h_
 #define DNS_h_
 
 #include "nscore.h"
+#include "nsString.h"
 #include "prio.h"
 #include "prnetdb.h"
 #include "plstr.h"
 #include "mozilla/LinkedList.h"
 #include "mozilla/MemoryReporting.h"
 
 #if !defined(XP_WIN)
 #include <arpa/inet.h>
@@ -128,44 +129,43 @@ public:
   NetAddrElement(const NetAddrElement& netAddr);
   ~NetAddrElement();
 
   NetAddr mAddress;
 };
 
 class AddrInfo {
 public:
-  // Creates an AddrInfo object. It calls the AddrInfo(const char*, const char*)
-  // to initialize the host and the cname.
-  explicit AddrInfo(const char *host, const PRAddrInfo *prAddrInfo, bool disableIPv4,
-                    bool filterNameCollision, const char *cname);
+  // Creates an AddrInfo object.
+  explicit AddrInfo(const nsACString& host, const PRAddrInfo *prAddrInfo,
+           bool disableIPv4, bool filterNameCollision,
+           const nsACString& cname);
 
   // Creates a basic AddrInfo object (initialize only the host, cname and TRR type).
-  explicit AddrInfo(const char *host, const char *cname, unsigned int TRRType);
+  explicit AddrInfo(const nsACString& host, const nsACString& cname, unsigned int TRRType);
 
   // Creates a basic AddrInfo object (initialize only the host and TRR status).
-  explicit AddrInfo(const char *host, unsigned int TRRType);
+  explicit AddrInfo(const nsACString& host, unsigned int TRRType);
   ~AddrInfo();
 
   explicit AddrInfo(const AddrInfo *src); // copy
 
   void AddAddress(NetAddrElement *address);
 
   size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
 
-  char *mHostName;
-  char *mCanonicalName;
+  nsCString mHostName;
+  nsCString mCanonicalName;
   uint32_t ttl;
   static const uint32_t NO_TTL_DATA = (uint32_t) -1;
 
   LinkedList<NetAddrElement> mAddresses;
   unsigned int IsTRR() { return mFromTRR; }
 private:
   unsigned int mFromTRR;
-  void Init(const char *host, const char *cname);
 };
 
 // Copies the contents of a PRNetAddr to a NetAddr.
 // Does not do a ptr safety check!
 void PRNetAddrToNetAddr(const PRNetAddr *prAddr, NetAddr *addr);
 
 // Copies the contents of a NetAddr to a PRNetAddr.
 // Does not do a ptr safety check!
--- a/netwerk/dns/GetAddrInfo.cpp
+++ b/netwerk/dns/GetAddrInfo.cpp
@@ -277,18 +277,18 @@ static MOZ_ALWAYS_INLINE nsresult
   }
 
   const char* canonName = nullptr;
   if (aFlags & nsHostResolver::RES_CANON_NAME) {
     canonName = PR_GetCanonNameFromAddrInfo(prai);
   }
 
   bool filterNameCollision = !(aFlags & nsHostResolver::RES_ALLOW_NAME_COLLISION);
-  nsAutoPtr<AddrInfo> ai(new AddrInfo(aCanonHost, prai, disableIPv4,
-                                      filterNameCollision, canonName));
+  nsAutoPtr<AddrInfo> ai(new AddrInfo(nsCString(aCanonHost), prai, disableIPv4,
+                                      filterNameCollision, nsCString(canonName)));
   PR_FreeAddrInfo(prai);
   if (ai->mAddresses.isEmpty()) {
     return NS_ERROR_UNKNOWN_HOST;
   }
 
   *aAddrInfo = ai.forget();
 
   return NS_OK;
@@ -344,18 +344,18 @@ GetAddrInfo(const char* aHost, uint16_t 
   nsresult rv = _GetAddrInfo_Portable(aHost, aAddressFamily, aFlags,
                                       aAddrInfo);
 
 #ifdef DNSQUERY_AVAILABLE
   if (aGetTtl && NS_SUCCEEDED(rv)) {
     // Figure out the canonical name, or if that fails, just use the host name
     // we have.
     const char *name = nullptr;
-    if (*aAddrInfo != nullptr && (*aAddrInfo)->mCanonicalName) {
-      name = (*aAddrInfo)->mCanonicalName;
+    if (*aAddrInfo != nullptr && !(*aAddrInfo)->mCanonicalName.IsEmpty()) {
+      name = (*aAddrInfo)->mCanonicalName.get();
     } else {
       name = aHost;
     }
 
     LOG("Getting TTL for %s (cname = %s).", aHost, name);
     uint32_t ttl = 0;
     nsresult ttlRv = _GetTTLData_Windows(name, &ttl, aAddressFamily);
     if (NS_SUCCEEDED(ttlRv)) {
--- a/netwerk/dns/TRR.cpp
+++ b/netwerk/dns/TRR.cpp
@@ -819,17 +819,17 @@ TRR::DohDecode(nsCString &aHost)
   }
   return NS_OK;
 }
 
 nsresult
 TRR::ReturnData()
 {
   // create and populate an AddrInfo instance to pass on
-  nsAutoPtr<AddrInfo> ai(new AddrInfo(mHost.get(), mType));
+  nsAutoPtr<AddrInfo> ai(new AddrInfo(mHost, mType));
   DOHaddr *item;
   uint32_t ttl = AddrInfo::NO_TTL_DATA;
   while ((item = static_cast<DOHaddr*>(mDNS.mAddresses.popFirst()))) {
     PRNetAddr prAddr;
     NetAddrToPRNetAddr(&item->mNet, &prAddr);
     auto *addrElement = new NetAddrElement(&prAddr);
     ai->AddAddress(addrElement);
     if (item->mTtl < ttl) {
@@ -852,17 +852,17 @@ TRR::ReturnData()
 nsresult
 TRR::FailData()
 {
   if (!mHostResolver) {
     return NS_ERROR_FAILURE;
   }
   // create and populate an TRR AddrInfo instance to pass on to signal that
   // this comes from TRR
-  AddrInfo *ai = new AddrInfo(mHost.get(), mType);
+  AddrInfo *ai = new AddrInfo(mHost, mType);
 
   (void)mHostResolver->CompleteLookup(mRec, NS_ERROR_FAILURE, ai, mPB);
   mHostResolver = nullptr;
   mRec = nullptr;
   return NS_OK;
 }
 
 nsresult
--- a/netwerk/dns/TRRService.cpp
+++ b/netwerk/dns/TRRService.cpp
@@ -584,20 +584,20 @@ TRRService::CompleteLookup(nsHostRecord 
       }
       mRetryConfirmInterval = 1000;
     }
     return LOOKUP_OK;
   }
 
   // when called without a host record, this is a domain name check response.
   if (NS_SUCCEEDED(status)) {
-    LOG(("TRR verified %s to be fine!\n", newRRSet->mHostName));
+    LOG(("TRR verified %s to be fine!\n", newRRSet->mHostName.get()));
   } else {
-    LOG(("TRR says %s doesn't resolve as NS!\n", newRRSet->mHostName));
-    TRRBlacklist(nsCString(newRRSet->mHostName), pb, false);
+    LOG(("TRR says %s doesn't resolve as NS!\n", newRRSet->mHostName.get()));
+    TRRBlacklist(newRRSet->mHostName, pb, false);
   }
   return LOOKUP_OK;
 }
 
 #undef LOG
 
 } // namespace net
 } // namespace mozilla
--- a/netwerk/dns/nsDNSService2.cpp
+++ b/netwerk/dns/nsDNSService2.cpp
@@ -87,25 +87,28 @@ NS_IMPL_ISUPPORTS(nsDNSRecord, nsIDNSRec
 NS_IMETHODIMP
 nsDNSRecord::GetCanonicalName(nsACString &result)
 {
     // this method should only be called if we have a CNAME
     NS_ENSURE_TRUE(mHostRecord->flags & nsHostResolver::RES_CANON_NAME,
                    NS_ERROR_NOT_AVAILABLE);
 
     MutexAutoLock lock(mHostRecord->addr_info_lock);
-    if (mHostRecord->addr_info) {
-        const char* cname = mHostRecord->addr_info->mCanonicalName ?
-            mHostRecord->addr_info->mCanonicalName :
-            mHostRecord->addr_info->mHostName;
-        result.Assign(cname);
+
+    // if the record is for an IP address literal, then the canonical
+    // host name is the IP address literal.
+    if (!mHostRecord->addr_info) {
+        result = mHostRecord->host;
+        return NS_OK;
+    }
+
+    if (mHostRecord->addr_info->mCanonicalName.IsEmpty()) {
+        result = mHostRecord->addr_info->mHostName;
     } else {
-        // if the record is for an IP address literal, then the canonical
-        // host name is the IP address literal.
-        result = mHostRecord->host;
+        result = mHostRecord->addr_info->mCanonicalName;
     }
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDNSRecord::IsTRR(bool *retval)
 {
     MutexAutoLock lock(mHostRecord->addr_info_lock);
--- a/netwerk/dns/nsHostResolver.cpp
+++ b/netwerk/dns/nsHostResolver.cpp
@@ -1413,17 +1413,17 @@ merge_rrset(AddrInfo *rrto, AddrInfo *rr
 
 static bool
 different_rrset(AddrInfo *rrset1, AddrInfo *rrset2)
 {
     if (!rrset1 || !rrset2) {
         return true;
     }
 
-    LOG(("different_rrset %s\n", rrset1->mHostName));
+    LOG(("different_rrset %s\n", rrset1->mHostName.get()));
     nsTArray<NetAddr> orderedSet1;
     nsTArray<NetAddr> orderedSet2;
 
     if (rrset1->IsTRR() != rrset2->IsTRR()) {
         return true;
     }
 
     for (NetAddrElement *element = rrset1->mAddresses.getFirst();
@@ -1490,17 +1490,17 @@ nsHostResolver::CompleteLookup(nsHostRec
     rec->mResolving--;
     LOG(("nsHostResolver::CompleteLookup %s %p %X trr=%d stillResolving=%d\n",
          rec->host.get(), aNewRRSet, (unsigned int)status,
          aNewRRSet ? aNewRRSet->IsTRR() : 0, rec->mResolving));
 
     if (trrResult) {
         MutexAutoLock trrlock(rec->mTrrLock);
         LOG(("TRR lookup Complete (%d) %s %s\n",
-             newRRSet->IsTRR(), newRRSet->mHostName,
+             newRRSet->IsTRR(), newRRSet->mHostName.get(),
              NS_SUCCEEDED(status) ? "OK" : "FAILED"));
         MOZ_ASSERT(TRROutstanding());
         if (newRRSet->IsTRR() == TRRTYPE_A) {
             MOZ_ASSERT(rec->mTrrA);
             rec->mTrrA = nullptr;
             rec->mTrrAUsed = NS_SUCCEEDED(status) ? nsHostRecord::OK : nsHostRecord::FAILED;
         } else if (newRRSet->IsTRR() == TRRTYPE_AAAA) {
             MOZ_ASSERT(rec->mTrrAAAA);