Skip to content

Commit c954b18

Browse files
targosdebadree25
authored andcommitted
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: nodejs#49876 Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
1 parent 9e04f2c commit c954b18

5 files changed

+25
-25
lines changed

lib/internal/blob.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ const {
2323
createBlobFromFilePath: _createBlobFromFilePath,
2424
getDataObject,
2525
} = internalBinding('blob');
26+
const {
27+
kMaxLength,
28+
} = internalBinding('buffer');
2629

2730
const {
2831
TextDecoder,
@@ -61,7 +64,6 @@ const {
6164
} = require('internal/errors');
6265

6366
const {
64-
isUint32,
6567
validateDictionary,
6668
} = require('internal/validators');
6769

@@ -159,8 +161,8 @@ class Blob {
159161
return src;
160162
});
161163

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

165167
this[kHandle] = _createBlob(sources_, length);
166168
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)