From 0e2a47510dddadc05d0057491e970f481dd3858c Mon Sep 17 00:00:00 2001 From: Voltrex Date: Tue, 29 Jun 2021 05:20:00 +0430 Subject: [PATCH 1/3] dgram: use missing validator We don't mention a value being "falsy" in validation, its better to use a validator here to keep consistency. fixup! dgram: use missing validator Updated the validation to be more strict. Co-authored-by: Antoine du Hamel --- lib/dgram.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/dgram.js b/lib/dgram.js index 4630be4dff8c5c..2fe71da97111b4 100644 --- a/lib/dgram.js +++ b/lib/dgram.js @@ -636,8 +636,8 @@ Socket.prototype.send = function(buffer, if (typeof address === 'function') { callback = address; address = undefined; - } else if (address && typeof address !== 'string') { - throw new ERR_INVALID_ARG_TYPE('address', ['string', 'falsy'], address); + } else if (address != null) { + validateString(address, 'address'); } healthCheck(this); From a67307ac9726115c60843a6f874ef724678da7e3 Mon Sep 17 00:00:00 2001 From: Voltrex Date: Tue, 29 Jun 2021 06:44:24 +0430 Subject: [PATCH 2/3] test: fix dgram test - Fixed the dgram expected error message. - Removed invalid addresses in the `dgram`'s `Socket.prototype.send()` tests. - Lowered the amount of expected anonymous function calls. - Added a few missing invalid address test cases and made the valid address test cases non-blocking. - Added missing falsy values for the invalid address test check list. test: remove invalid addresses Removed invalid addresses in the `dgram`'s `Socket.prototype.send()` tests. test: lower expected function calls Lowered the amount of expected anonymous function calls. test: add missing and non-blocking cases Added a few missing invalid address test cases and made the valid address test cases non-blocking. test: omit invalid test case Omitted the invalid anonymous function test case as the `address` parameter can also be a function. test: add missing falsy values Added missing falsy values for the invalid address test check list. --- .../parallel/test-dgram-send-address-types.js | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-dgram-send-address-types.js b/test/parallel/test-dgram-send-address-types.js index a88ef088dcf8a9..a31e53f9038fbb 100644 --- a/test/parallel/test-dgram-send-address-types.js +++ b/test/parallel/test-dgram-send-address-types.js @@ -5,15 +5,17 @@ const dgram = require('dgram'); const buf = Buffer.from('test'); +const defaultCases = ['', null, undefined]; + const onMessage = common.mustSucceed((bytes) => { assert.strictEqual(bytes, buf.length); -}, 6); +}, defaultCases.length + 1); const client = dgram.createSocket('udp4').bind(0, () => { const port = client.address().port; // Check valid addresses - [false, '', null, 0, undefined].forEach((address) => { + defaultCases.forEach((address) => { client.send(buf, port, address, onMessage); }); @@ -21,11 +23,21 @@ const client = dgram.createSocket('udp4').bind(0, () => { client.send(buf, port, onMessage); // Check invalid addresses - [[], 1, true].forEach((invalidInput) => { + [ + [], + 0, + 1, + true, + false, + 0n, + 1n, + {}, + Symbol(), + ].forEach((invalidInput) => { const expectedError = { code: 'ERR_INVALID_ARG_TYPE', name: 'TypeError', - message: 'The "address" argument must be of type string or falsy.' + + message: 'The "address" argument must be of type string.' + `${common.invalidArgTypeHelper(invalidInput)}` }; assert.throws(() => client.send(buf, port, invalidInput), expectedError); From 17cf05b69cd511dd09897e86598b24b7726aad71 Mon Sep 17 00:00:00 2001 From: Voltrex Date: Wed, 30 Jun 2021 17:43:25 +0430 Subject: [PATCH 3/3] doc: add change entry Added a change entry for the stricter validation of the `address` parameter in `Socket.prototype.send()`. doc: use nullish instead of falsy Used nullish instead of falsy to point at that the parameter now only accepts a `string`, `null` or `undefined`. --- doc/api/dgram.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/doc/api/dgram.md b/doc/api/dgram.md index 365c30c9ae64a7..87456418623d3d 100644 --- a/doc/api/dgram.md +++ b/doc/api/dgram.md @@ -468,6 +468,10 @@ if the socket is not connected.