Bug 1950136 - add guards for res_ninit and res_nclose. r=necko-reviewers,kershaw

Differential Revision: https://phabricator.services.mozilla.com/D244737
This commit is contained in:
smayya
2025-04-16 09:29:50 +00:00
parent 7b552a7985
commit ffc5d04c81

View File

@@ -9,6 +9,7 @@
#include "mozilla/net/DNSPacket.h"
#include "nsIDNSService.h"
#include "mozilla/Maybe.h"
#include "mozilla/Mutex.h"
#include "mozilla/StaticPrefs_network.h"
#include "mozilla/ThreadLocal.h"
@@ -22,6 +23,7 @@ namespace mozilla::net {
#if defined(HAVE_RES_NINIT)
MOZ_THREAD_LOCAL(struct __res_state*) sThreadRes;
mozilla::StaticMutex sMutex MOZ_UNANNOTATED;
#endif
#define LOG(msg, ...) \
@@ -44,10 +46,13 @@ nsresult ResolveHTTPSRecordImpl(const nsACString& aHost,
if (!sThreadRes.get()) {
UniquePtr<struct __res_state> resState(new struct __res_state);
memset(resState.get(), 0, sizeof(struct __res_state));
{
StaticMutexAutoLock lock(sMutex);
if (int ret = res_ninit(resState.get())) {
LOG("res_ninit failed: %d", ret);
return NS_ERROR_UNKNOWN_HOST;
}
}
sThreadRes.set(resState.release());
}
#endif
@@ -90,7 +95,10 @@ void DNSThreadShutdown() {
}
sThreadRes.set(nullptr);
{
StaticMutexAutoLock lock(sMutex);
res_nclose(res);
}
free(res);
#endif
}