Skip to content

Commit 58d67e2

Browse files
targosrvagg
authored andcommitted
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 5de3dc5 commit 58d67e2

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

lib/buffer.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ Buffer.isEncoding = function(encoding) {
219219

220220
Buffer.concat = function(list, length) {
221221
if (!Array.isArray(list))
222-
throw new TypeError('list argument must be an Array of Buffers.');
222+
throw new TypeError('list argument must be an Array of Buffers');
223223

224224
if (list.length === 0)
225225
return new Buffer(0);
@@ -236,6 +236,8 @@ Buffer.concat = function(list, length) {
236236
var pos = 0;
237237
for (let i = 0; i < list.length; i++) {
238238
var buf = list[i];
239+
if (!Buffer.isBuffer(buf))
240+
throw new TypeError('list argument must be an Array of Buffers');
239241
buf.copy(buffer, pos);
240242
pos += buf.length;
241243
}

test/parallel/test-buffer-concat.js

+14-4
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)