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: 44b2995900
PR-URL: https://github.com/nodejs/node/pull/49876
Reviewed-By: Richard Lau <rlau@redhat.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
This commit is contained in:
Michaël Zasso 2023-09-28 14:50:20 +02:00 committed by GitHub
parent cc725a653a
commit a4fdb1abe0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 25 deletions

View File

@ -24,6 +24,9 @@ const {
concat, concat,
getDataObject, getDataObject,
} = internalBinding('blob'); } = internalBinding('blob');
const {
kMaxLength,
} = internalBinding('buffer');
const { const {
TextDecoder, TextDecoder,
@ -62,7 +65,6 @@ const {
} = require('internal/errors'); } = require('internal/errors');
const { const {
isUint32,
validateDictionary, validateDictionary,
} = require('internal/validators'); } = require('internal/validators');
@ -160,8 +162,8 @@ class Blob {
return src; return src;
}); });
if (!isUint32(length)) if (length > kMaxLength)
throw new ERR_BUFFER_TOO_LARGE(0xFFFFFFFF); throw new ERR_BUFFER_TOO_LARGE(kMaxLength);
this[kHandle] = _createBlob(sources_, length); this[kHandle] = _createBlob(sources_, length);
this[kLength] = length; this[kLength] = length;

View File

@ -3,17 +3,17 @@
const common = require('../common'); const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const { Blob } = require('buffer'); const { Blob, kMaxLength } = require('buffer');
if (common.isFreeBSD) if (common.isFreeBSD)
common.skip('Oversized buffer make the FreeBSD CI runner crash'); common.skip('Oversized buffer make the FreeBSD CI runner crash');
try { try {
new Blob([new Uint8Array(0xffffffff), [1]]); new Blob([new Uint8Array(kMaxLength), [1]]);
} catch (e) { } catch (e) {
if ( if (
e.message === 'Array buffer allocation failed' || e.message === 'Array buffer allocation failed' ||
e.message === 'Invalid typed array length: 4294967295' e.message === `Invalid typed array length: ${kMaxLength}`
) { ) {
common.skip( common.skip(
'Insufficient memory on this platform for oversized buffer test.' 'Insufficient memory on this platform for oversized buffer test.'

View File

@ -4,13 +4,16 @@ const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const vm = require('vm'); const vm = require('vm');
const SlowBuffer = require('buffer').SlowBuffer; const {
SlowBuffer,
kMaxLength,
} = require('buffer');
// Verify the maximum Uint8Array size. There is no concrete limit by spec. The // Verify the maximum Uint8Array size. There is no concrete limit by spec. The
// internal limits should be updated if this fails. // internal limits should be updated if this fails.
assert.throws( assert.throws(
() => new Uint8Array(2 ** 32 + 1), () => new Uint8Array(kMaxLength + 1),
{ message: 'Invalid typed array length: 4294967297' } { message: `Invalid typed array length: ${kMaxLength + 1}` },
); );
const b = Buffer.allocUnsafe(1024); const b = Buffer.allocUnsafe(1024);

View File

@ -12,18 +12,8 @@ const bufferMaxSizeMsg = {
name: 'RangeError', name: 'RangeError',
}; };
assert.throws(() => Buffer((-1 >>> 0) + 2), bufferMaxSizeMsg);
assert.throws(() => SlowBuffer((-1 >>> 0) + 2), bufferMaxSizeMsg);
assert.throws(() => Buffer.alloc((-1 >>> 0) + 2), bufferMaxSizeMsg);
assert.throws(() => Buffer.allocUnsafe((-1 >>> 0) + 2), bufferMaxSizeMsg);
assert.throws(() => Buffer.allocUnsafeSlow((-1 >>> 0) + 2), bufferMaxSizeMsg);
assert.throws(() => Buffer(kMaxLength + 1), bufferMaxSizeMsg); assert.throws(() => Buffer(kMaxLength + 1), bufferMaxSizeMsg);
assert.throws(() => SlowBuffer(kMaxLength + 1), bufferMaxSizeMsg); assert.throws(() => SlowBuffer(kMaxLength + 1), bufferMaxSizeMsg);
assert.throws(() => Buffer.alloc(kMaxLength + 1), bufferMaxSizeMsg); assert.throws(() => Buffer.alloc(kMaxLength + 1), bufferMaxSizeMsg);
assert.throws(() => Buffer.allocUnsafe(kMaxLength + 1), bufferMaxSizeMsg); assert.throws(() => Buffer.allocUnsafe(kMaxLength + 1), bufferMaxSizeMsg);
assert.throws(() => Buffer.allocUnsafeSlow(kMaxLength + 1), bufferMaxSizeMsg); assert.throws(() => Buffer.allocUnsafeSlow(kMaxLength + 1), bufferMaxSizeMsg);
// issue GH-4331
assert.throws(() => Buffer.allocUnsafe(0x100000001), bufferMaxSizeMsg);
assert.throws(() => Buffer.allocUnsafe(0xFFFFFFFFF), bufferMaxSizeMsg);

View File

@ -1,17 +1,22 @@
'use strict'; 'use strict';
require('../common'); require('../common');
// This test ensures that Node.js throws a RangeError when trying to convert a // This test ensures that Node.js throws an Error when trying to convert a
// gigantic buffer into a string. // large buffer into a string.
// Regression test for https://github.com/nodejs/node/issues/649. // Regression test for https://github.com/nodejs/node/issues/649.
const assert = require('assert'); const assert = require('assert');
const SlowBuffer = require('buffer').SlowBuffer; const {
SlowBuffer,
constants: {
MAX_STRING_LENGTH,
},
} = require('buffer');
const len = 1422561062959; const len = MAX_STRING_LENGTH + 1;
const message = { const message = {
code: 'ERR_OUT_OF_RANGE', code: 'ERR_STRING_TOO_LONG',
name: 'RangeError', name: 'Error',
}; };
assert.throws(() => Buffer(len).toString('utf8'), message); assert.throws(() => Buffer(len).toString('utf8'), message);
assert.throws(() => SlowBuffer(len).toString('utf8'), message); assert.throws(() => SlowBuffer(len).toString('utf8'), message);