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: https://github.com/nodejs/node/pull/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>
This commit is contained in:
parent
67af1ad671
commit
9a0829d728
@ -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$/);
|
||||
|
Loading…
x
Reference in New Issue
Block a user