Skip to content

Commit

Permalink
crypto: accept decimal Number in randomBytes
Browse files Browse the repository at this point in the history
This change adds the ability to pass a double into randomBytes.

PR-URL: #15130
Fixes: #15118
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Evan Lucas <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Timothy Gu <[email protected]>
Reviewed-By: Tobias Nießen <[email protected]>
Reviewed-By: Joyee Cheung <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
  • Loading branch information
Benjamin Gruenbaum committed Sep 4, 2017
1 parent 8d5b013 commit 484bfa2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
6 changes: 3 additions & 3 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5616,13 +5616,13 @@ void RandomBytesProcessSync(Environment* env,
void RandomBytes(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);

if (!args[0]->IsUint32()) {
if (!args[0]->IsNumber() || args[0].As<v8::Number>()->Value() < 0) {
return env->ThrowTypeError("size must be a number >= 0");
}

const int64_t size = args[0]->IntegerValue();
if (size < 0 || size > Buffer::kMaxLength)
return env->ThrowRangeError("size is not a valid Smi");
if (size > Buffer::kMaxLength)
return env->ThrowTypeError("size must be a uint32");

Local<Object> obj = env->randombytes_constructor_template()->
NewInstance(env->context()).ToLocalChecked();
Expand Down
23 changes: 12 additions & 11 deletions test/parallel/test-crypto-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ crypto.DEFAULT_ENCODING = 'buffer';
// bump, we register a lot of exit listeners
process.setMaxListeners(256);

const errMessages = {
offsetNotNumber: /^TypeError: offset must be a number$/,
offsetOutOfRange: /^RangeError: offset out of range$/,
offsetNotUInt32: /^TypeError: offset must be a uint32$/,
sizeNotNumber: /^TypeError: size must be a number$/,
sizeNotUInt32: /^TypeError: size must be a uint32$/,
bufferTooSmall: /^RangeError: buffer too small$/,
};

const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
[crypto.randomBytes, crypto.pseudoRandomBytes].forEach(function(f) {
[-1, undefined, null, false, true, {}, []].forEach(function(value) {
Expand All @@ -41,10 +50,10 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
expectedErrorRegexp);
});

[0, 1, 2, 4, 16, 256, 1024].forEach(function(len) {
[0, 1, 2, 4, 16, 256, 1024, 101.2].forEach(function(len) {
f(len, common.mustCall(function(ex, buf) {
assert.strictEqual(ex, null);
assert.strictEqual(buf.length, len);
assert.strictEqual(buf.length, Math.floor(len));
assert.ok(Buffer.isBuffer(buf));
}));
});
Expand Down Expand Up @@ -139,14 +148,6 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
Buffer.alloc(10),
new Uint8Array(new Array(10).fill(0))
];
const errMessages = {
offsetNotNumber: /^TypeError: offset must be a number$/,
offsetOutOfRange: /^RangeError: offset out of range$/,
offsetNotUInt32: /^TypeError: offset must be a uint32$/,
sizeNotNumber: /^TypeError: size must be a number$/,
sizeNotUInt32: /^TypeError: size must be a uint32$/,
bufferTooSmall: /^RangeError: buffer too small$/,
};

const max = require('buffer').kMaxLength + 1;

Expand Down Expand Up @@ -264,4 +265,4 @@ const expectedErrorRegexp = /^TypeError: size must be a number >= 0$/;
// length exceeds max acceptable value"
assert.throws(function() {
crypto.randomBytes((-1 >>> 0) + 1);
}, /^TypeError: size must be a number >= 0$/);
}, errMessages.sizeNotUInt32);

0 comments on commit 484bfa2

Please sign in to comment.