Skip to content

Commit 74772b4

Browse files
committed
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: #2003 Reviewed-By: Chris Dickinson <[email protected]>
1 parent 309c0f4 commit 74772b4

File tree

6 files changed

+21
-7
lines changed

6 files changed

+21
-7
lines changed

lib/fs.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const Readable = Stream.Readable;
2020
const Writable = Stream.Writable;
2121

2222
const kMinPoolSpace = 128;
23-
const kMaxLength = process.binding('smalloc').kMaxLength;
23+
const kMaxLength = require('buffer').kMaxLength;
2424

2525
const O_APPEND = constants.O_APPEND || 0;
2626
const O_CREAT = constants.O_CREAT || 0;

lib/internal/buffer_new.js

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const internalUtil = require('internal/util');
66
exports.Buffer = Buffer;
77
exports.SlowBuffer = SlowBuffer;
88
exports.INSPECT_MAX_BYTES = 50;
9+
exports.kMaxLength = binding.kMaxLength;
910

1011

1112
Buffer.poolSize = 8 * 1024;

lib/internal/buffer_old.js

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const kMaxLength = smalloc.kMaxLength;
1111
exports.Buffer = Buffer;
1212
exports.SlowBuffer = SlowBuffer;
1313
exports.INSPECT_MAX_BYTES = 50;
14+
exports.kMaxLength = binding.kMaxLength;
1415

1516

1617
Buffer.poolSize = 8 * 1024;

lib/zlib.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const Transform = require('_stream_transform');
55
const binding = process.binding('zlib');
66
const util = require('util');
77
const assert = require('assert').ok;
8-
const kMaxLength = process.binding('smalloc').kMaxLength;
8+
const kMaxLength = require('buffer').kMaxLength;
99
const kRangeErrorMessage = 'Cannot create final Buffer. ' +
1010
'It would be larger than 0x' + kMaxLength.toString(16) + ' bytes.';
1111

src/node_buffer.cc

+11
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ using v8::FunctionCallbackInfo;
6565
using v8::FunctionTemplate;
6666
using v8::Handle;
6767
using v8::HandleScope;
68+
using v8::Integer;
6869
using v8::Isolate;
6970
using v8::Local;
7071
using v8::Maybe;
@@ -1156,6 +1157,16 @@ void Initialize(Handle<Object> target,
11561157
env->SetMethod(target, "writeDoubleLE", WriteDoubleLE);
11571158
env->SetMethod(target, "writeFloatBE", WriteFloatBE);
11581159
env->SetMethod(target, "writeFloatLE", WriteFloatLE);
1160+
1161+
uint32_t kMaxLength;
1162+
if (sizeof(int32_t) == sizeof(intptr_t) || using_old_buffer) {
1163+
kMaxLength = 0x3fffffff;
1164+
} else {
1165+
kMaxLength = 0x7fffffff;
1166+
}
1167+
target->Set(env->context(),
1168+
FIXED_ONE_BYTE_STRING(env->isolate(), "kMaxLength"),
1169+
Integer::NewFromUnsigned(env->isolate(), kMaxLength)).FromJust();
11591170
}
11601171

11611172

test/parallel/test-regress-GH-io-1811.js

+6-5
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
const assert = require('assert');
44

5-
// Change kMaxLength for zlib to trigger the error
6-
// without having to allocate 1GB of buffers
7-
const smalloc = process.binding('smalloc');
8-
smalloc.kMaxLength = 128;
5+
// Change kMaxLength for zlib to trigger the error without having to allocate
6+
// large Buffers.
7+
const buffer = require('buffer');
8+
const oldkMaxLength = buffer.kMaxLength;
9+
buffer.kMaxLength = 128;
910
const zlib = require('zlib');
10-
smalloc.kMaxLength = 0x3fffffff;
11+
buffer.kMaxLength = oldkMaxLength;
1112

1213
const encoded = new Buffer('H4sIAAAAAAAAA0tMHFgAAIw2K/GAAAAA', 'base64');
1314

0 commit comments

Comments
 (0)