| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
This prevents the confusing behavior of `buf.toString(0, 5)` by disallowing passing `0` as the encoding.
|
Should we consider this semver-major? |
Sorry, something went wrong.
There was a problem hiding this comment.
Mind adding a test for it?
Sorry, something went wrong.
To be safe, I think so. |
Sorry, something went wrong.
There was a problem hiding this comment.
LGTM with a test.
Sorry, something went wrong.
Considering the docs only mention undefined, this boils down to whether anyone in the ecosystem passes any other falsy values to toString.
Do you think there's an existing test file where I could add it, or does it belong in a new file? |
Sorry, something went wrong.
|
@seishun Probably test-buffer-tostring-range.js? EDIT: that's a existing one. |
Sorry, something went wrong.
|
@joyeecheung added a test, PTAL |
Sorry, something went wrong.
|
I would love to see the same change in lib/internal/util.js's normalizeEncoding as well. |
Sorry, something went wrong.
| }}), 'abc'); | ||
|
|
||
| // try toString() with 0 as the encoding | ||
| assert.throws(() => rangeBuffer.toString(0, 1, 2), /Unknown encoding/); |
There was a problem hiding this comment.
Can you improve the regular expression to something like /^TypeError: Unknown encoding: 0$/.
Sorry, something went wrong.
There was a problem hiding this comment.
Can you add a similar test for null, minimally.
Sorry, something went wrong.
| }}), 'abc'); | ||
|
|
||
| // try toString() with 0 as the encoding | ||
| assert.throws(() => rangeBuffer.toString(0, 1, 2), /Unknown encoding/); |
There was a problem hiding this comment.
Better include the complete message, like ^TypeError: Unknown encoding: undefined$.
Sorry, something went wrong.
|
@thefourtheye what public API would it affect? |
Sorry, something went wrong.
|
@thefourtheye normalizeEncoding does coerce false and '' to utf8, not sure would anybody in the userland really pass those kinds of falsy values into the buffer methods that calls it though. Nonetheless we could at least add a test in test-internal-util-normalizeencoding.js |
Sorry, something went wrong.
|
@seishun I could find only the following in a quick scan.
|
Sorry, something went wrong.
|
@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. |
Sorry, something went wrong.
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>
| Back | FazBrowse Home | New Git URL |
Currently, Buffer.from('hello').toString(0, 1) returns ello, which is confusing. On the other hand, Buffer.from('hello').toString(1, 2) throws an expected error. This PR disallows passing anything other than undefined or a valid encoding as encoding.
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
Affected core subsystem(s)
buffer