Skip to content

Commit b394ef2

Browse files
committed
crypto: fixup randomFill size and offset handling
Signed-off-by: James M Snell <jasnell@gmail.com>
1 parent 4bc8f75 commit b394ef2

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/internal/crypto/random.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,10 @@ function randomFill(buf, offset, size, callback) {
155155
if (typeof offset === 'function') {
156156
callback = offset;
157157
offset = 0;
158-
size = buf.bytesLength;
158+
size = buf.length;
159159
} else if (typeof size === 'function') {
160160
callback = size;
161-
size = buf.byteLength - offset;
161+
size = buf.length - offset;
162162
} else {
163163
validateCallback(callback);
164164
}
@@ -176,7 +176,6 @@ function randomFill(buf, offset, size, callback) {
176176
return;
177177
}
178178

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

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)