Skip to content

Commit 5bca8fe

Browse files
committed
lib,test: do not hardcode Buffer.kMaxLength
V8 will soon support typed arrays as large as the maximum array buffer length. This patch replaces hardcoded values related to Buffer.kMaxLength with the actual constant. It also fixes a test that was passing by accident. Refs: v8/v8@44b2995 PR-URL: #49876 Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent c71e548 commit 5bca8fe

5 files changed

+25
-25
lines changed

lib/internal/blob.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ const {
2424
concat,
2525
getDataObject,
2626
} = internalBinding('blob');
27+
const {
28+
kMaxLength,
29+
} = internalBinding('buffer');
2730

2831
const {
2932
TextDecoder,
@@ -62,7 +65,6 @@ const {
6265
} = require('internal/errors');
6366

6467
const {
65-
isUint32,
6668
validateDictionary,
6769
} = require('internal/validators');
6870

@@ -158,8 +160,8 @@ class Blob {
158160
return src;
159161
});
160162

161-
if (!isUint32(length))
162-
throw new ERR_BUFFER_TOO_LARGE(0xFFFFFFFF);
163+
if (length > kMaxLength)
164+
throw new ERR_BUFFER_TOO_LARGE(kMaxLength);
163165

164166
this[kHandle] = _createBlob(sources_, length);
165167
this[kLength] = length;

test/parallel/test-blob-buffer-too-large.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@
33

44
const common = require('../common');
55
const assert = require('assert');
6-
const { Blob } = require('buffer');
6+
const { Blob, kMaxLength } = require('buffer');
77

88
if (common.isFreeBSD)
99
common.skip('Oversized buffer make the FreeBSD CI runner crash');
1010

1111
try {
12-
new Blob([new Uint8Array(0xffffffff), [1]]);
12+
new Blob([new Uint8Array(kMaxLength), [1]]);
1313
} catch (e) {
1414
if (
1515
e.message === 'Array buffer allocation failed' ||
16-
e.message === 'Invalid typed array length: 4294967295'
16+
e.message === `Invalid typed array length: ${kMaxLength}`
1717
) {
1818
common.skip(
1919
'Insufficient memory on this platform for oversized buffer test.'

test/parallel/test-buffer-alloc.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@ const common = require('../common');
44
const assert = require('assert');
55
const vm = require('vm');
66

7-
const SlowBuffer = require('buffer').SlowBuffer;
7+
const {
8+
SlowBuffer,
9+
kMaxLength,
10+
} = require('buffer');
811

912
// Verify the maximum Uint8Array size. There is no concrete limit by spec. The
1013
// internal limits should be updated if this fails.
1114
assert.throws(
12-
() => new Uint8Array(2 ** 32 + 1),
13-
{ message: 'Invalid typed array length: 4294967297' }
15+
() => new Uint8Array(kMaxLength + 1),
16+
{ message: `Invalid typed array length: ${kMaxLength + 1}` },
1417
);
1518

1619
const b = Buffer.allocUnsafe(1024);

test/parallel/test-buffer-over-max-length.js

-10
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,8 @@ const bufferMaxSizeMsg = {
1212
name: 'RangeError',
1313
};
1414

15-
assert.throws(() => Buffer((-1 >>> 0) + 2), bufferMaxSizeMsg);
16-
assert.throws(() => SlowBuffer((-1 >>> 0) + 2), bufferMaxSizeMsg);
17-
assert.throws(() => Buffer.alloc((-1 >>> 0) + 2), bufferMaxSizeMsg);
18-
assert.throws(() => Buffer.allocUnsafe((-1 >>> 0) + 2), bufferMaxSizeMsg);
19-
assert.throws(() => Buffer.allocUnsafeSlow((-1 >>> 0) + 2), bufferMaxSizeMsg);
20-
2115
assert.throws(() => Buffer(kMaxLength + 1), bufferMaxSizeMsg);
2216
assert.throws(() => SlowBuffer(kMaxLength + 1), bufferMaxSizeMsg);
2317
assert.throws(() => Buffer.alloc(kMaxLength + 1), bufferMaxSizeMsg);
2418
assert.throws(() => Buffer.allocUnsafe(kMaxLength + 1), bufferMaxSizeMsg);
2519
assert.throws(() => Buffer.allocUnsafeSlow(kMaxLength + 1), bufferMaxSizeMsg);
26-
27-
// issue GH-4331
28-
assert.throws(() => Buffer.allocUnsafe(0x100000001), bufferMaxSizeMsg);
29-
assert.throws(() => Buffer.allocUnsafe(0xFFFFFFFFF), bufferMaxSizeMsg);

test/parallel/test-buffer-tostring-rangeerror.js

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
'use strict';
22
require('../common');
33

4-
// This test ensures that Node.js throws a RangeError when trying to convert a
5-
// gigantic buffer into a string.
4+
// This test ensures that Node.js throws an Error when trying to convert a
5+
// large buffer into a string.
66
// Regression test for https://github.com/nodejs/node/issues/649.
77

88
const assert = require('assert');
9-
const SlowBuffer = require('buffer').SlowBuffer;
9+
const {
10+
SlowBuffer,
11+
constants: {
12+
MAX_STRING_LENGTH,
13+
},
14+
} = require('buffer');
1015

11-
const len = 1422561062959;
16+
const len = MAX_STRING_LENGTH + 1;
1217
const message = {
13-
code: 'ERR_OUT_OF_RANGE',
14-
name: 'RangeError',
18+
code: 'ERR_STRING_TOO_LONG',
19+
name: 'Error',
1520
};
1621
assert.throws(() => Buffer(len).toString('utf8'), message);
1722
assert.throws(() => SlowBuffer(len).toString('utf8'), message);

0 commit comments

Comments
 (0)