From 944f68046c028484f3f5e741d63f85d65e08090c Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Tue, 2 Jun 2015 13:10:47 -0600 Subject: [PATCH] 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: https://github.com/nodejs/io.js/pull/1825 Reviewed-By: Ben Noordhuis --- src/node_crypto.cc | 10 +++++++--- test/parallel/test-crypto-random.js | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 953bf1b..3dc1bfa 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -4869,9 +4869,13 @@ void RandomBytes(const FunctionCallbackInfo& 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 obj = Object::New(env->isolate()); diff --git a/test/parallel/test-crypto-random.js b/test/parallel/test-crypto-random.js index 3454796..2d28ccc 100644 --- a/test/parallel/test-crypto-random.js +++ b/test/parallel/test-crypto-random.js @@ -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); -- 2.7.4