diff --git a/lib/internal/validators.js b/lib/internal/validators.js index 21017ffc5ab791..7fea1bdf84c173 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; } } diff --git a/test/parallel/test-fs-truncate.js b/test/parallel/test-fs-truncate.js index 2f8839583202d0..eb469b8e271aed 100644 --- a/test/parallel/test-fs-truncate.js +++ b/test/parallel/test-fs-truncate.js @@ -210,7 +210,7 @@ function testFtruncate(cb) { code: 'ERR_OUT_OF_RANGE', name: 'RangeError [ERR_OUT_OF_RANGE]', message: 'The value of "len" is out of range. It must be ' + - `> -2147483649 && < 2147483648. Received ${input}` + `>= -2147483648 && <= 2147483647. Received ${input}` } ); });