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 <[email protected]>
Reviewed-By: Minwoo Jung <[email protected]>
Reviewed-By: Antoine du Hamel <[email protected]>
  • Loading branch information
mawaregetsuka authored May 22, 2022
1 parent b4398df commit 419669c
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 67 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 @@ -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
28 changes: 0 additions & 28 deletions test/parallel/test-crypto-keygen.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,18 +1122,6 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
}
}

function addNumericalSeparator(val) {
val = String(val);
let res = '';
let i = val.length;
const start = val[0] === '-' ? 1 : 0;
for (; i >= start + 4; i -= 3) {
res = `_${val.slice(i - 3, i)}${res}`;
}
return `${val.slice(0, i)}${res}`;
}


// Test RSA parameters.
{
// Test invalid modulus lengths. (non-number)
Expand Down Expand Up @@ -1170,10 +1158,6 @@ function addNumericalSeparator(val) {
}, common.mustNotCall()), {
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
message:
'The value of "options.modulusLength" is out of range. ' +
'It must be >= 0 && < 4294967296. ' +
`Received ${addNumericalSeparator(modulusLength)}`
});
}

Expand Down Expand Up @@ -1214,10 +1198,6 @@ function addNumericalSeparator(val) {
}, common.mustNotCall()), {
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
message:
'The value of "options.publicExponent" is out of range. ' +
'It must be >= 0 && < 4294967296. ' +
`Received ${addNumericalSeparator(publicExponent)}`
});
}
}
Expand All @@ -1244,10 +1224,6 @@ function addNumericalSeparator(val) {
}, common.mustNotCall()), {
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
message:
'The value of "options.modulusLength" is out of range. ' +
'It must be an integer. ' +
`Received ${inspect(modulusLength)}`
});
}

Expand All @@ -1258,10 +1234,6 @@ function addNumericalSeparator(val) {
}, common.mustNotCall()), {
name: 'RangeError',
code: 'ERR_OUT_OF_RANGE',
message:
'The value of "options.modulusLength" is out of range. ' +
'It must be >= 0 && < 4294967296. ' +
`Received ${addNumericalSeparator(modulusLength)}`
});
}

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
6 changes: 1 addition & 5 deletions test/parallel/test-readline-promises-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,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 419669c

Please sign in to comment.