From b3aa9f53cdbf6dac72396c4392e11dcc4c37df65 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 24 Oct 2022 15:50:39 +0200 Subject: [PATCH] Fix random number generators input checks MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/utils/Utils.ts | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/utils/Utils.ts b/src/utils/Utils.ts index e8e658d1..d396cfa9 100644 --- a/src/utils/Utils.ts +++ b/src/utils/Utils.ts @@ -106,7 +106,7 @@ export default class Utils { } public static getRandomFloat(max = Number.MAX_VALUE, min = 0, negative = false): number { - if (max < min || min < 0 || max < 0) { + if (max < min || max < 0 || min < 0) { throw new RangeError('Invalid interval'); } const randomPositiveFloat = crypto.randomBytes(4).readUInt32LE() / 0xffffffff; @@ -115,14 +115,11 @@ export default class Utils { } public static getRandomInteger(max = Number.MAX_SAFE_INTEGER, min = 0): number { - if (max < 0) { + if (max < min || max < 0 || min < 0) { throw new RangeError('Invalid interval'); } max = Math.floor(max); if (!Utils.isNullOrUndefined(min) && min !== 0) { - if (max < min || min < 0) { - throw new RangeError('Invalid interval'); - } min = Math.ceil(min); return Math.floor(Utils.secureRandom() * (max - min + 1)) + min; } -- 2.34.1