diff --git a/lib/buffer.js b/lib/buffer.js index 9bc9090697..00be048d4a 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 0000000000..2102c1f9a7 --- /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'); +});