Skip to content

Commit 3796b48

Browse files
deepview-autofixclaudeChALkeR
authored
lib: fix validatePort to reject string '0' when allowZero is false
The `port === 0 && !allowZero` check used strict equality, so the string `'0'` (and other numeric-zero string forms like `'0x0'`) slipped through because `'0' === 0` is `false`. As a result, `validatePort('0', name, false)` returned `0`, contradicting the `allowZero=false` contract and letting callers such as `dgram.Socket#connect('0', ...)` proceed past the validator and fail deep in the stack with `EADDRNOTAVAIL` while leaking the handle. Compare the numeric coercion (`+port === 0`) so all numeric-zero forms are rejected consistently. Co-Authored-By: Claude <[email protected]> Co-Authored-By: DeepView Autofix <[email protected]> Co-Authored-By: Nikita Skovoroda <[email protected]> Signed-off-by: Nikita Skovoroda <[email protected]>
1 parent 14e16db commit 3796b48

3 files changed

Lines changed: 9 additions & 2 deletions

File tree

lib/internal/validators.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ const validatePort = hideStackFrames((port, name = 'Port', allowZero = true) =>
428428
(typeof port === 'string' && StringPrototypeTrim(port).length === 0) ||
429429
+port !== (+port >>> 0) ||
430430
port > 0xFFFF ||
431-
(port === 0 && !allowZero)) {
431+
(+port === 0 && !allowZero)) {
432432
throw new ERR_SOCKET_BAD_PORT(name, port, allowZero);
433433
}
434434
return port | 0;

test/parallel/test-dgram-connect.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ assert.throws(() => {
5555
code: 'ERR_SOCKET_DGRAM_NOT_CONNECTED'
5656
});
5757

58-
[ 0, null, 78960, undefined ].forEach((port) => {
58+
[ 0, '0', null, 78960, undefined ].forEach((port) => {
5959
assert.throws(() => {
6060
client.connect(port);
6161
}, {

test/parallel/test-internal-validators-validateport.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,10 @@ for (let n = 0; n <= 0xFFFF; n++) {
2121
].forEach((i) => assert.throws(() => validatePort(i), {
2222
code: 'ERR_SOCKET_BAD_PORT'
2323
}));
24+
25+
// When `allowZero` is false, both the number `0` and the string `'0'` (and
26+
// other numeric-zero string forms) must be rejected.
27+
[0, '0', '0x0', '0o0', '0b0'].forEach((i) => assert.throws(
28+
() => validatePort(i, 'Port', false),
29+
{ code: 'ERR_SOCKET_BAD_PORT' },
30+
));

0 commit comments

Comments
 (0)