Skip to content

Commit 9a0829d

Browse files
authored
buffer: stricter argument checking in toString
This prevents the confusing behavior of `buf.toString(0, 5)` by disallowing passing `0` as the encoding. PR-URL: #11120 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Sakthipriyan Vairamani <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 67af1ad commit 9a0829d

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/buffer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ function slowToString(encoding, start, end) {
463463
if (end <= start)
464464
return '';
465465

466-
if (!encoding) encoding = 'utf8';
466+
if (encoding === undefined) encoding = 'utf8';
467467

468468
while (true) {
469469
switch (encoding) {

test/parallel/test-buffer-tostring-range.js

+8
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,11 @@ assert.strictEqual(rangeBuffer.toString('ascii', 0, true), 'a');
8282
assert.strictEqual(rangeBuffer.toString({toString: function() {
8383
return 'ascii';
8484
}}), 'abc');
85+
86+
// try toString() with 0 and null as the encoding
87+
assert.throws(() => {
88+
rangeBuffer.toString(0, 1, 2);
89+
}, /^TypeError: Unknown encoding: 0$/);
90+
assert.throws(() => {
91+
rangeBuffer.toString(null, 1, 2);
92+
}, /^TypeError: Unknown encoding: null$/);

0 commit comments

Comments
 (0)