Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4438550

Browse filesBrowse files
addaleaxFishrock123
authored andcommitted
buffer: ignore negative allocation lengths
Treat negative length arguments to `Buffer()`/`allocUnsafe()` as if they were zero so the allocation does not affect the pool’s offset. Fixes: #7047 PR-URL: #7051 Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com> Reviewed-By: Trevor Norris <trev.norris@gmail.com> Reviewed-By: Rod Vagg <rod@vagg.org>
1 parent 29200ed commit 4438550
Copy full SHA for 4438550

File tree

Expand file treeCollapse file tree

2 files changed

+13
-2
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+13
-2
lines changed
Open diff view settings
Collapse file

‎lib/buffer.js‎

Copy file name to clipboardExpand all lines: lib/buffer.js
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ Object.setPrototypeOf(SlowBuffer, Uint8Array);
199199

200200

201201
function allocate(size) {
202-
if (size === 0) {
203-
return createBuffer(size);
202+
if (size <= 0) {
203+
return createBuffer(0);
204204
}
205205
if (size < (Buffer.poolSize >>> 1)) {
206206
if (size > (poolSize - poolOffset))
Collapse file

‎test/parallel/test-buffer.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-buffer.js
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1465,3 +1465,14 @@ assert.equal(Buffer.prototype.parent, undefined);
14651465
assert.equal(Buffer.prototype.offset, undefined);
14661466
assert.equal(SlowBuffer.prototype.parent, undefined);
14671467
assert.equal(SlowBuffer.prototype.offset, undefined);
1468+
1469+
{
1470+
// Test that large negative Buffer length inputs don't affect the pool offset.
1471+
assert.deepStrictEqual(Buffer(-Buffer.poolSize), Buffer.from(''));
1472+
assert.deepStrictEqual(Buffer(-100), Buffer.from(''));
1473+
assert.deepStrictEqual(Buffer.allocUnsafe(-Buffer.poolSize), Buffer.from(''));
1474+
assert.deepStrictEqual(Buffer.allocUnsafe(-100), Buffer.from(''));
1475+
1476+
// Check pool offset after that by trying to write string into the pool.
1477+
assert.doesNotThrow(() => Buffer.from('abc'));
1478+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.