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: https://github.com/nodejs/node/issues/4949 PR-URL: https://github.com/nodejs/node/pull/4951 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com> Reviewed-By: Roman Klauke <romaaan.git@gmail.com>
This commit is contained in:
parent
de3e94b0ef
commit
d2dc234def
@ -241,6 +241,8 @@ Buffer.concat = function(list, length) {
|
|||||||
var pos = 0;
|
var pos = 0;
|
||||||
for (let i = 0; i < list.length; i++) {
|
for (let i = 0; i < list.length; i++) {
|
||||||
var buf = list[i];
|
var buf = list[i];
|
||||||
|
if (!Buffer.isBuffer(buf))
|
||||||
|
throw new TypeError('"list" argument must be an Array of Buffers');
|
||||||
buf.copy(buffer, pos);
|
buf.copy(buffer, pos);
|
||||||
pos += buf.length;
|
pos += buf.length;
|
||||||
}
|
}
|
||||||
|
@ -20,8 +20,18 @@ assert(flatOne !== one[0]);
|
|||||||
assert(flatLong.toString() === (new Array(10 + 1).join('asdf')));
|
assert(flatLong.toString() === (new Array(10 + 1).join('asdf')));
|
||||||
assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf')));
|
assert(flatLongLen.toString() === (new Array(10 + 1).join('asdf')));
|
||||||
|
|
||||||
assert.throws(function() {
|
assertWrongList();
|
||||||
Buffer.concat([42]);
|
assertWrongList(null);
|
||||||
}, TypeError);
|
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';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user