Permalink
Browse files

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 <anna@addaleax.net>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information...
seishun committed Feb 5, 2017
1 parent 67af1ad commit 9a0829d7281666eafeb6b0136fad0356f031eb4a
Showing with 9 additions and 1 deletion.
  1. +1 −1 lib/buffer.js
  2. +8 −0 test/parallel/test-buffer-tostring-range.js
View
@@ -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) {
@@ -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.