From b394ef2b361b6af0e128b8fadef2f90da7d9a08f Mon Sep 17 00:00:00 2001 From: James M Snell Date: Wed, 7 Apr 2021 09:09:33 -0700 Subject: [PATCH 1/3] crypto: fixup randomFill size and offset handling Signed-off-by: James M Snell --- lib/internal/crypto/random.js | 5 ++--- test/parallel/test-crypto-random.js | 7 +++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/internal/crypto/random.js b/lib/internal/crypto/random.js index 88b535a357c4fa..aa6cd96d7a3fd5 100644 --- a/lib/internal/crypto/random.js +++ b/lib/internal/crypto/random.js @@ -155,10 +155,10 @@ function randomFill(buf, offset, size, callback) { if (typeof offset === 'function') { callback = offset; offset = 0; - size = buf.bytesLength; + size = buf.length; } else if (typeof size === 'function') { callback = size; - size = buf.byteLength - offset; + size = buf.length - offset; } else { validateCallback(callback); } @@ -176,7 +176,6 @@ function randomFill(buf, offset, size, callback) { return; } - // TODO(@jasnell): This is not yet handling byte offsets right const job = new RandomBytesJob( kCryptoJobAsync, buf, diff --git a/test/parallel/test-crypto-random.js b/test/parallel/test-crypto-random.js index 4f0c7dbb43f634..752bb8779f313d 100644 --- a/test/parallel/test-crypto-random.js +++ b/test/parallel/test-crypto-random.js @@ -525,3 +525,10 @@ assert.throws( assert.throws(() => crypto.randomInt(0, 1, i), cbError); }); } + +{ + // Verify that it doesn't throw or abort + crypto.randomFill(new Uint16Array(10), 0, common.mustSucceed()); + crypto.randomFill(new Uint32Array(10), 0, common.mustSucceed()); + crypto.randomFill(new Uint32Array(10), 0, 1, common.mustSucceed()); +} From 1e916156d39416e4208749fa557532fc469a0d61 Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 8 Apr 2021 07:18:02 -0700 Subject: [PATCH 2/3] [Squash] nit Co-authored-by: Anna Henningsen --- lib/internal/crypto/random.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/internal/crypto/random.js b/lib/internal/crypto/random.js index aa6cd96d7a3fd5..52f8b079b49511 100644 --- a/lib/internal/crypto/random.js +++ b/lib/internal/crypto/random.js @@ -155,6 +155,7 @@ function randomFill(buf, offset, size, callback) { if (typeof offset === 'function') { callback = offset; offset = 0; + // size is a length here, assertSize() call turns it into a number of bytes size = buf.length; } else if (typeof size === 'function') { callback = size; From 4643682f3c53930fdc0fe331ce0b4a49f74f8b4c Mon Sep 17 00:00:00 2001 From: James M Snell Date: Thu, 8 Apr 2021 11:11:46 -0700 Subject: [PATCH 3/3] [Squash] nit Co-authored-by: Darshan Sen --- lib/internal/crypto/random.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/internal/crypto/random.js b/lib/internal/crypto/random.js index 52f8b079b49511..e966074daa53c5 100644 --- a/lib/internal/crypto/random.js +++ b/lib/internal/crypto/random.js @@ -155,7 +155,7 @@ function randomFill(buf, offset, size, callback) { if (typeof offset === 'function') { callback = offset; offset = 0; - // size is a length here, assertSize() call turns it into a number of bytes + // Size is a length here, assertSize() call turns it into a number of bytes size = buf.length; } else if (typeof size === 'function') { callback = size;