Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: optimize validators #43071

Merged
merged 7 commits into from
May 22, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 2 additions & 2 deletions lib/internal/dns/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ const {

function validateTimeout(options) {
const { timeout = -1 } = { ...options };
validateInt32(timeout, 'options.timeout', -1, 2 ** 31 - 1);
validateInt32(timeout, 'options.timeout', -1);
return timeout;
}

function validateTries(options) {
const { tries = 4 } = { ...options };
validateInt32(tries, 'options.tries', 1, 2 ** 31 - 1);
validateInt32(tries, 'options.tries', 1);
return tries;
}

Expand Down
25 changes: 10 additions & 15 deletions lib/internal/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function parseFileMode(value, name, def) {
value = NumberParseInt(value, 8);
}

validateInt32(value, name, 0, 2 ** 32 - 1);
validateUint32(value, name);
return value;
}

Expand All @@ -85,11 +85,8 @@ const validateInt32 = hideStackFrames(
if (typeof value !== 'number') {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}
if (!isInt32(value)) {
if (!NumberIsInteger(value)) {
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
}
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
if (!NumberIsInteger(value)) {
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
}
if (value < min || value > max) {
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
Expand All @@ -101,16 +98,14 @@ const validateUint32 = hideStackFrames((value, name, positive) => {
if (typeof value !== 'number') {
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
}
if (!isUint32(value)) {
if (!NumberIsInteger(value)) {
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
}
const min = positive ? 1 : 0;
// 2 ** 32 === 4294967296
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && < 4294967296`, value);
if (!NumberIsInteger(value)) {
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
}
if (positive && value === 0) {
throw new ERR_OUT_OF_RANGE(name, '>= 1 && < 4294967296', value);
const min = positive ? 1 : 0;
// 2 ** 32 === 4294967296
const max = 4_294_967_295;
if (value < min || value > max) {
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
}
});

Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-crypto-keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -1172,7 +1172,7 @@ function addNumericalSeparator(val) {
code: 'ERR_OUT_OF_RANGE',
message:
'The value of "options.modulusLength" is out of range. ' +
'It must be >= 0 && < 4294967296. ' +
'It must be >= 0 && <= 4294967295. ' +
`Received ${addNumericalSeparator(modulusLength)}`
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
});
}
Expand Down Expand Up @@ -1216,7 +1216,7 @@ function addNumericalSeparator(val) {
code: 'ERR_OUT_OF_RANGE',
message:
'The value of "options.publicExponent" is out of range. ' +
'It must be >= 0 && < 4294967296. ' +
'It must be >= 0 && <= 4294967295. ' +
`Received ${addNumericalSeparator(publicExponent)}`
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
});
}
Expand Down Expand Up @@ -1260,7 +1260,7 @@ function addNumericalSeparator(val) {
code: 'ERR_OUT_OF_RANGE',
message:
'The value of "options.modulusLength" is out of range. ' +
'It must be >= 0 && < 4294967296. ' +
'It must be >= 0 && <= 4294967295. ' +
`Received ${addNumericalSeparator(modulusLength)}`
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
});
}
Expand Down
6 changes: 3 additions & 3 deletions test/parallel/test-crypto-pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ for (const iterations of [-1, 0]) {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "iterations" is out of range. ' +
`It must be >= 1 && < 4294967296. Received ${iterations}`
`It must be >= 1 && <= 4294967295. Received ${iterations}`
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
}
);
}
Expand Down Expand Up @@ -108,8 +108,8 @@ for (const iterations of [-1, 0]) {
}, {
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "keylen" is out of range. It must be >= 0 && < ' +
`4294967296. Received ${input === -1 ? '-1' : '4_294_967_297'}`
message: 'The value of "keylen" is out of range. It must be >= 0 && <=' +
` 4294967295. Received ${input === -1 ? '-1' : '4_294_967_297'}`
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
});
});

Expand Down
13 changes: 7 additions & 6 deletions test/parallel/test-file-validate-mode-flag.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,28 @@ const {
} = require('fs');

// These should throw, not crash.
const invalid = 4294967296;
mawaregetsuka marked this conversation as resolved.
Show resolved Hide resolved

assert.throws(() => open(__filename, 2176057344, common.mustNotCall()), {
assert.throws(() => open(__filename, invalid, common.mustNotCall()), {
code: 'ERR_OUT_OF_RANGE'
});

assert.throws(() => open(__filename, 0, 2176057344, common.mustNotCall()), {
assert.throws(() => open(__filename, 0, invalid, common.mustNotCall()), {
code: 'ERR_OUT_OF_RANGE'
});

assert.throws(() => openSync(__filename, 2176057344), {
assert.throws(() => openSync(__filename, invalid), {
code: 'ERR_OUT_OF_RANGE'
});

assert.throws(() => openSync(__filename, 0, 2176057344), {
assert.throws(() => openSync(__filename, 0, invalid), {
code: 'ERR_OUT_OF_RANGE'
});

assert.rejects(openPromise(__filename, 2176057344), {
assert.rejects(openPromise(__filename, invalid), {
code: 'ERR_OUT_OF_RANGE'
});

assert.rejects(openPromise(__filename, 0, 2176057344), {
assert.rejects(openPromise(__filename, 0, invalid), {
code: 'ERR_OUT_OF_RANGE'
});
2 changes: 1 addition & 1 deletion test/parallel/test-process-setgroups.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ assert.throws(
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "groups[1]" is out of range. ' +
'It must be >= 0 && < 4294967296. Received -1'
'It must be >= 0 && <= 4294967295. Received -1'
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
}
);

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function assertCursorRowsAndCols(rli, rows, cols) {
}),
{
message: 'The value of "tabSize" is out of range. ' +
'It must be >= 1 && < 4294967296. Received 0',
'It must be >= 1 && <= 4294967295. Received 0',
code: 'ERR_OUT_OF_RANGE'
}
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-readline-promises-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function assertCursorRowsAndCols(rli, rows, cols) {
}),
{
message: 'The value of "tabSize" is out of range. ' +
'It must be >= 1 && < 4294967296. Received 0',
'It must be >= 1 && <= 4294967295. Received 0',
code: 'ERR_OUT_OF_RANGE'
}
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
);
Expand Down