Skip to content

Commit 944f680

Browse files
trevnorrisrvagg
authored andcommitted
crypto: remove kMaxLength on randomBytes()
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]>
1 parent 8664084 commit 944f680

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/node_crypto.cc

+7-3
Original file line numberDiff line numberDiff line change
@@ -4869,9 +4869,13 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
48694869
return env->ThrowTypeError("size must be a number >= 0");
48704870
}
48714871

4872-
const uint32_t size = args[0]->Uint32Value();
4873-
if (size > Buffer::kMaxLength) {
4874-
return env->ThrowTypeError("size > Buffer::kMaxLength");
4872+
const int64_t size = args[0]->IntegerValue();
4873+
if (using_old_buffer) {
4874+
if (size > Buffer::kMaxLength)
4875+
return env->ThrowTypeError("size > Buffer::kMaxLength");
4876+
} else {
4877+
if (!IsValidSmi(size))
4878+
return env->ThrowRangeError("size is not a valid Smi");
48754879
}
48764880

48774881
Local<Object> obj = Object::New(env->isolate());

test/parallel/test-crypto-random.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,5 @@ function checkCall(cb, desc) {
5353
// #5126, "FATAL ERROR: v8::Object::SetIndexedPropertiesToExternalArrayData()
5454
// length exceeds max acceptable value"
5555
assert.throws(function() {
56-
crypto.randomBytes(0x3fffffff + 1);
56+
crypto.randomBytes((-1 >>> 0) + 1);
5757
}, TypeError);

0 commit comments

Comments
 (0)