From b625ab4242f55b7769e990ea1386d77ee224f273 Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Wed, 17 Jun 2015 11:34:12 -0600 Subject: [PATCH] buffer: fix usage of kMaxLength Starting in V8 v4.3 the maximum array index of a typed array is the same as the largest Smi supported on a given architecture. To compensate for these differences export kMaxLength from the buffer module with the correct size for the given architecture. PR-URL: https://github.com/nodejs/io.js/pull/2003 Reviewed-By: Chris Dickinson --- lib/fs.js | 2 +- lib/internal/buffer_new.js | 1 + lib/internal/buffer_old.js | 1 + lib/zlib.js | 2 +- src/node_buffer.cc | 11 +++++++++++ test/parallel/test-regress-GH-io-1811.js | 11 ++++++----- 6 files changed, 21 insertions(+), 7 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 9c13a82c41e5b1..138a90e2f2ecd5 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -20,7 +20,7 @@ const Readable = Stream.Readable; const Writable = Stream.Writable; const kMinPoolSpace = 128; -const kMaxLength = process.binding('smalloc').kMaxLength; +const kMaxLength = require('buffer').kMaxLength; const O_APPEND = constants.O_APPEND || 0; const O_CREAT = constants.O_CREAT || 0; diff --git a/lib/internal/buffer_new.js b/lib/internal/buffer_new.js index 97df88e17e4c60..4bbb1a57a039b5 100644 --- a/lib/internal/buffer_new.js +++ b/lib/internal/buffer_new.js @@ -6,6 +6,7 @@ const internalUtil = require('internal/util'); exports.Buffer = Buffer; exports.SlowBuffer = SlowBuffer; exports.INSPECT_MAX_BYTES = 50; +exports.kMaxLength = binding.kMaxLength; Buffer.poolSize = 8 * 1024; diff --git a/lib/internal/buffer_old.js b/lib/internal/buffer_old.js index 2d8d00249ee642..92786a9c37701f 100644 --- a/lib/internal/buffer_old.js +++ b/lib/internal/buffer_old.js @@ -11,6 +11,7 @@ const kMaxLength = smalloc.kMaxLength; exports.Buffer = Buffer; exports.SlowBuffer = SlowBuffer; exports.INSPECT_MAX_BYTES = 50; +exports.kMaxLength = binding.kMaxLength; Buffer.poolSize = 8 * 1024; diff --git a/lib/zlib.js b/lib/zlib.js index 67bdcaff64bed6..a10d9118d6194e 100644 --- a/lib/zlib.js +++ b/lib/zlib.js @@ -5,7 +5,7 @@ const Transform = require('_stream_transform'); const binding = process.binding('zlib'); const util = require('util'); const assert = require('assert').ok; -const kMaxLength = process.binding('smalloc').kMaxLength; +const kMaxLength = require('buffer').kMaxLength; const kRangeErrorMessage = 'Cannot create final Buffer. ' + 'It would be larger than 0x' + kMaxLength.toString(16) + ' bytes.'; diff --git a/src/node_buffer.cc b/src/node_buffer.cc index f567ef9373eab0..96cd61f89e9fd5 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -71,6 +71,7 @@ using v8::FunctionCallbackInfo; using v8::FunctionTemplate; using v8::Handle; using v8::HandleScope; +using v8::Integer; using v8::Isolate; using v8::Local; using v8::Maybe; @@ -1188,6 +1189,16 @@ void Initialize(Handle target, env->SetMethod(target, "writeDoubleLE", WriteDoubleLE); env->SetMethod(target, "writeFloatBE", WriteFloatBE); env->SetMethod(target, "writeFloatLE", WriteFloatLE); + + uint32_t kMaxLength; + if (sizeof(int32_t) == sizeof(intptr_t) || using_old_buffer) { + kMaxLength = 0x3fffffff; + } else { + kMaxLength = 0x7fffffff; + } + target->Set(env->context(), + FIXED_ONE_BYTE_STRING(env->isolate(), "kMaxLength"), + Integer::NewFromUnsigned(env->isolate(), kMaxLength)).FromJust(); } diff --git a/test/parallel/test-regress-GH-io-1811.js b/test/parallel/test-regress-GH-io-1811.js index 2ef8ff8c090aa4..b7c99a17a5cdd5 100644 --- a/test/parallel/test-regress-GH-io-1811.js +++ b/test/parallel/test-regress-GH-io-1811.js @@ -2,12 +2,13 @@ const assert = require('assert'); -// Change kMaxLength for zlib to trigger the error -// without having to allocate 1GB of buffers -const smalloc = process.binding('smalloc'); -smalloc.kMaxLength = 128; +// Change kMaxLength for zlib to trigger the error without having to allocate +// large Buffers. +const buffer = require('buffer'); +const oldkMaxLength = buffer.kMaxLength; +buffer.kMaxLength = 128; const zlib = require('zlib'); -smalloc.kMaxLength = 0x3fffffff; +buffer.kMaxLength = oldkMaxLength; const encoded = new Buffer('H4sIAAAAAAAAA0tMHFgAAIw2K/GAAAAA', 'base64');