Skip to content

Commit 6fb7baf

Browse files
ZYSzysaddaleax
authored andcommitted
buffer: harden validation of buffer allocation size
This makes using `NaN` as the buffer size throw an error. Fixes: #26151 PR-URL: #26162 Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent dbfe14c commit 6fb7baf

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

lib/buffer.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ function assertSize(size) {
256256

257257
if (typeof size !== 'number') {
258258
err = new ERR_INVALID_ARG_TYPE('size', 'number', size);
259-
} else if (size < 0 || size > kMaxLength) {
259+
} else if (!(size >= 0 && size <= kMaxLength)) {
260260
err = new ERR_INVALID_OPT_VALUE.RangeError('size', size);
261261
}
262262

@@ -458,8 +458,11 @@ Buffer.concat = function concat(list, length) {
458458

459459
if (length === undefined) {
460460
length = 0;
461-
for (i = 0; i < list.length; i++)
462-
length += list[i].length;
461+
for (i = 0; i < list.length; i++) {
462+
if (list[i].length) {
463+
length += list[i].length;
464+
}
465+
}
463466
} else {
464467
length = length >>> 0;
465468
}

test/parallel/test-buffer-alloc.js

-1
Original file line numberDiff line numberDiff line change
@@ -764,7 +764,6 @@ assert.strictEqual(x.inspect(), '<Buffer 81 a3 66 6f 6f a3 62 61 72>');
764764
Buffer.allocUnsafe(3.3).fill().toString();
765765
// throws bad argument error in commit 43cb4ec
766766
Buffer.alloc(3.3).fill().toString();
767-
assert.strictEqual(Buffer.allocUnsafe(NaN).length, 0);
768767
assert.strictEqual(Buffer.allocUnsafe(3.3).length, 3);
769768
assert.strictEqual(Buffer.from({ length: 3.3 }).length, 3);
770769
assert.strictEqual(Buffer.from({ length: 'BAM' }).length, 0);

test/parallel/test-buffer-no-negative-allocation.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,26 @@ const msg = common.expectsError({
77
code: 'ERR_INVALID_OPT_VALUE',
88
type: RangeError,
99
message: /^The value "[^"]*" is invalid for option "size"$/
10-
}, 12);
10+
}, 16);
1111

1212
// Test that negative Buffer length inputs throw errors.
1313

1414
assert.throws(() => Buffer(-Buffer.poolSize), msg);
1515
assert.throws(() => Buffer(-100), msg);
1616
assert.throws(() => Buffer(-1), msg);
17+
assert.throws(() => Buffer(NaN), msg);
1718

1819
assert.throws(() => Buffer.alloc(-Buffer.poolSize), msg);
1920
assert.throws(() => Buffer.alloc(-100), msg);
2021
assert.throws(() => Buffer.alloc(-1), msg);
22+
assert.throws(() => Buffer.alloc(NaN), msg);
2123

2224
assert.throws(() => Buffer.allocUnsafe(-Buffer.poolSize), msg);
2325
assert.throws(() => Buffer.allocUnsafe(-100), msg);
2426
assert.throws(() => Buffer.allocUnsafe(-1), msg);
27+
assert.throws(() => Buffer.allocUnsafe(NaN), msg);
2528

2629
assert.throws(() => Buffer.allocUnsafeSlow(-Buffer.poolSize), msg);
2730
assert.throws(() => Buffer.allocUnsafeSlow(-100), msg);
2831
assert.throws(() => Buffer.allocUnsafeSlow(-1), msg);
32+
assert.throws(() => Buffer.allocUnsafeSlow(NaN), msg);

test/parallel/test-buffer-slow.js

+9-21
Original file line numberDiff line numberDiff line change
@@ -43,29 +43,17 @@ try {
4343
assert.strictEqual(SlowBuffer('6').length, 6);
4444
assert.strictEqual(SlowBuffer(true).length, 1);
4545

46-
// Should create zero-length buffer if parameter is not a number
47-
assert.strictEqual(SlowBuffer().length, 0);
48-
assert.strictEqual(SlowBuffer(NaN).length, 0);
49-
assert.strictEqual(SlowBuffer({}).length, 0);
50-
assert.strictEqual(SlowBuffer('string').length, 0);
51-
5246
// should throw with invalid length
5347
const bufferMaxSizeMsg = common.expectsError({
5448
code: 'ERR_INVALID_OPT_VALUE',
5549
type: RangeError,
5650
message: /^The value "[^"]*" is invalid for option "size"$/
57-
}, 2);
58-
assert.throws(function() {
59-
SlowBuffer(Infinity);
60-
}, bufferMaxSizeMsg);
61-
common.expectsError(function() {
62-
SlowBuffer(-1);
63-
}, {
64-
code: 'ERR_INVALID_OPT_VALUE',
65-
type: RangeError,
66-
message: 'The value "-1" is invalid for option "size"'
67-
});
68-
69-
assert.throws(function() {
70-
SlowBuffer(buffer.kMaxLength + 1);
71-
}, bufferMaxSizeMsg);
51+
}, 7);
52+
53+
assert.throws(() => SlowBuffer(), bufferMaxSizeMsg);
54+
assert.throws(() => SlowBuffer(NaN), bufferMaxSizeMsg);
55+
assert.throws(() => SlowBuffer({}), bufferMaxSizeMsg);
56+
assert.throws(() => SlowBuffer('string'), bufferMaxSizeMsg);
57+
assert.throws(() => SlowBuffer(Infinity), bufferMaxSizeMsg);
58+
assert.throws(() => SlowBuffer(-1), bufferMaxSizeMsg);
59+
assert.throws(() => SlowBuffer(buffer.kMaxLength + 1), bufferMaxSizeMsg);

0 commit comments

Comments
 (0)