diff --git a/lib/buffer.js b/lib/buffer.js index 1035ab06d1783b..bd2c859c3ee389 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -219,7 +219,7 @@ Buffer.isEncoding = function(encoding) { Buffer.concat = function(list, length) { if (!Array.isArray(list)) - throw new TypeError('list argument must be an Array of Buffers.'); + throw new TypeError('list argument must be an Array of Buffers'); if (list.length === 0) return new Buffer(0); @@ -236,6 +236,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; } diff --git a/test/parallel/test-buffer-concat.js b/test/parallel/test-buffer-concat.js index 07f763a76dc419..527d7d69bacaba 100644 --- a/test/parallel/test-buffer-concat.js +++ b/test/parallel/test-buffer-concat.js @@ -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'; + }); +}