Skip to content

Commit 408a585

Browse files
not-an-aardvarkMylesBorins
authored andcommitted
buffer: fix fill with encoding in Buffer.alloc()
Previously, the implementation of Buffer.alloc() called Buffer#fill() with another Buffer as an argument. However, in v4.x, Buffer#fill does not support a Buffer as a parameter. As a workaround, call binding.fill() directly in the Buffer.alloc() implementation. Fixes: #9226 PR-URL: #9238 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Myles Borins <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 1230062 commit 408a585

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

lib/buffer.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -91,21 +91,29 @@ Object.setPrototypeOf(Buffer, Uint8Array);
9191
/**
9292
* Creates a new filled Buffer instance.
9393
* alloc(size[, fill[, encoding]])
94+
*
95+
* Only pay attention to encoding if it's a string. This
96+
* prevents accidentally sending in a number that would
97+
* be interpreted as a start offset.
98+
* Also, don't apply encoding if fill is a number.
99+
*
100+
* These comments are placed before the function to keep the text length
101+
* down, to ensure that it remains inlineable by V8.
94102
**/
95103
Buffer.alloc = function(size, fill, encoding) {
96104
if (typeof size !== 'number')
97105
throw new TypeError('"size" argument must be a number');
98106
if (size <= 0)
99107
return createBuffer(size);
100108
if (fill !== undefined) {
101-
// Only pay attention to encoding if it's a string. This
102-
// prevents accidentally sending in a number that would
103-
// be interpretted as a start offset.
104-
// Also, don't apply encoding if fill is a number.
105109
if (typeof fill !== 'number' && typeof encoding === 'string')
106110
fill = Buffer.from(fill, encoding);
107111

108-
return createBuffer(size, true).fill(fill);
112+
const buf = createBuffer(size, true);
113+
// Buffer.prototype.fill does not support filling with other buffers in v4.
114+
// Instead, call binding.fill directly.
115+
binding.fill(buf, fill, 0, buf.length);
116+
return buf;
109117
}
110118
return createBuffer(size);
111119
};

test/parallel/test-buffer-alloc.js

+13
Original file line numberDiff line numberDiff line change
@@ -1060,6 +1060,19 @@ assert.throws(function() {
10601060
Buffer.allocUnsafe(0xFFFFFFFFF);
10611061
}, RangeError);
10621062

1063+
assert(Buffer.alloc.toString().length < 600, 'Buffer.alloc is not inlineable');
1064+
1065+
// https://github.com/nodejs/node/issues/9226
1066+
{
1067+
const buf = Buffer.alloc(4, 'YQ==', 'base64');
1068+
const expectedBuf = Buffer.from([97, 97, 97, 97]);
1069+
assert(buf.equals(expectedBuf));
1070+
}
1071+
{
1072+
const buf = Buffer.alloc(4, 'ab', 'ascii');
1073+
const expectedBuf = Buffer.from([97, 98, 97, 98]);
1074+
assert(buf.equals(expectedBuf));
1075+
}
10631076

10641077
// attempt to overflow buffers, similar to previous bug in array buffers
10651078
assert.throws(function() {

0 commit comments

Comments
 (0)