-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
buffer: stricter argument checking in toString #11120
Conversation
This prevents the confusing behavior of `buf.toString(0, 5)` by disallowing passing `0` as the encoding.
Should we consider this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mind adding a test for it?
To be safe, I think so. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with a test.
Considering the docs only mention
Do you think there's an existing test file where I could add it, or does it belong in a new file? |
@seishun Probably test-buffer-tostring-range.js? EDIT: that's a existing one. |
@joyeecheung added a test, PTAL |
I would love to see the same change in |
@@ -82,3 +82,6 @@ assert.strictEqual(rangeBuffer.toString('ascii', 0, true), 'a'); | |||
assert.strictEqual(rangeBuffer.toString({toString: function() { | |||
return 'ascii'; | |||
}}), 'abc'); | |||
|
|||
// try toString() with 0 as the encoding | |||
assert.throws(() => rangeBuffer.toString(0, 1, 2), /Unknown encoding/); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you improve the regular expression to something like /^TypeError: Unknown encoding: 0$/
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a similar test for null
, minimally.
@@ -82,3 +82,6 @@ assert.strictEqual(rangeBuffer.toString('ascii', 0, true), 'a'); | |||
assert.strictEqual(rangeBuffer.toString({toString: function() { | |||
return 'ascii'; | |||
}}), 'abc'); | |||
|
|||
// try toString() with 0 as the encoding | |||
assert.throws(() => rangeBuffer.toString(0, 1, 2), /Unknown encoding/); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better include the complete message, like ^TypeError: Unknown encoding: undefined$
.
@thefourtheye what public API would it affect? |
@thefourtheye |
@seishun I could find only the following in a quick scan.
|
@thefourtheye only the last two are affected, the others check the type. I don't think this is a problem, there is no confusion it can cause like in this case. In any case, it belongs in a separate PR. |
This prevents the confusing behavior of `buf.toString(0, 5)` by disallowing passing `0` as the encoding. PR-URL: nodejs#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>
Currently,
Buffer.from('hello').toString(0, 1)
returnsello
, which is confusing. On the other hand,Buffer.from('hello').toString(1, 2)
throws an expected error. This PR disallows passing anything other thanundefined
or a valid encoding asencoding
.I would love to remove the check altogether thereby also disallowing
undefined
, but the documentation mentions this feature and there is a test for it, so I guess it's not happening, although I can hardly imagine anyone doing this.Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passesAffected core subsystem(s)
buffer