mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
crypto: fix randomInt range check
Refs: https://github.com/nodejs/node/pull/34600 PR-URL: https://github.com/nodejs/node/pull/35052 Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
1 parent
f6b2286e12
commit
a12d92c97b
2 files changed
+12
-9
No files matched your search
@@ -149,8 +149,8 @@ function randomInt(min, max, callback) {
|
||||
if (!NumberIsSafeInteger(max)) {
|
||||
throw new ERR_INVALID_ARG_TYPE('max', 'safe integer', max);
|
||||
}
|
||||
if (!(max >= min)) {
|
||||
throw new ERR_OUT_OF_RANGE('max', `>= ${min}`, max);
|
||||
if (max <= min) {
|
||||
throw new ERR_OUT_OF_RANGE('max', `> ${min}`, max);
|
||||
}
|
||||
|
||||
// First we generate a random int between [0..range)
|
||||
|
||||
@@ -456,13 +456,16 @@ assert.throws(
|
||||
}
|
||||
);
|
||||
|
||||
crypto.randomInt(0, common.mustCall());
|
||||
crypto.randomInt(0, 0, common.mustCall());
|
||||
assert.throws(() => crypto.randomInt(-1, common.mustNotCall()), {
|
||||
code: 'ERR_OUT_OF_RANGE',
|
||||
name: 'RangeError',
|
||||
message: 'The value of "max" is out of range. It must be >= 0. Received -1'
|
||||
});
|
||||
crypto.randomInt(1, common.mustCall());
|
||||
crypto.randomInt(0, 1, common.mustCall());
|
||||
for (const arg of [[0], [1, 1], [3, 2], [-5, -5], [11, -10]]) {
|
||||
assert.throws(() => crypto.randomInt(...arg, common.mustNotCall()), {
|
||||
code: 'ERR_OUT_OF_RANGE',
|
||||
name: 'RangeError',
|
||||
message: 'The value of "max" is out of range. It must be > ' +
|
||||
`${arg[arg.length - 2] || 0}. Received ${arg[arg.length - 1]}`
|
||||
});
|
||||
}
|
||||
|
||||
const MAX_RANGE = 0xFFFF_FFFF_FFFF;
|
||||
crypto.randomInt(MAX_RANGE, common.mustCall());
|
||||
|
||||
Reference in new issue
Block a user