Skip to content

Commit d2f116c

Browse files
committed
crypto: fixup randomFill size and offset handling
Signed-off-by: James M Snell <[email protected]> PR-URL: #38138 Fixes: #38137 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: Darshan Sen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 7216eb6 commit d2f116c

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

Diff for: lib/internal/crypto/random.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,11 @@ function randomFill(buf, offset, size, callback) {
155155
if (typeof offset === 'function') {
156156
callback = offset;
157157
offset = 0;
158-
size = buf.bytesLength;
158+
// Size is a length here, assertSize() call turns it into a number of bytes
159+
size = buf.length;
159160
} else if (typeof size === 'function') {
160161
callback = size;
161-
size = buf.byteLength - offset;
162+
size = buf.length - offset;
162163
} else {
163164
validateCallback(callback);
164165
}
@@ -176,7 +177,6 @@ function randomFill(buf, offset, size, callback) {
176177
return;
177178
}
178179

179-
// TODO(@jasnell): This is not yet handling byte offsets right
180180
const job = new RandomBytesJob(
181181
kCryptoJobAsync,
182182
buf,

Diff for: test/parallel/test-crypto-random.js

+7
Original file line numberDiff line numberDiff line change
@@ -525,3 +525,10 @@ assert.throws(
525525
assert.throws(() => crypto.randomInt(0, 1, i), cbError);
526526
});
527527
}
528+
529+
{
530+
// Verify that it doesn't throw or abort
531+
crypto.randomFill(new Uint16Array(10), 0, common.mustSucceed());
532+
crypto.randomFill(new Uint32Array(10), 0, common.mustSucceed());
533+
crypto.randomFill(new Uint32Array(10), 0, 1, common.mustSucceed());
534+
}

0 commit comments

Comments
 (0)