From 76b2c40d45e7ee8d1fd0537bc5c971beaceaa65b Mon Sep 17 00:00:00 2001 From: Trevor Norris Date: Thu, 16 Jul 2015 15:19:01 -0600 Subject: [PATCH] buffer: fix missing null/undefined check The new implementation of Buffer missed the check for null/undefined as the first argument to new Buffer(). Reintroduce the check and add test. Fix: e8734c0 "buffer: implement Uint8Array backed Buffer" Fix: https://github.com/nodejs/io.js/issues/2194 Reviewed-By: Colin Ihrig Reviewed-By: Rod Vagg --- lib/buffer.js | 4 ++++ test/parallel/test-buffer.js | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/buffer.js b/lib/buffer.js index b9fe5bfb84b64a..403c344bdbe9c8 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -105,6 +105,10 @@ function fromObject(obj) { return b; } + if (obj == null) { + throw new TypeError('must start with number, buffer, array or string'); + } + if (obj instanceof ArrayBuffer) { return binding.createFromArrayBuffer(obj); } diff --git a/test/parallel/test-buffer.js b/test/parallel/test-buffer.js index 3daa02eac790f1..d5013c355289c8 100644 --- a/test/parallel/test-buffer.js +++ b/test/parallel/test-buffer.js @@ -1181,3 +1181,11 @@ Buffer.poolSize = ps; assert.throws(function() { Buffer(10).copy(); }); + +assert.throws(function() { + new Buffer(); +}, /must start with number, buffer, array or string/); + +assert.throws(function() { + new Buffer(null); +}, /must start with number, buffer, array or string/);