diff --git a/lib/dns.js b/lib/dns.js index 782cdfa3f7..6683eacf14 100644 --- a/lib/dns.js +++ b/lib/dns.js @@ -189,6 +189,9 @@ exports.lookupService = function(host, port, callback) { if (cares.isIP(host) === 0) throw new TypeError('host needs to be a valid IP address'); + if (typeof port !== 'number') + throw new TypeError(`port argument must be a number, got "${port}"`); + callback = makeAsync(callback); var req = new GetNameInfoReqWrap(); diff --git a/test/parallel/test-dns.js b/test/parallel/test-dns.js index 3362e8c534..04264907d3 100644 --- a/test/parallel/test-dns.js +++ b/test/parallel/test-dns.js @@ -145,3 +145,19 @@ assert.doesNotThrow(function() { hints: dns.ADDRCONFIG | dns.V4MAPPED }, noop); }); + +assert.throws(function() { + dns.lookupService('0.0.0.0'); +}, /invalid arguments/); + +assert.throws(function() { + dns.lookupService('fasdfdsaf', 0, noop); +}, /host needs to be a valid IP address/); + +assert.throws(function() { + dns.lookupService('0.0.0.0', '0', noop); +}, /port argument must be a number, got "0"/); + +assert.doesNotThrow(function() { + dns.lookupService('0.0.0.0', 0, noop); +});