Bug 1420677 - Convert nsHostResolver.addr_info to a UniquePtr. r=valentin

MozReview-Commit-ID: 4zX73VhxiKe
This commit is contained in:
Jeff Hemphill 2017-11-25 23:55:26 -08:00
parent 6986c42dfa
commit bc64b6ad42
4 changed files with 28 additions and 29 deletions

View File

@ -251,10 +251,9 @@ _GetTTLData_Windows(const char* aHost, uint32_t* aResult, uint16_t aAddressFamil
static MOZ_ALWAYS_INLINE nsresult static MOZ_ALWAYS_INLINE nsresult
_GetAddrInfo_Portable(const char* aCanonHost, uint16_t aAddressFamily, _GetAddrInfo_Portable(const char* aCanonHost, uint16_t aAddressFamily,
uint16_t aFlags, const char* aNetworkInterface, uint16_t aFlags, const char* aNetworkInterface,
AddrInfo** aAddrInfo) UniquePtr<AddrInfo>& aAddrInfo)
{ {
MOZ_ASSERT(aCanonHost); MOZ_ASSERT(aCanonHost);
MOZ_ASSERT(aAddrInfo);
// We accept the same aFlags that nsHostResolver::ResolveHost accepts, but we // We accept the same aFlags that nsHostResolver::ResolveHost accepts, but we
// need to translate the aFlags into a form that PR_GetAddrInfoByName // need to translate the aFlags into a form that PR_GetAddrInfoByName
@ -283,14 +282,14 @@ _GetAddrInfo_Portable(const char* aCanonHost, uint16_t aAddressFamily,
} }
bool filterNameCollision = !(aFlags & nsHostResolver::RES_ALLOW_NAME_COLLISION); bool filterNameCollision = !(aFlags & nsHostResolver::RES_ALLOW_NAME_COLLISION);
nsAutoPtr<AddrInfo> ai(new AddrInfo(aCanonHost, prai, disableIPv4, auto ai = MakeUnique<AddrInfo>(aCanonHost, prai, disableIPv4,
filterNameCollision, canonName)); filterNameCollision, canonName);
PR_FreeAddrInfo(prai); PR_FreeAddrInfo(prai);
if (ai->mAddresses.isEmpty()) { if (ai->mAddresses.isEmpty()) {
return NS_ERROR_UNKNOWN_HOST; return NS_ERROR_UNKNOWN_HOST;
} }
*aAddrInfo = ai.forget(); aAddrInfo = Move(ai);
return NS_OK; return NS_OK;
} }
@ -322,9 +321,10 @@ GetAddrInfoShutdown() {
nsresult nsresult
GetAddrInfo(const char* aHost, uint16_t aAddressFamily, uint16_t aFlags, GetAddrInfo(const char* aHost, uint16_t aAddressFamily, uint16_t aFlags,
const char* aNetworkInterface, AddrInfo** aAddrInfo, bool aGetTtl) const char* aNetworkInterface,
UniquePtr<AddrInfo>& aAddrInfo, bool aGetTtl)
{ {
if (NS_WARN_IF(!aHost) || NS_WARN_IF(!aAddrInfo)) { if (NS_WARN_IF(!aHost)) {
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
} }
@ -335,7 +335,7 @@ GetAddrInfo(const char* aHost, uint16_t aAddressFamily, uint16_t aFlags,
} }
#endif #endif
*aAddrInfo = nullptr; aAddrInfo = nullptr;
nsresult rv = _GetAddrInfo_Portable(aHost, aAddressFamily, aFlags, nsresult rv = _GetAddrInfo_Portable(aHost, aAddressFamily, aFlags,
aNetworkInterface, aAddrInfo); aNetworkInterface, aAddrInfo);
@ -344,8 +344,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 // Figure out the canonical name, or if that fails, just use the host name
// we have. // we have.
const char *name = nullptr; const char *name = nullptr;
if (*aAddrInfo != nullptr && (*aAddrInfo)->mCanonicalName) { if (aAddrInfo && aAddrInfo->mCanonicalName) {
name = (*aAddrInfo)->mCanonicalName; name = aAddrInfo->mCanonicalName;
} else { } else {
name = aHost; name = aHost;
} }
@ -354,7 +354,7 @@ GetAddrInfo(const char* aHost, uint16_t aAddressFamily, uint16_t aFlags,
uint32_t ttl = 0; uint32_t ttl = 0;
nsresult ttlRv = _GetTTLData_Windows(name, &ttl, aAddressFamily); nsresult ttlRv = _GetTTLData_Windows(name, &ttl, aAddressFamily);
if (NS_SUCCEEDED(ttlRv)) { if (NS_SUCCEEDED(ttlRv)) {
(*aAddrInfo)->ttl = ttl; aAddrInfo->ttl = ttl;
LOG("Got TTL %u for %s (name = %s).", ttl, aHost, name); LOG("Got TTL %u for %s (name = %s).", ttl, aHost, name);
} else { } else {
LOG_WARNING("Could not get TTL for %s (cname = %s).", aHost, name); LOG_WARNING("Could not get TTL for %s (cname = %s).", aHost, name);

View File

@ -40,7 +40,8 @@ class AddrInfo;
*/ */
nsresult nsresult
GetAddrInfo(const char* aHost, uint16_t aAddressFamily, uint16_t aFlags, GetAddrInfo(const char* aHost, uint16_t aAddressFamily, uint16_t aFlags,
const char* aNetworkInterface, AddrInfo** aAddrInfo, bool aGetTtl); const char* aNetworkInterface,
UniquePtr<AddrInfo>& aAddrInfo, bool aGetTtl);
/** /**
* Initialize the GetAddrInfo module. * Initialize the GetAddrInfo module.

View File

@ -231,7 +231,6 @@ nsHostRecord::CopyExpirationTimesAndFlagsFrom(const nsHostRecord *aFromHostRecor
nsHostRecord::~nsHostRecord() nsHostRecord::~nsHostRecord()
{ {
Telemetry::Accumulate(Telemetry::DNS_BLACKLIST_COUNT, mBlacklistedCount); Telemetry::Accumulate(Telemetry::DNS_BLACKLIST_COUNT, mBlacklistedCount);
delete addr_info;
} }
bool bool
@ -599,7 +598,8 @@ nsHostResolver::ClearPendingQueue(PRCList *aPendingQ)
while (node != aPendingQ) { while (node != aPendingQ) {
nsHostRecord *rec = static_cast<nsHostRecord *>(node); nsHostRecord *rec = static_cast<nsHostRecord *>(node);
node = node->next; node = node->next;
OnLookupComplete(rec, NS_ERROR_ABORT, nullptr); OnLookupComplete(rec, NS_ERROR_ABORT,
mozilla::UniquePtr<AddrInfo>(nullptr));
} }
} }
} }
@ -880,7 +880,7 @@ nsHostResolver::ResolveHost(const char *host,
if ((af == addrIter->mAddress.inet.family) && if ((af == addrIter->mAddress.inet.family) &&
!unspecHe->rec->Blacklisted(&addrIter->mAddress)) { !unspecHe->rec->Blacklisted(&addrIter->mAddress)) {
if (!he->rec->addr_info) { if (!he->rec->addr_info) {
he->rec->addr_info = new AddrInfo( he->rec->addr_info = mozilla::MakeUnique<AddrInfo>(
unspecHe->rec->addr_info->mHostName, unspecHe->rec->addr_info->mHostName,
unspecHe->rec->addr_info->mCanonicalName); unspecHe->rec->addr_info->mCanonicalName);
he->rec->CopyExpirationTimesAndFlagsFrom(unspecHe->rec); he->rec->CopyExpirationTimesAndFlagsFrom(unspecHe->rec);
@ -1282,7 +1282,8 @@ different_rrset(AddrInfo *rrset1, AddrInfo *rrset2)
// returns LOOKUP_RESOLVEAGAIN, but only if 'status' is not NS_ERROR_ABORT. // returns LOOKUP_RESOLVEAGAIN, but only if 'status' is not NS_ERROR_ABORT.
// takes ownership of AddrInfo parameter // takes ownership of AddrInfo parameter
nsHostResolver::LookupStatus nsHostResolver::LookupStatus
nsHostResolver::OnLookupComplete(nsHostRecord* rec, nsresult status, AddrInfo* newRRSet) nsHostResolver::OnLookupComplete(nsHostRecord* rec, nsresult status,
mozilla::UniquePtr<AddrInfo>&& newRRSet)
{ {
// get the list of pending callbacks for this lookup, and notify // get the list of pending callbacks for this lookup, and notify
// them that the lookup is complete. // them that the lookup is complete.
@ -1294,7 +1295,6 @@ nsHostResolver::OnLookupComplete(nsHostRecord* rec, nsresult status, AddrInfo* n
if (rec->mResolveAgain && (status != NS_ERROR_ABORT)) { if (rec->mResolveAgain && (status != NS_ERROR_ABORT)) {
LOG(("nsHostResolver record %p resolve again due to flushcache\n", rec)); LOG(("nsHostResolver record %p resolve again due to flushcache\n", rec));
rec->mResolveAgain = false; rec->mResolveAgain = false;
delete newRRSet;
return LOOKUP_RESOLVEAGAIN; return LOOKUP_RESOLVEAGAIN;
} }
@ -1303,22 +1303,18 @@ nsHostResolver::OnLookupComplete(nsHostRecord* rec, nsresult status, AddrInfo* n
// update record fields. We might have a rec->addr_info already if a // update record fields. We might have a rec->addr_info already if a
// previous lookup result expired and we're reresolving it.. // previous lookup result expired and we're reresolving it..
AddrInfo *old_addr_info;
{ {
MutexAutoLock lock(rec->addr_info_lock); MutexAutoLock lock(rec->addr_info_lock);
if (different_rrset(rec->addr_info, newRRSet)) { if (different_rrset(rec->addr_info.get(), newRRSet.get())) {
LOG(("nsHostResolver record %p new gencnt\n", rec)); LOG(("nsHostResolver record %p new gencnt\n", rec));
old_addr_info = rec->addr_info; rec->addr_info = Move(newRRSet);
rec->addr_info = newRRSet;
rec->addr_info_gencnt++; rec->addr_info_gencnt++;
} else { } else {
if (rec->addr_info && newRRSet) { if (rec->addr_info && newRRSet) {
rec->addr_info->ttl = newRRSet->ttl; rec->addr_info->ttl = newRRSet->ttl;
} }
old_addr_info = newRRSet;
} }
} }
delete old_addr_info;
rec->negative = !rec->addr_info; rec->negative = !rec->addr_info;
PrepareRecordExpiration(rec); PrepareRecordExpiration(rec);
@ -1468,7 +1464,7 @@ nsHostResolver::ThreadFunc(void *arg)
#endif #endif
nsHostResolver *resolver = (nsHostResolver *)arg; nsHostResolver *resolver = (nsHostResolver *)arg;
nsHostRecord *rec = nullptr; nsHostRecord *rec = nullptr;
AddrInfo *ai = nullptr; mozilla::UniquePtr<AddrInfo> ai = nullptr;
while (rec || resolver->GetHostToLookup(&rec)) { while (rec || resolver->GetHostToLookup(&rec)) {
LOG(("DNS lookup thread - Calling getaddrinfo for host [%s%s%s].\n", LOG(("DNS lookup thread - Calling getaddrinfo for host [%s%s%s].\n",
@ -1482,10 +1478,10 @@ nsHostResolver::ThreadFunc(void *arg)
#endif #endif
nsresult status = GetAddrInfo(rec->host, rec->af, rec->flags, rec->netInterface, nsresult status = GetAddrInfo(rec->host, rec->af, rec->flags, rec->netInterface,
&ai, getTtl); ai, getTtl);
#if defined(RES_RETRY_ON_FAILURE) #if defined(RES_RETRY_ON_FAILURE)
if (NS_FAILED(status) && rs.Reset()) { if (NS_FAILED(status) && rs.Reset()) {
status = GetAddrInfo(rec->host, rec->af, rec->flags, rec->netInterface, &ai, status = GetAddrInfo(rec->host, rec->af, rec->flags, rec->netInterface, ai,
getTtl); getTtl);
} }
#endif #endif
@ -1521,7 +1517,8 @@ nsHostResolver::ThreadFunc(void *arg)
LOG_HOST(rec->host, rec->netInterface), LOG_HOST(rec->host, rec->netInterface),
ai ? "success" : "failure: unknown host")); ai ? "success" : "failure: unknown host"));
if (LOOKUP_RESOLVEAGAIN == resolver->OnLookupComplete(rec, status, ai)) { if (LOOKUP_RESOLVEAGAIN == resolver->OnLookupComplete(rec, status,
mozilla::Move(ai))) {
// leave 'rec' assigned and loop to make a renewed host resolve // leave 'rec' assigned and loop to make a renewed host resolve
LOG(("DNS lookup thread - Re-resolving host [%s%s%s].\n", LOG(("DNS lookup thread - Re-resolving host [%s%s%s].\n",
LOG_HOST(rec->host, rec->netInterface))); LOG_HOST(rec->host, rec->netInterface)));

View File

@ -75,7 +75,7 @@ public:
*/ */
Mutex addr_info_lock; Mutex addr_info_lock;
int addr_info_gencnt; /* generation count of |addr_info| */ int addr_info_gencnt; /* generation count of |addr_info| */
mozilla::net::AddrInfo *addr_info; mozilla::UniquePtr<mozilla::net::AddrInfo> addr_info;
mozilla::UniquePtr<mozilla::net::NetAddr> addr; mozilla::UniquePtr<mozilla::net::NetAddr> addr;
bool negative; /* True if this record is a cache of a failed lookup. bool negative; /* True if this record is a cache of a failed lookup.
Negative cache entries are valid just like any other Negative cache entries are valid just like any other
@ -318,7 +318,8 @@ private:
LOOKUP_RESOLVEAGAIN, LOOKUP_RESOLVEAGAIN,
}; };
LookupStatus OnLookupComplete(nsHostRecord *, nsresult, mozilla::net::AddrInfo *); LookupStatus OnLookupComplete(nsHostRecord *, nsresult,
mozilla::UniquePtr<mozilla::net::AddrInfo>&&);
void DeQueue(PRCList &aQ, nsHostRecord **aResult); void DeQueue(PRCList &aQ, nsHostRecord **aResult);
void ClearPendingQueue(PRCList *aPendingQueue); void ClearPendingQueue(PRCList *aPendingQueue);
nsresult ConditionallyCreateThread(nsHostRecord *rec); nsresult ConditionallyCreateThread(nsHostRecord *rec);