From b6302926dc10613d1c18a8bada4704b2a4709f7a Mon Sep 17 00:00:00 2001 From: Mathias Buus Date: Sat, 2 Jan 2016 17:16:19 +0100 Subject: [PATCH] buffer: throw if both length and enc are passed --- lib/buffer.js | 5 +++++ test/sequential/test-buffer-bad-overload.js | 15 +++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 test/sequential/test-buffer-bad-overload.js diff --git a/lib/buffer.js b/lib/buffer.js index 9bc9090697a780..00be048d4a6e0b 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -47,6 +47,11 @@ function alignPool() { function Buffer(arg, encoding) { // Common case. if (typeof arg === 'number') { + if (typeof encoding === 'string') { + throw new Error( + 'If encoding is specified then the first argument must be a string' + ); + } // If less than zero, or NaN. if (arg < 0 || arg !== arg) arg = 0; diff --git a/test/sequential/test-buffer-bad-overload.js b/test/sequential/test-buffer-bad-overload.js new file mode 100644 index 00000000000000..2102c1f9a71d59 --- /dev/null +++ b/test/sequential/test-buffer-bad-overload.js @@ -0,0 +1,15 @@ +'use strict'; +require('../common'); +const assert = require('assert'); + +assert.doesNotThrow(function() { + new Buffer(10); +}); + +assert.throws(function() { + new Buffer(10, 'hex'); +}); + +assert.doesNotThrow(function() { + new Buffer('deadbeaf', 'hex'); +});