diff --git a/lib/dns.js b/lib/dns.js index 9dffad8bf40..715351d8d29 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -158,7 +158,8 @@ function lookup(hostname, options, callback) { validateFunction(callback, 'callback'); validateOneOf(options, 'family', validFamilies); - family = options; + // Coerce -0 to +0. + family = options + 0; } else if (options !== undefined && typeof options !== 'object') { validateFunction(arguments.length === 2 ? options : callback, 'callback'); throw new ERR_INVALID_ARG_TYPE('options', ['integer', 'object'], options); @@ -180,7 +181,8 @@ function lookup(hostname, options, callback) { break; default: validateOneOf(options.family, 'options.family', validFamilies); - family = options.family; + // Coerce -0 to +0. + family = options.family + 0; break; } } @@ -280,7 +282,8 @@ function lookupService(address, port, callback) { validateFunction(callback, 'callback'); - port = +port; + // Coerce -0 to +0. + port = +port + 0; const req = new GetNameInfoReqWrap(); req.callback = callback; diff --git a/lib/internal/dns/promises.js b/lib/internal/dns/promises.js index dcaae5346bb..899e8c6672d 100644 --- a/lib/internal/dns/promises.js +++ b/lib/internal/dns/promises.js @@ -207,7 +207,8 @@ function lookup(hostname, options) { if (typeof options === 'number') { validateOneOf(options, 'family', validFamilies); - family = options; + // Coerce -0 to +0. + family = options + 0; } else if (options !== undefined && typeof options !== 'object') { throw new ERR_INVALID_ARG_TYPE('options', ['integer', 'object'], options); } else { @@ -218,7 +219,8 @@ function lookup(hostname, options) { } if (options?.family != null) { validateOneOf(options.family, 'options.family', validFamilies); - family = options.family; + // Coerce -0 to +0. + family = options.family + 0; } if (options?.all != null) { validateBoolean(options.all, 'options.all'); diff --git a/lib/internal/dns/utils.js b/lib/internal/dns/utils.js index 32f8794d62a..9453b9e80f1 100644 --- a/lib/internal/dns/utils.js +++ b/lib/internal/dns/utils.js @@ -45,14 +45,18 @@ const { } = require('internal/v8/startup_snapshot'); function validateTimeout(options) { - const { timeout = -1 } = { ...options }; + let { timeout = -1 } = { ...options }; validateInt32(timeout, 'options.timeout', -1); + // Coerce -0 to +0. + timeout += 0; return timeout; } function validateMaxTimeout(options) { - const { maxTimeout = 0 } = { ...options }; + let { maxTimeout = 0 } = { ...options }; validateUint32(maxTimeout, 'options.maxTimeout'); + // Coerce -0 to +0. + maxTimeout += 0; return maxTimeout; } diff --git a/test/parallel/test-dns-negative-zero.js b/test/parallel/test-dns-negative-zero.js new file mode 100644 index 00000000000..2b07144bc06 --- /dev/null +++ b/test/parallel/test-dns-negative-zero.js @@ -0,0 +1,14 @@ +'use strict'; + +const common = require('../common'); + +const dns = require('dns'); + +dns.lookup('localhost', -0, common.mustCall()); +dns.lookup('localhost', { family: -0 }, common.mustCall()); +dns.lookupService('127.0.0.1', -0, common.mustCall()); + +new dns.Resolver({ timeout: -0 }); +new dns.Resolver({ maxTimeout: -0 }); + +dns.promises.lookup('localhost', { family: -0 }).then(common.mustCall());