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

Merged
merged 4 commits into from Feb 5, 2017

Conversation

Projects
None yet
8 participants
@seishun
Member

seishun commented Feb 2, 2017

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
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

buffer

buffer: stricter argument checking in toString
This prevents the confusing behavior of `buf.toString(0, 5)` by
disallowing passing `0` as the encoding.
@addaleax

This comment has been minimized.

Show comment
Hide comment
@addaleax

addaleax Feb 2, 2017

Member

Should we consider this semver-major?

Member

addaleax commented Feb 2, 2017

Should we consider this semver-major?

@joyeecheung

Mind adding a test for it?

@cjihrig

This comment has been minimized.

Show comment
Hide comment
@cjihrig

cjihrig Feb 2, 2017

Contributor

Should we consider this semver-major?

To be safe, I think so.

Contributor

cjihrig commented Feb 2, 2017

Should we consider this semver-major?

To be safe, I think so.

@cjihrig

cjihrig approved these changes Feb 2, 2017

LGTM with a test.

@seishun

This comment has been minimized.

Show comment
Hide comment
@seishun

seishun Feb 2, 2017

Member

Should we consider this semver-major?

Considering the docs only mention undefined, this boils down to whether anyone in the ecosystem passes any other falsy values to toString.

Mind adding a test for it?

Do you think there's an existing test file where I could add it, or does it belong in a new file?

Member

seishun commented Feb 2, 2017

Should we consider this semver-major?

Considering the docs only mention undefined, this boils down to whether anyone in the ecosystem passes any other falsy values to toString.

Mind adding a test for it?

Do you think there's an existing test file where I could add it, or does it belong in a new file?

@joyeecheung

This comment has been minimized.

Show comment
Hide comment
@joyeecheung

joyeecheung Feb 2, 2017

Member

@seishun Probably test-buffer-tostring-range.js?

EDIT: that's a existing one.

Member

joyeecheung commented Feb 2, 2017

@seishun Probably test-buffer-tostring-range.js?

EDIT: that's a existing one.

@jasnell

jasnell approved these changes Feb 2, 2017

@seishun

This comment has been minimized.

Show comment
Hide comment
@seishun

seishun Feb 2, 2017

Member

@joyeecheung added a test, PTAL

Member

seishun commented Feb 2, 2017

@joyeecheung added a test, PTAL

@thefourtheye

This comment has been minimized.

Show comment
Hide comment
@thefourtheye

thefourtheye Feb 2, 2017

Contributor

I would love to see the same change in lib/internal/util.js's normalizeEncoding as well.

Contributor

thefourtheye commented Feb 2, 2017

I would love to see the same change in lib/internal/util.js's normalizeEncoding as well.

@seishun

This comment has been minimized.

Show comment
Hide comment
@seishun

seishun Feb 2, 2017

Member

@thefourtheye what public API would it affect?

Member

seishun commented Feb 2, 2017

@thefourtheye what public API would it affect?

@joyeecheung

This comment has been minimized.

Show comment
Hide comment
@joyeecheung

joyeecheung Feb 2, 2017

Member

@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

Member

joyeecheung commented Feb 2, 2017

@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

@thefourtheye

This comment has been minimized.

Show comment
Hide comment
@thefourtheye

thefourtheye Feb 2, 2017

Contributor

@seishun I could find only the following in a quick scan.

  • Buffer.isEncoding
  • Buffer.fill
  • Buffer.transcode
  • StringDecoder
Contributor

thefourtheye commented Feb 2, 2017

@seishun I could find only the following in a quick scan.

  • Buffer.isEncoding
  • Buffer.fill
  • Buffer.transcode
  • StringDecoder
@seishun

This comment has been minimized.

Show comment
Hide comment
@seishun

seishun Feb 2, 2017

Member

@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.

Member

seishun commented Feb 2, 2017

@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.

@lpinca

lpinca approved these changes Feb 4, 2017

@seishun seishun merged commit 9a0829d into nodejs:master Feb 5, 2017

9 of 11 checks passed

test/arm tests failed!
Details
test/osx tests failed!
Details
linter no errors found
Details
test/aix all tests passed
Details
test/freebsd all tests passed
Details
test/linux all tests passed
Details
test/linux-fips all tests passed
Details
test/linux-one all tests passed
Details
test/ppc-linux all tests passed
Details
test/smartos all tests passed
Details
test/windows-fanned all tests passed
Details

@seishun seishun deleted the seishun:buffer-encoding-0 branch Feb 10, 2017

KryDos added a commit to KryDos/node that referenced this pull request Feb 25, 2017

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: 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment