Skip to content

Commit 701d404

Browse files
mawaregetsukabengl
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 d9fb25c commit 701d404

8 files changed

+21
-67
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
@@ -64,7 +64,7 @@ function parseFileMode(value, name, def) {
6464
value = NumberParseInt(value, 8);
6565
}
6666

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

@@ -85,11 +85,8 @@ const validateInt32 = hideStackFrames(
8585
if (typeof value !== 'number') {
8686
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
8787
}
88-
if (!isInt32(value)) {
89-
if (!NumberIsInteger(value)) {
90-
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
91-
}
92-
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
88+
if (!NumberIsInteger(value)) {
89+
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
9390
}
9491
if (value < min || value > max) {
9592
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
@@ -101,16 +98,14 @@ const validateUint32 = hideStackFrames((value, name, positive) => {
10198
if (typeof value !== 'number') {
10299
throw new ERR_INVALID_ARG_TYPE(name, 'number', value);
103100
}
104-
if (!isUint32(value)) {
105-
if (!NumberIsInteger(value)) {
106-
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
107-
}
108-
const min = positive ? 1 : 0;
109-
// 2 ** 32 === 4294967296
110-
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && < 4294967296`, value);
101+
if (!NumberIsInteger(value)) {
102+
throw new ERR_OUT_OF_RANGE(name, 'an integer', value);
111103
}
112-
if (positive && value === 0) {
113-
throw new ERR_OUT_OF_RANGE(name, '>= 1 && < 4294967296', value);
104+
const min = positive ? 1 : 0;
105+
// 2 ** 32 === 4294967296
106+
const max = 4_294_967_295;
107+
if (value < min || value > max) {
108+
throw new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
114109
}
115110
});
116111

test/parallel/test-crypto-keygen.js

-28
Original file line numberDiff line numberDiff line change
@@ -1122,18 +1122,6 @@ const sec1EncExp = (cipher) => getRegExpForPEM('EC PRIVATE KEY', cipher);
11221122
}
11231123
}
11241124

1125-
function addNumericalSeparator(val) {
1126-
val = String(val);
1127-
let res = '';
1128-
let i = val.length;
1129-
const start = val[0] === '-' ? 1 : 0;
1130-
for (; i >= start + 4; i -= 3) {
1131-
res = `_${val.slice(i - 3, i)}${res}`;
1132-
}
1133-
return `${val.slice(0, i)}${res}`;
1134-
}
1135-
1136-
11371125
// Test RSA parameters.
11381126
{
11391127
// Test invalid modulus lengths. (non-number)
@@ -1170,10 +1158,6 @@ function addNumericalSeparator(val) {
11701158
}, common.mustNotCall()), {
11711159
name: 'RangeError',
11721160
code: 'ERR_OUT_OF_RANGE',
1173-
message:
1174-
'The value of "options.modulusLength" is out of range. ' +
1175-
'It must be >= 0 && < 4294967296. ' +
1176-
`Received ${addNumericalSeparator(modulusLength)}`
11771161
});
11781162
}
11791163

@@ -1214,10 +1198,6 @@ function addNumericalSeparator(val) {
12141198
}, common.mustNotCall()), {
12151199
name: 'RangeError',
12161200
code: 'ERR_OUT_OF_RANGE',
1217-
message:
1218-
'The value of "options.publicExponent" is out of range. ' +
1219-
'It must be >= 0 && < 4294967296. ' +
1220-
`Received ${addNumericalSeparator(publicExponent)}`
12211201
});
12221202
}
12231203
}
@@ -1244,10 +1224,6 @@ function addNumericalSeparator(val) {
12441224
}, common.mustNotCall()), {
12451225
name: 'RangeError',
12461226
code: 'ERR_OUT_OF_RANGE',
1247-
message:
1248-
'The value of "options.modulusLength" is out of range. ' +
1249-
'It must be an integer. ' +
1250-
`Received ${inspect(modulusLength)}`
12511227
});
12521228
}
12531229

@@ -1258,10 +1234,6 @@ function addNumericalSeparator(val) {
12581234
}, common.mustNotCall()), {
12591235
name: 'RangeError',
12601236
code: 'ERR_OUT_OF_RANGE',
1261-
message:
1262-
'The value of "options.modulusLength" is out of range. ' +
1263-
'It must be >= 0 && < 4294967296. ' +
1264-
`Received ${addNumericalSeparator(modulusLength)}`
12651237
});
12661238
}
12671239

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(

test/parallel/test-readline-promises-interface.js

+1-5
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,7 @@ function assertCursorRowsAndCols(rli, rows, cols) {
121121
input,
122122
tabSize: 0
123123
}),
124-
{
125-
message: 'The value of "tabSize" is out of range. ' +
126-
'It must be >= 1 && < 4294967296. Received 0',
127-
code: 'ERR_OUT_OF_RANGE'
128-
}
124+
{ code: 'ERR_OUT_OF_RANGE' }
129125
);
130126

131127
assert.throws(

0 commit comments

Comments
 (0)