Bug 1417827: Convert internal char* to nsCString in DNS.h r=valentin
authorJeff Hemphill <jthemphill@gmail.com>
Wed, 29 Nov 2017 23:30:27 -0800
changeset 394516 34cfc821e335cafbb98e18d400c2abbcbc32c19f
parent 394515 0f6ee76c53931458b43a32f62adff085d9fe0cb7
child 394517 b6ee65d949c5fddfb971759f4e3dfdae28d7ffd2
push id56348
push uservalentin.gosu@gmail.com
push dateFri, 01 Dec 2017 04:40:01 +0000
treeherderautoland@34cfc821e335 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersvalentin
bugs1417827
milestone59.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=valentin MozReview-Commit-ID: Js1mXiKaKnt
netwerk/dns/DNS.cpp
netwerk/dns/DNS.h
netwerk/dns/GetAddrInfo.cpp
netwerk/dns/nsDNSService2.cpp
netwerk/dns/nsHostResolver.cpp
--- a/netwerk/dns/DNS.cpp
+++ b/netwerk/dns/DNS.cpp
@@ -2,16 +2,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 {
@@ -286,89 +287,67 @@ 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)
 {
   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)
-  : mHostName(nullptr)
-  , mCanonicalName(nullptr)
+AddrInfo::AddrInfo(const nsACString& host, const nsACString& cname)
+  : mHostName(host)
+  , mCanonicalName(cname)
   , ttl(NO_TTL_DATA)
 {
-  Init(host, cname);
 }
 
 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,38 +129,37 @@ 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.
-  AddrInfo(const char *host, const PRAddrInfo *prAddrInfo, bool disableIPv4,
-           bool filterNameCollision, const char *cname);
+  // Creates an AddrInfo object.
+  AddrInfo(const nsACString& host, const PRAddrInfo *prAddrInfo,
+           bool disableIPv4, bool filterNameCollision,
+           const nsACString& cname);
 
-  // Creates a basic AddrInfo object (initialize only the host and the cname).
-  AddrInfo(const char *host, const char *cname);
+  // Creates a basic AddrInfo object (initialize only the host and the
+  // cname).
+  AddrInfo(const nsACString& host, const nsACString& cname);
+
   ~AddrInfo();
 
   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;
-
-private:
-  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);
-  auto ai = MakeUnique<AddrInfo>(aCanonHost, prai, disableIPv4,
-                                 filterNameCollision, canonName);
+  auto ai = MakeUnique<AddrInfo>(nsCString(aCanonHost), prai, disableIPv4,
+                                 filterNameCollision, nsCString(canonName));
   PR_FreeAddrInfo(prai);
   if (ai->mAddresses.isEmpty()) {
     return NS_ERROR_UNKNOWN_HOST;
   }
 
   aAddrInfo = Move(ai);
 
   return NS_OK;
@@ -339,18 +339,18 @@ GetAddrInfo(const char* aHost, uint16_t 
   nsresult rv = _GetAddrInfo_Portable(aHost, aAddressFamily, aFlags,
                                       aNetworkInterface, aAddrInfo);
 
 #if 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 && aAddrInfo->mCanonicalName) {
-      name = aAddrInfo->mCanonicalName;
+    if (aAddrInfo && !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/nsDNSService2.cpp
+++ b/netwerk/dns/nsDNSService2.cpp
@@ -83,28 +83,31 @@ 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);
 
-    // if the record is for an IP address literal, then the canonical
-    // host name is the IP address literal.
-    const char *cname;
     {
         MutexAutoLock lock(mHostRecord->addr_info_lock);
-        if (mHostRecord->addr_info)
-            cname = mHostRecord->addr_info->mCanonicalName ?
-                mHostRecord->addr_info->mCanonicalName :
-                mHostRecord->addr_info->mHostName;
-        else
-            cname = mHostRecord->host;
-        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 {
+            result = mHostRecord->addr_info->mCanonicalName;
+        }
     }
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsDNSRecord::GetNextAddr(uint16_t port, NetAddr *addr)
 {
     if (mDone) {
--- a/netwerk/dns/nsHostResolver.cpp
+++ b/netwerk/dns/nsHostResolver.cpp
@@ -1231,17 +1231,17 @@ nsHostResolver::PrepareRecordExpiration(
 
 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;
 
     for (NetAddrElement *element = rrset1->mAddresses.getFirst();
          element; element = element->getNext()) {
         if (LOG_ENABLED()) {
             char buf[128];
             NetAddrToString(&element->mAddress, buf, 128);