Browse Source

buffer: throw if both length and enc are passed

PR-URL: https://github.com/nodejs/node/pull/4514
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: Vladimir Kurchatkin <vladimir.kurchatkin@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: Сковорода Никита Андреевич <chalkerx@gmail.com>
process-exit-stdio-flushing
Mathias Buus 9 years ago
committed by James M Snell
parent
commit
3b27dd5ce1
  1. 5
      lib/buffer.js
  2. 15
      test/sequential/test-buffer-bad-overload.js

5
lib/buffer.js

@ -47,6 +47,11 @@ function alignPool() {
function Buffer(arg, encoding) { function Buffer(arg, encoding) {
// Common case. // Common case.
if (typeof arg === 'number') { 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 less than zero, or NaN.
if (arg < 0 || arg !== arg) if (arg < 0 || arg !== arg)
arg = 0; arg = 0;

15
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');
});
Loading…
Cancel
Save