diff options
author | cjihrig <cjihrig@gmail.com> | 2018-05-19 21:07:16 -0400 |
---|---|---|
committer | cjihrig <cjihrig@gmail.com> | 2018-05-19 22:23:20 -0400 |
commit | 1c1b8ae409368adee82ebe4edc090db570d21764 (patch) | |
tree | 8c4baa76ddf1ba7995cf936550b26322dfcfaa89 /lib/internal/validators.js | |
parent | a7fa0dba88be73cc7210c21117c08281b0b45205 (diff) | |
download | android-node-v8-1c1b8ae409368adee82ebe4edc090db570d21764.tar.gz android-node-v8-1c1b8ae409368adee82ebe4edc090db570d21764.tar.bz2 android-node-v8-1c1b8ae409368adee82ebe4edc090db570d21764.zip |
lib: support ranges in validateInt32()
This commit adds minimum and maximum value checks to the
validateInt32() validator.
PR-URL: https://github.com/nodejs/node/pull/20588
Fixes: https://github.com/nodejs/node/issues/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>
Diffstat (limited to 'lib/internal/validators.js')
-rw-r--r-- | lib/internal/validators.js | 10 |
1 files changed, 7 insertions, 3 deletions
diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 21017ffc5a..7fea1bdf84 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -48,7 +48,8 @@ function validateAndMaskMode(value, name, def) { throw new ERR_INVALID_ARG_VALUE(name, value, modeDesc); } -function validateInt32(value, name) { +function validateInt32(value, name, min = -2147483648, max = 2147483647) { + // The defaults for min and max correspond to the limits of 32-bit integers. if (!isInt32(value)) { let err; if (typeof value !== 'number') { @@ -56,11 +57,14 @@ function validateInt32(value, name) { } else if (!Number.isInteger(value)) { err = new ERR_OUT_OF_RANGE(name, 'an integer', value); } else { - // 2 ** 31 === 2147483648 - err = new ERR_OUT_OF_RANGE(name, '> -2147483649 && < 2147483648', value); + err = new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value); } Error.captureStackTrace(err, validateInt32); throw err; + } else if (value < min || value > max) { + const err = new ERR_OUT_OF_RANGE(name, `>= ${min} && <= ${max}`, value); + Error.captureStackTrace(err, validateInt32); + throw err; } } |