Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

buffer: fix fill with encoding in Buffer.alloc() #9238

Closed
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ Buffer.alloc = function(size, fill, encoding) {
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
11 changes: 11 additions & 0 deletions test/parallel/test-buffer-alloc.js
Original file line number Diff line number Diff line change
Expand Up @@ -1060,6 +1060,17 @@ assert.throws(function() {
Buffer.allocUnsafe(0xFFFFFFFFF);
}, RangeError);

// issue GH-9226
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be better to provide a URL instead (or at least in addition).

{
const buf = Buffer.alloc(4, 'YQ==', 'base64');
const expectedBuf = new Buffer([97, 97, 97, 97]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this be Buffer.from() instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea; I had been under the impression that Buffer.from() had different behavior in v4, but it seems like that's not an issue here.

assert(buf.equals(expectedBuf));
}
{
const buf = Buffer.alloc(4, 'ab', 'ascii');
const expectedBuf = new Buffer([97, 98, 97, 98]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

assert(buf.equals(expectedBuf));
}

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