Skip to content

Commit d2dc234

Browse files
committed
buffer: validate list elements in Buffer.concat
Without this change, if any of the elements in the list to be concatenated is not a Buffer instance, the method fails with "buf.copy is not a function". Make an isBuffer check before using the copy method so that we can throw with a better message. Fixes: #4949 PR-URL: #4951 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Roman Klauke <[email protected]>
1 parent de3e94b commit d2dc234

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

lib/buffer.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ Buffer.concat = function(list, length) {
241241
var pos = 0;
242242
for (let i = 0; i < list.length; i++) {
243243
var buf = list[i];
244+
if (!Buffer.isBuffer(buf))
245+
throw new TypeError('"list" argument must be an Array of Buffers');
244246
buf.copy(buffer, pos);
245247
pos += buf.length;
246248
}

test/parallel/test-buffer-concat.js

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,18 @@ assert(flatOne !== one[0]);
2020
assert(flatLong.toString() === (new Array(10 + 1).join('asdf')));
2121
assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf')));
2222

23-
assert.throws(function() {
24-
Buffer.concat([42]);
25-
}, TypeError);
23+
assertWrongList();
24+
assertWrongList(null);
25+
assertWrongList(new Buffer('hello'));
26+
assertWrongList([42]);
27+
assertWrongList(['hello', 'world']);
28+
assertWrongList(['hello', new Buffer('world')]);
2629

27-
console.log('ok');
30+
function assertWrongList(value) {
31+
assert.throws(function() {
32+
Buffer.concat(value);
33+
}, function(err) {
34+
return err instanceof TypeError &&
35+
err.message === '"list" argument must be an Array of Buffers';
36+
});
37+
}

0 commit comments

Comments
 (0)