Skip to content

Commit

Permalink
buffer: stricter argument checking in toString
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
seishun authored Feb 5, 2017
1 parent 67af1ad commit 9a0829d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ function slowToString(encoding, start, end) {
if (end <= start)
return '';

if (!encoding) encoding = 'utf8';
if (encoding === undefined) encoding = 'utf8';

while (true) {
switch (encoding) {
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-buffer-tostring-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,11 @@ assert.strictEqual(rangeBuffer.toString('ascii', 0, true), 'a');
assert.strictEqual(rangeBuffer.toString({toString: function() {
return 'ascii';
}}), 'abc');

// try toString() with 0 and null as the encoding
assert.throws(() => {
rangeBuffer.toString(0, 1, 2);
}, /^TypeError: Unknown encoding: 0$/);
assert.throws(() => {
rangeBuffer.toString(null, 1, 2);
}, /^TypeError: Unknown encoding: null$/);

0 comments on commit 9a0829d

Please sign in to comment.