Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 36e5100

Browse filesBrowse files
cjihrigtargos
authored andcommitted
lib: support ranges in validateInt32()
This commit adds minimum and maximum value checks to the validateInt32() validator. PR-URL: #20588 Fixes: #20498 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: Weijia Wang <starkwang@126.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Backport-PR-URL: #21172
1 parent 2ffb9d6 commit 36e5100
Copy full SHA for 36e5100

File tree

Expand file treeCollapse file tree

1 file changed

+6
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+6
-2
lines changed
Open diff view settings
Collapse file

‎lib/internal/validators.js‎

Copy file name to clipboardExpand all lines: lib/internal/validators.js
+6-2Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,22 @@ function validateInteger(value, name) {
6363
}
6464

6565
function validateInt32(value, name, min = -2147483648, max = 2147483647) {
66+
// The defaults for min and max correspond to the limits of 32-bit integers.
6667
if (!isInt32(value)) {
6768
let err;
6869
if (typeof value !== 'number') {
6970
err = new ERR_INVALID_ARG_TYPE(name, 'number', value);
7071
} else if (!Number.isInteger(value)) {
7172
err = new ERR_OUT_OF_RANGE(name, 'an integer', value);
7273
} else {
73-
// 2 ** 31 === 2147483648
74-
err = new ERR_OUT_OF_RANGE(name, '> -2147483649 && < 2147483648', value);
74+
err = new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
7575
}
7676
Error.captureStackTrace(err, validateInt32);
7777
throw err;
78+
} else if (value < min || value > max) {
79+
const err = new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value);
80+
Error.captureStackTrace(err, validateInt32);
81+
throw err;
7882
}
7983
}
8084

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.