Skip to content

Commit

Permalink
buffer: throw if both length and enc are passed
Browse files Browse the repository at this point in the history
PR-URL: nodejs#4514
Reviewed-By: Rod Vagg <[email protected]>
Reviewed-By: Vladimir Kurchatkin <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Trevor Norris <[email protected]>
Reviewed-By: Сковорода Никита Андреевич <[email protected]>
  • Loading branch information
mafintosh authored and Michael Scovetta committed Apr 2, 2016
1 parent eae1dac commit a161031
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
5 changes: 5 additions & 0 deletions lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions test/sequential/test-buffer-bad-overload.js
Original file line number Diff line number Diff line change
@@ -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');
});

0 comments on commit a161031

Please sign in to comment.