From 9cf36a1be87810ad722569aa9a81593d1c94ba36 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 e3e2083ff9ca9c..e703c06ebf1603 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 308019eae5a08c..bfbd16117b431e 100644 --- a/test/parallel/test-buffer.js +++ b/test/parallel/test-buffer.js @@ -1179,3 +1179,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/);