From 948d7580ab467b143ba0d92bbe60b2dd4305175c Mon Sep 17 00:00:00 2001 From: Lazizbek Ergashev Date: Tue, 4 Aug 2026 15:50:37 +0500 Subject: [PATCH 1/2] dns: fix crash on setServers with port 0 Signed-off-by: Lazizbek Ergashev --- src/cares_wrap.cc | 8 ++++---- test/parallel/test-dns.js | 4 ++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index eb7d1079f673..e0ba3ce947d6 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -2251,13 +2251,13 @@ void SetServers(const FunctionCallbackInfo& args) { if (!elm->Get(env->context(), 1).ToLocal(&ipValue)) return; if (!elm->Get(env->context(), 2).ToLocal(&portValue)) return; - CHECK(familyValue->Int32Value(env->context()).FromJust()); CHECK(ipValue->IsString()); - CHECK(portValue->Int32Value(env->context()).FromJust()); - int fam = familyValue->Int32Value(env->context()).FromJust(); + int32_t fam; + if (!familyValue->Int32Value(env->context()).To(&fam)) return; node::Utf8Value ip(env->isolate(), ipValue); - int port = portValue->Int32Value(env->context()).FromJust(); + int32_t port; + if (!portValue->Int32Value(env->context()).To(&port)) return; if (!csv.empty()) csv += ','; diff --git a/test/parallel/test-dns.js b/test/parallel/test-dns.js index e885a700752e..2ac763ffa2d0 100644 --- a/test/parallel/test-dns.js +++ b/test/parallel/test-dns.js @@ -134,6 +134,10 @@ const portsExpected = [ dns.setServers(ports); assert.deepStrictEqual(dns.getServers(), portsExpected); +// Port 0 means "use the default port" for c-ares. +dns.setServers(['4.4.4.4:0', '[2001:4860:4860::8888]:0']); +assert.deepStrictEqual(dns.getServers(), ['4.4.4.4', '2001:4860:4860::8888']); + // Link-local IPv6 addresses require a zone index (scope id) to be usable; // c-ares drops link-local servers configured without one. dns.setServers(['[fe80::483a:5aff:fee6:1f04]%eth0']); From 42c89eefdb8c691aba57c624c998bd98301e9e89 Mon Sep 17 00:00:00 2001 From: lazerg Date: Tue, 4 Aug 2026 18:16:19 +0500 Subject: [PATCH 2/2] dns: keep type assertions in SetServers --- src/cares_wrap.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index e0ba3ce947d6..db2fb75438ee 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -2251,13 +2251,13 @@ void SetServers(const FunctionCallbackInfo& args) { if (!elm->Get(env->context(), 1).ToLocal(&ipValue)) return; if (!elm->Get(env->context(), 2).ToLocal(&portValue)) return; + CHECK(familyValue->IsInt32()); CHECK(ipValue->IsString()); + CHECK(portValue->IsInt32()); - int32_t fam; - if (!familyValue->Int32Value(env->context()).To(&fam)) return; + int32_t fam = familyValue.As()->Value(); node::Utf8Value ip(env->isolate(), ipValue); - int32_t port; - if (!portValue->Int32Value(env->context()).To(&port)) return; + int32_t port = portValue.As()->Value(); if (!csv.empty()) csv += ',';