Skip to content

Commit

Permalink
crypto: remove kMaxLength on randomBytes()
Browse files Browse the repository at this point in the history
New Buffer implementation allows greater than kMaxLength to be created.
So instead check if the passed value is a valid Smi.

PR-URL: #1825
Reviewed-By: Ben Noordhuis <[email protected]>
  • Loading branch information
trevnorris authored and rvagg committed Aug 4, 2015
1 parent 8664084 commit 944f680
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4869,9 +4869,13 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
return env->ThrowTypeError("size must be a number >= 0");
}

const uint32_t size = args[0]->Uint32Value();
if (size > Buffer::kMaxLength) {
return env->ThrowTypeError("size > Buffer::kMaxLength");
const int64_t size = args[0]->IntegerValue();
if (using_old_buffer) {
if (size > Buffer::kMaxLength)
return env->ThrowTypeError("size > Buffer::kMaxLength");
} else {
if (!IsValidSmi(size))
return env->ThrowRangeError("size is not a valid Smi");
}

Local<Object> obj = Object::New(env->isolate());
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-crypto-random.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ function checkCall(cb, desc) {
// #5126, "FATAL ERROR: v8::Object::SetIndexedPropertiesToExternalArrayData()
// length exceeds max acceptable value"
assert.throws(function() {
crypto.randomBytes(0x3fffffff + 1);
crypto.randomBytes((-1 >>> 0) + 1);
}, TypeError);

0 comments on commit 944f680

Please sign in to comment.