From 0d0efc9d9244583f5dab2d7d90753ffac906cfd8 Mon Sep 17 00:00:00 2001 From: Jeff Hemphill Date: Wed, 29 Nov 2017 23:30:27 -0800 Subject: [PATCH] Bug 1417827: Convert internal char* to nsCString in DNS.h r=bagder MozReview-Commit-ID: Js1mXiKaKnt --HG-- extra : rebase_source : 55ae6349a65f20a2401aa76711b1e9c871b0c167 --- netwerk/dns/DNS.cpp | 60 +++++++++------------------------- netwerk/dns/DNS.h | 18 +++++----- netwerk/dns/GetAddrInfo.cpp | 8 ++--- netwerk/dns/TRR.cpp | 4 +-- netwerk/dns/TRRService.cpp | 6 ++-- netwerk/dns/nsDNSService2.cpp | 19 ++++++----- netwerk/dns/nsHostResolver.cpp | 4 +-- 7 files changed, 47 insertions(+), 72 deletions(-) diff --git a/netwerk/dns/DNS.cpp b/netwerk/dns/DNS.cpp index 7d524cb46263..5487686dddf0 100644 --- a/netwerk/dns/DNS.cpp +++ b/netwerk/dns/DNS.cpp @@ -9,6 +9,7 @@ #include "mozilla/Assertions.h" #include "mozilla/mozalloc.h" #include "mozilla/ArrayUtils.h" +#include "nsString.h" #include #ifdef XP_WIN @@ -293,17 +294,17 @@ NetAddrElement::NetAddrElement(const NetAddrElement& netAddr) 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 { @@ -318,35 +319,27 @@ AddrInfo::AddrInfo(const char *host, const PRAddrInfo *prAddrInfo, } 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; @@ -362,27 +355,6 @@ AddrInfo::~AddrInfo() 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(moz_xmalloc(hostlen + 1)); - memcpy(mHostName, host, hostlen + 1); - if (cname) { - size_t cnameLen = strlen(cname); - mCanonicalName = static_cast(moz_xmalloc(cnameLen + 1)); - memcpy(mCanonicalName, cname, cnameLen + 1); - } - else { - mCanonicalName = nullptr; - } } void @@ -397,8 +369,8 @@ 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; } diff --git a/netwerk/dns/DNS.h b/netwerk/dns/DNS.h index 2dee9915b52c..84e394cfa909 100644 --- a/netwerk/dns/DNS.h +++ b/netwerk/dns/DNS.h @@ -8,6 +8,7 @@ #define DNS_h_ #include "nscore.h" +#include "nsString.h" #include "prio.h" #include "prnetdb.h" #include "plstr.h" @@ -133,16 +134,16 @@ public: 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 @@ -151,8 +152,8 @@ public: 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; @@ -160,7 +161,6 @@ public: 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. diff --git a/netwerk/dns/GetAddrInfo.cpp b/netwerk/dns/GetAddrInfo.cpp index cc310790b762..e70d0b35532e 100644 --- a/netwerk/dns/GetAddrInfo.cpp +++ b/netwerk/dns/GetAddrInfo.cpp @@ -282,8 +282,8 @@ _GetAddrInfo_Portable(const char* aCanonHost, uint16_t aAddressFamily, } bool filterNameCollision = !(aFlags & nsHostResolver::RES_ALLOW_NAME_COLLISION); - nsAutoPtr ai(new AddrInfo(aCanonHost, prai, disableIPv4, - filterNameCollision, canonName)); + nsAutoPtr ai(new AddrInfo(nsCString(aCanonHost), prai, disableIPv4, + filterNameCollision, nsCString(canonName))); PR_FreeAddrInfo(prai); if (ai->mAddresses.isEmpty()) { return NS_ERROR_UNKNOWN_HOST; @@ -349,8 +349,8 @@ GetAddrInfo(const char* aHost, uint16_t aAddressFamily, uint16_t aFlags, // 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; } diff --git a/netwerk/dns/TRR.cpp b/netwerk/dns/TRR.cpp index 0f4fcd8bfa21..b86ea2046a35 100644 --- a/netwerk/dns/TRR.cpp +++ b/netwerk/dns/TRR.cpp @@ -824,7 +824,7 @@ nsresult TRR::ReturnData() { // create and populate an AddrInfo instance to pass on - nsAutoPtr ai(new AddrInfo(mHost.get(), mType)); + nsAutoPtr ai(new AddrInfo(mHost, mType)); DOHaddr *item; uint32_t ttl = AddrInfo::NO_TTL_DATA; while ((item = static_cast(mDNS.mAddresses.popFirst()))) { @@ -857,7 +857,7 @@ TRR::FailData() } // 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; diff --git a/netwerk/dns/TRRService.cpp b/netwerk/dns/TRRService.cpp index 3ec8f23e2b4a..a06a092bb5d6 100644 --- a/netwerk/dns/TRRService.cpp +++ b/netwerk/dns/TRRService.cpp @@ -589,10 +589,10 @@ TRRService::CompleteLookup(nsHostRecord *rec, nsresult status, AddrInfo *aNewRRS // 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; } diff --git a/netwerk/dns/nsDNSService2.cpp b/netwerk/dns/nsDNSService2.cpp index a663811bcc35..54384c065259 100644 --- a/netwerk/dns/nsDNSService2.cpp +++ b/netwerk/dns/nsDNSService2.cpp @@ -92,15 +92,18 @@ nsDNSRecord::GetCanonicalName(nsACString &result) 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); - } else { - // if the record is for an IP address literal, then the canonical - // host name is the IP address literal. + + // 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; } diff --git a/netwerk/dns/nsHostResolver.cpp b/netwerk/dns/nsHostResolver.cpp index c6c9801d20a7..914893cb13ae 100644 --- a/netwerk/dns/nsHostResolver.cpp +++ b/netwerk/dns/nsHostResolver.cpp @@ -1418,7 +1418,7 @@ different_rrset(AddrInfo *rrset1, AddrInfo *rrset2) return true; } - LOG(("different_rrset %s\n", rrset1->mHostName)); + LOG(("different_rrset %s\n", rrset1->mHostName.get())); nsTArray orderedSet1; nsTArray orderedSet2; @@ -1495,7 +1495,7 @@ nsHostResolver::CompleteLookup(nsHostRecord* rec, nsresult status, AddrInfo* aNe 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) {