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: validate list elements in Buffer.concat #4951

Merged
merged 1 commit into from
Jan 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,8 @@ Buffer.concat = function(list, length) {
var pos = 0;
for (let i = 0; i < list.length; i++) {
var buf = list[i];
if (!Buffer.isBuffer(buf))
throw new TypeError('"list" argument must be an Array of Buffers');
buf.copy(buffer, pos);
pos += buf.length;
}
Expand Down
18 changes: 14 additions & 4 deletions test/parallel/test-buffer-concat.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,18 @@ assert(flatOne !== one[0]);
assert(flatLong.toString() === (new Array(10 + 1).join('asdf')));
assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf')));

assert.throws(function() {
Buffer.concat([42]);
}, TypeError);
assertWrongList();
assertWrongList(null);
assertWrongList(new Buffer('hello'));
assertWrongList([42]);
assertWrongList(['hello', 'world']);
assertWrongList(['hello', new Buffer('world')]);

console.log('ok');
function assertWrongList(value) {
assert.throws(function() {
Buffer.concat(value);
}, function(err) {
return err instanceof TypeError &&
err.message === '"list" argument must be an Array of Buffers';
});
}