Skip to content

Commit

Permalink
buffer: fix fill with encoding in Buffer.alloc()
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
not-an-aardvark authored and MylesBorins committed Oct 26, 2016
1 parent 1230062 commit 408a585
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
18 changes: 13 additions & 5 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,21 +91,29 @@ Object.setPrototypeOf(Buffer, Uint8Array);
/**
* Creates a new filled Buffer instance.
* alloc(size[, fill[, encoding]])
*
* Only pay attention to encoding if it's a string. This
* prevents accidentally sending in a number that would
* be interpreted as a start offset.
* Also, don't apply encoding if fill is a number.
*
* These comments are placed before the function to keep the text length
* down, to ensure that it remains inlineable by V8.
**/
Buffer.alloc = function(size, fill, encoding) {
if (typeof size !== 'number')
throw new TypeError('"size" argument must be a number');
if (size <= 0)
return createBuffer(size);
if (fill !== undefined) {
// Only pay attention to encoding if it's a string. This
// prevents accidentally sending in a number that would
// be interpretted as a start offset.
// Also, don't apply encoding if fill is a number.
if (typeof fill !== 'number' && typeof encoding === 'string')
fill = Buffer.from(fill, encoding);

return createBuffer(size, true).fill(fill);
const buf = createBuffer(size, true);
// Buffer.prototype.fill does not support filling with other buffers in v4.
// Instead, call binding.fill directly.
binding.fill(buf, fill, 0, buf.length);
return buf;
}
return createBuffer(size);
};
Expand Down
13 changes: 13 additions & 0 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,19 @@ assert.throws(function() {
Buffer.allocUnsafe(0xFFFFFFFFF);
}, RangeError);

assert(Buffer.alloc.toString().length < 600, 'Buffer.alloc is not inlineable');

// https://github.com/nodejs/node/issues/9226
{
const buf = Buffer.alloc(4, 'YQ==', 'base64');
const expectedBuf = Buffer.from([97, 97, 97, 97]);
assert(buf.equals(expectedBuf));
}
{
const buf = Buffer.alloc(4, 'ab', 'ascii');
const expectedBuf = Buffer.from([97, 98, 97, 98]);
assert(buf.equals(expectedBuf));
}

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

0 comments on commit 408a585

Please sign in to comment.