Skip to content
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: inconsistencies in readIntBE() and readIntLE() #10515

Closed
Trott opened this issue Dec 29, 2016 · 8 comments
Closed

buffer: inconsistencies in readIntBE() and readIntLE() #10515

Trott opened this issue Dec 29, 2016 · 8 comments
Labels
buffer Issues and PRs related to the buffer subsystem.

Comments

@Trott
Copy link
Member

Trott commented Dec 29, 2016

  • Version: 7.3.0
  • Platform: OS X 10.12.2
  • Subsystem: buffer

The documentation for readIntLE() and readIntBE() both indicate that the second argument (byteLength) is required. However, both functions will seem to operate without a byteLength provided. Unfortunately, byteLength will default to 0 in that case, which results in unpredictable behavior:

> var buf = Buffer.from([126, 127, 128, 129, 130])
undefined
> buf.readIntLE(0)
126
> buf.readIntBE(0)
undefined
> buf.readIntBE(4)
128
> buf.readIntLE(4)
129
>

Perhaps we should do one of the following?:

  • have byteLength default to 1 rather than 0
  • throw an error if byteLength is not provided
  • warn in the documentation that if you fail to provide byteLength, then you will get a result but there are no guarantees even about the return value type
@Trott Trott added the buffer Issues and PRs related to the buffer subsystem. label Dec 29, 2016
@Trott
Copy link
Member Author

Trott commented Dec 29, 2016

/cc @nodejs/buffer

@cjihrig
Copy link
Contributor

cjihrig commented Dec 29, 2016

If a value is required, but not provided, we should throw.

@sam-github
Copy link
Contributor

The behaviour of signed int reads should be consistent with the other read functions, what do the unsigned variants do? I just tried, and they seem to have an offset that defaults to zero, which is what would I would expect if I was to guess (but not what is documented).

@sam-github
Copy link
Contributor

Also, can you check whether when used with offsets larger than the buffer size, they are allowing reads of arbitrary memory? That could be the source of what looks "unpredictable", and could have security implications.

@Trott
Copy link
Member Author

Trott commented Dec 29, 2016

@sam-github I might be misunderstanding you or expressing myself in an unclear way, but I agree that offset defaulting to 0 makes sense and could be added to the documentation.

The problematic part is the way byteLength also defaults to 0. That's not even a valid byteLength value.

@sam-github
Copy link
Contributor

Agreed.

I wish for a test for byteLength of zero, with and without noAssert, and that shows that that the readInt variants behave the same with or without noAssert. Ideally, they should throw, but if they do something else right now, that's worth writing a test for.... so that when we fix it we can doc it, change the test, and mark it semver-major.

@trevnorris
Copy link
Contributor

Looks like this issue is open and known, just need someone to write the patch + test?

@Trott
Copy link
Member Author

Trott commented Jul 15, 2017

#11146 seems really close. Head over there and help push it over the finish line?

Trott pushed a commit to seppevs/node that referenced this issue Jan 5, 2018
The 'byteLength' argument should be required and of type 'number'. It should have a value between 1 and 6.
This is a fix for issue: nodejs#10515
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem.
Projects
None yet
Development

No branches or pull requests

5 participants
@sam-github @trevnorris @Trott @cjihrig and others