Skip to content

Commit 3b27dd5

Browse files
mafintoshjasnell
authored andcommitted
buffer: throw if both length and enc are passed
PR-URL: #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]>
1 parent e51bbfd commit 3b27dd5

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Diff for: lib/buffer.js

+5
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ function alignPool() {
4747
function Buffer(arg, encoding) {
4848
// Common case.
4949
if (typeof arg === 'number') {
50+
if (typeof encoding === 'string') {
51+
throw new Error(
52+
'If encoding is specified then the first argument must be a string'
53+
);
54+
}
5055
// If less than zero, or NaN.
5156
if (arg < 0 || arg !== arg)
5257
arg = 0;

Diff for: test/sequential/test-buffer-bad-overload.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'use strict';
2+
require('../common');
3+
const assert = require('assert');
4+
5+
assert.doesNotThrow(function() {
6+
new Buffer(10);
7+
});
8+
9+
assert.throws(function() {
10+
new Buffer(10, 'hex');
11+
});
12+
13+
assert.doesNotThrow(function() {
14+
new Buffer('deadbeaf', 'hex');
15+
});

0 commit comments

Comments
 (0)