From 15b5a4567d7341ce93219b8294f051d6857cf62e Mon Sep 17 00:00:00 2001 From: cjihrig Date: Sat, 19 May 2018 21:07:16 -0400 Subject: [PATCH] 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 Reviewed-By: Weijia Wang Reviewed-By: Luigi Pinca Reviewed-By: James M Snell Reviewed-By: Matteo Collina Backport-PR-URL: https://github.com/nodejs/node/pull/21172 --- lib/internal/validators.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 991af52fee5b95..17b10dab189133 100644 --- a/lib/internal/validators.js +++ b/lib/internal/validators.js @@ -63,6 +63,7 @@ function validateInteger(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') { @@ -70,11 +71,14 @@ function validateInt32(value, name, min = -2147483648, max = 2147483647) { } 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; } }