Skip to content

Commit d4ee390

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 <info@bnoordhuis.nl>
1 parent 54e9a0b commit d4ee390

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
@@ -4862,9 +4862,13 @@ void RandomBytes(const FunctionCallbackInfo<Value>& args) {
48624862
return env->ThrowTypeError("size must be a number >= 0");
48634863
}
48644864

4865-
const uint32_t size = args[0]->Uint32Value();
4866-
if (size > Buffer::kMaxLength) {
4867-
return env->ThrowTypeError("size > Buffer::kMaxLength");
4865+
const int64_t size = args[0]->IntegerValue();
4866+
if (using_old_buffer) {
4867+
if (size > Buffer::kMaxLength)
4868+
return env->ThrowTypeError("size > Buffer::kMaxLength");
4869+
} else {
4870+
if (!IsValidSmi(size))
4871+
return env->ThrowRangeError("size is not a valid Smi");
48684872
}
48694873

48704874
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)