Skip to content

Commit

Permalink
lib: refactor validateInt32 and validateUint32
Browse files Browse the repository at this point in the history
PR-URL: #43071
Reviewed-By: Paolo Insogna <paolo@cowtech.it>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
mawaregetsuka authored and juanarbol committed May 31, 2022
1 parent 27b646e commit 27bcf33
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 34 deletions.
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 @@ -65,7 +65,7 @@ function parseFileMode(value, name, def) {
value = NumberParseInt(value, 8);
}

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

Expand All @@ -86,11 +86,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 @@ -102,16 +99,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
4 changes: 0 additions & 4 deletions test/parallel/test-crypto-pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ 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}`
}
);
}
Expand Down Expand Up @@ -108,8 +106,6 @@ 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'}`
});
});

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 = 4_294_967_296;

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: 0 additions & 2 deletions test/parallel/test-process-setgroups.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ 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'
}
);

Expand Down
6 changes: 1 addition & 5 deletions test/parallel/test-readline-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,7 @@ function assertCursorRowsAndCols(rli, rows, cols) {
input,
tabSize: 0
}),
{
message: 'The value of "tabSize" is out of range. ' +
'It must be >= 1 && < 4294967296. Received 0',
code: 'ERR_OUT_OF_RANGE'
}
{ code: 'ERR_OUT_OF_RANGE' }
);

assert.throws(
Expand Down

0 comments on commit 27bcf33

Please sign in to comment.