Skip to content

Commit 27bcf33

Browse files
mawaregetsukajuanarbol
authored andcommitted
lib: refactor validateInt32 and validateUint32
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>
1 parent 27b646e commit 27bcf33

6 files changed

+20
-34
lines changed

lib/internal/dns/utils.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ const {
3838

3939
function validateTimeout(options) {
4040
const { timeout = -1 } = { ...options };
41-
validateInt32(timeout, 'options.timeout', -1, 2 ** 31 - 1);
41+
validateInt32(timeout, 'options.timeout', -1);
4242
return timeout;
4343
}
4444

4545
function validateTries(options) {
4646
const { tries = 4 } = { ...options };
47-
validateInt32(tries, 'options.tries', 1, 2 ** 31 - 1);
47+
validateInt32(tries, 'options.tries', 1);
4848
return tries;
4949
}
5050

lib/internal/validators.js

+10-15
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function parseFileMode(value, name, def) {
6565
value = NumberParseInt(value, 8);
6666
}
6767

68-
validateInt32(value, name, 0, 2 ** 32 - 1);
68+
validateUint32(value, name);
6969
return value;
7070
}
7171

@@ -86,11 +86,8 @@ const validateInt32 = hideStackFrames(
8686
if (typeof value !== 'number') {
8787
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
8888
}
89-
if (!isInt32(value)) {
90-
if (!NumberIsInteger(value)) {
91-
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
92-
}
93-
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
89+
if (!NumberIsInteger(value)) {
90+
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
9491
}
9592
if (value < min || value > max) {
9693
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
@@ -102,16 +99,14 @@ const validateUint32 = hideStackFrames((value, name, positive) => {
10299
if (typeof value !== 'number') {
103100
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
104101
}
105-
if (!isUint32(value)) {
106-
if (!NumberIsInteger(value)) {
107-
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
108-
}
109-
const min = positive ? 1 : 0;
110-
// 2 ** 32 === 4294967296
111-
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && < 4294967296`, value);
102+
if (!NumberIsInteger(value)) {
103+
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
112104
}
113-
if (positive && value === 0) {
114-
throw new ERR_OUT_OF_RANGE(name, '>= 1 && < 4294967296', value);
105+
const min = positive ? 1 : 0;
106+
// 2 ** 32 === 4294967296
107+
const max = 4_294_967_295;
108+
if (value < min || value > max) {
109+
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
115110
}
116111
});
117112

test/parallel/test-crypto-pbkdf2.js

-4
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ for (const iterations of [-1, 0]) {
6969
{
7070
code: 'ERR_OUT_OF_RANGE',
7171
name: 'RangeError',
72-
message: 'The value of "iterations" is out of range. ' +
73-
`It must be >= 1 && < 4294967296. Received ${iterations}`
7472
}
7573
);
7674
}
@@ -108,8 +106,6 @@ for (const iterations of [-1, 0]) {
108106
}, {
109107
code: 'ERR_OUT_OF_RANGE',
110108
name: 'RangeError',
111-
message: 'The value of "keylen" is out of range. It must be >= 0 && < ' +
112-
`4294967296. Received ${input === -1 ? '-1' : '4_294_967_297'}`
113109
});
114110
});
115111

test/parallel/test-file-validate-mode-flag.js

+7-6
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,28 @@ const {
1313
} = require('fs');
1414

1515
// These should throw, not crash.
16+
const invalid = 4_294_967_296;
1617

17-
assert.throws(() => open(__filename, 2176057344, common.mustNotCall()), {
18+
assert.throws(() => open(__filename, invalid, common.mustNotCall()), {
1819
code: 'ERR_OUT_OF_RANGE'
1920
});
2021

21-
assert.throws(() => open(__filename, 0, 2176057344, common.mustNotCall()), {
22+
assert.throws(() => open(__filename, 0, invalid, common.mustNotCall()), {
2223
code: 'ERR_OUT_OF_RANGE'
2324
});
2425

25-
assert.throws(() => openSync(__filename, 2176057344), {
26+
assert.throws(() => openSync(__filename, invalid), {
2627
code: 'ERR_OUT_OF_RANGE'
2728
});
2829

29-
assert.throws(() => openSync(__filename, 0, 2176057344), {
30+
assert.throws(() => openSync(__filename, 0, invalid), {
3031
code: 'ERR_OUT_OF_RANGE'
3132
});
3233

33-
assert.rejects(openPromise(__filename, 2176057344), {
34+
assert.rejects(openPromise(__filename, invalid), {
3435
code: 'ERR_OUT_OF_RANGE'
3536
});
3637

37-
assert.rejects(openPromise(__filename, 0, 2176057344), {
38+
assert.rejects(openPromise(__filename, 0, invalid), {
3839
code: 'ERR_OUT_OF_RANGE'
3940
});

test/parallel/test-process-setgroups.js

-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,6 @@ assert.throws(
2929
{
3030
code: 'ERR_OUT_OF_RANGE',
3131
name: 'RangeError',
32-
message: 'The value of "groups[1]" is out of range. ' +
33-
'It must be >= 0 && < 4294967296. Received -1'
3432
}
3533
);
3634

test/parallel/test-readline-interface.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,7 @@ function assertCursorRowsAndCols(rli, rows, cols) {
131131
input,
132132
tabSize: 0
133133
}),
134-
{
135-
message: 'The value of "tabSize" is out of range. ' +
136-
'It must be >= 1 && < 4294967296. Received 0',
137-
code: 'ERR_OUT_OF_RANGE'
138-
}
134+
{ code: 'ERR_OUT_OF_RANGE' }
139135
);
140136

141137
assert.throws(

0 commit comments

Comments
 (0)