From d2745cb14f5c8dc128c4a8210af9b526af6aece5 Mon Sep 17 00:00:00 2001 From: Qingyu Deng Date: Mon, 5 Apr 2021 21:35:40 +0800 Subject: [PATCH] crypto: reduce range of size to int max Refs: https://github.com/nodejs/node/issues/38092 --- lib/internal/crypto/random.js | 5 +++-- test/parallel/test-crypto-random.js | 6 +++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/internal/crypto/random.js b/lib/internal/crypto/random.js index 88b535a357c4fae..1479ff7cfdf2770 100644 --- a/lib/internal/crypto/random.js +++ b/lib/internal/crypto/random.js @@ -57,8 +57,8 @@ const { const { FastBuffer } = require('internal/buffer'); -const kMaxUint32 = 2 ** 32 - 1; -const kMaxPossibleLength = MathMin(kMaxLength, kMaxUint32); +const kMaxInt32 = 2 ** 31 - 1; +const kMaxPossibleLength = MathMin(kMaxLength, kMaxInt32); function assertOffset(offset, elementSize, length) { validateNumber(offset, 'offset'); @@ -89,6 +89,7 @@ function assertSize(size, elementSize, offset, length) { } function randomBytes(size, callback) { + const kMaxSize = 2 ** 31 - 1 size = assertSize(size, 1, 0, Infinity); if (callback !== undefined) { validateCallback(callback); diff --git a/test/parallel/test-crypto-random.js b/test/parallel/test-crypto-random.js index 4f0c7dbb43f634b..a8a4f908f48115c 100644 --- a/test/parallel/test-crypto-random.js +++ b/test/parallel/test-crypto-random.js @@ -32,8 +32,8 @@ const cryptop = require('crypto').webcrypto; const { kMaxLength } = require('buffer'); const { inspect } = require('util'); -const kMaxUint32 = Math.pow(2, 32) - 1; -const kMaxPossibleLength = Math.min(kMaxLength, kMaxUint32); +const kMaxInt32 = 2 ** 31 - 1 +const kMaxPossibleLength = Math.min(kMaxLength, kMaxInt32); common.expectWarning('DeprecationWarning', 'crypto.pseudoRandomBytes is deprecated.', 'DEP0115'); @@ -51,7 +51,7 @@ common.expectWarning('DeprecationWarning', assert.throws(() => f(value, common.mustNotCall()), errObj); }); - [-1, NaN, 2 ** 32].forEach((value) => { + [-1, NaN, 2 ** 32, 2 ** 31].forEach((value) => { const errObj = { code: 'ERR_OUT_OF_RANGE', name: 'RangeError',