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‎

Lines changed: 3 additions & 1 deletion
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-
thrownewTypeError('list argument must be an Array of Buffers.');
222+
thrownewTypeError('list argument must be an Array of Buffers');
223223

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

‎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()===(newArray(10+1).join('asdf')));
2121
assert(flatLongLen.toString()===(newArray(10+1).join('asdf')));
2222

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

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

0 commit comments

Comments
(0)