From a16103143d39d4a737d719cc3fda60a0453fc577 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 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/nodejs/node/pull/4514 Reviewed-By: Rod Vagg Reviewed-By: Vladimir Kurchatkin Reviewed-By: James M Snell Reviewed-By: Trevor Norris Reviewed-By: Сковорода Никита Андреевич --- 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'); +});