crypto: fixup randomFill size and offset handling

Signed-off-by: James M Snell <jasnell@gmail.com>

PR-URL: https://github.com/nodejs/node/pull/38138
Fixes: https://github.com/nodejs/node/issues/38137
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Darshan Sen <raisinten@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
James M Snell 2021-04-07 09:09:33 -07:00
parent 7216eb67df
commit d2f116c6bb
No known key found for this signature in database
GPG Key ID: 7341B15C070877AC
2 changed files with 10 additions and 3 deletions

View File

@ -155,10 +155,11 @@ function randomFill(buf, offset, size, callback) {
if (typeof offset === 'function') {
callback = offset;
offset = 0;
size = buf.bytesLength;
// Size is a length here, assertSize() call turns it into a number of bytes
size = buf.length;
} else if (typeof size === 'function') {
callback = size;
size = buf.byteLength - offset;
size = buf.length - offset;
} else {
validateCallback(callback);
}
@ -176,7 +177,6 @@ function randomFill(buf, offset, size, callback) {
return;
}
// TODO(@jasnell): This is not yet handling byte offsets right
const job = new RandomBytesJob(
kCryptoJobAsync,
buf,

View File

@ -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());
}