Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ const validatePort = hideStackFrames((port, name = 'Port', allowZero = true) =>
(typeof port === 'string' && StringPrototypeTrim(port).length === 0) ||
+port !== (+port >>> 0) ||
port > 0xFFFF ||
(port === 0 && !allowZero)) {
(+port === 0 && !allowZero)) {
throw new ERR_SOCKET_BAD_PORT(name, port, allowZero);
}
return port | 0;
Expand Down
14 changes: 14 additions & 0 deletions test/parallel/test-internal-validators-validateport.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,17 @@ for (let n = 0; n <= 0xFFFF; n++) {
].forEach((i) => assert.throws(() => validatePort(i), {
code: 'ERR_SOCKET_BAD_PORT'
}));

// When allowZero is false, every form of zero must be rejected, including
// the string forms that coerce to 0. Refs: the zero check must coerce the
// value the same way the rest of the validation does (`+port`).
[
0, '0', ' 0 ', '00', '0x0', '0o0', '0b0',
].forEach((i) => assert.throws(() => validatePort(i, 'Port', false), {
code: 'ERR_SOCKET_BAD_PORT'
}));

// With allowZero left at its default (true), those same values are accepted.
[
0, '0', ' 0 ', '00', '0x0', '0o0', '0b0',
].forEach((i) => assert.strictEqual(validatePort(i), 0));
Loading