Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: fixup randomFill size and offset handling #38138

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions lib/internal/crypto/random.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,10 @@ function randomFill(buf, offset, size, callback) {
if (typeof offset === 'function') {
callback = offset;
offset = 0;
size = buf.bytesLength;
size = buf.length;
jasnell marked this conversation as resolved.
Show resolved Hide resolved
} else if (typeof size === 'function') {
callback = size;
size = buf.byteLength - offset;
size = buf.length - offset;
} else {
validateCallback(callback);
}
Expand All @@ -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,
Expand Down
7 changes: 7 additions & 0 deletions test/parallel/test-crypto-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}