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's indexOf does not seem to handle numbers correctly #7591

Closed
Sha-Bang opened this issue Jul 7, 2016 · 3 comments
Closed

Buffer's indexOf does not seem to handle numbers correctly #7591

Sha-Bang opened this issue Jul 7, 2016 · 3 comments
Labels
buffer Issues and PRs related to the buffer subsystem.

Comments

@Sha-Bang
Copy link

Sha-Bang commented Jul 7, 2016

Node seems to behave oddly when using indexOf (or includes) on a buffer

It seems that when a string or buffer is the parameter of indexOf it returns the index of the first byte that matches it; however, when a number is passed in it returns the index of the last byte that matched it, unless there are duplicates, then it behaves very oddly.

$ node
> buf = new Buffer('this is a test');
 <Buffer 74 68 69 73 20 69 73 20 61 20 74 65 73 74>
> buf.indexOf('is') // pass
2
> buf.indexOf(new Buffer('is')) // pass
2
> buf.indexOf(new Buffer('istest')) // pass
-1
> buf.indexOf('istest') // pass
-1
> buf.indexOf(0x6973) // odd 
3
> buf.indexOf(0x697320) // appears to return the index of the last byte that matched the number given
4
> buf.indexOf(0x69732069) // unless there are duplicates, then it does weird things
2
> buf.indexOf(0x697374657374) 
0
> buf.indexOf(0x69737374)
0
> buf.indexOf(0x69737465)
11
@addaleax addaleax added the buffer Issues and PRs related to the buffer subsystem. label Jul 7, 2016
@addaleax
Copy link
Member

addaleax commented Jul 7, 2016

I think this is the intended behaviour, because .indexOf() converts the argument to an 8-bit integer, i.e. it truncates 0x69737374 to 0x74.

@jasnell
Copy link
Member

jasnell commented Jul 7, 2016

@nodejs/buffer

@jasnell
Copy link
Member

jasnell commented Jul 7, 2016

Yes, this seems to be the correct behavior

addaleax added a commit to addaleax/node that referenced this issue Jul 8, 2016
Hopefully clarify the behaviour of `buffer.indexOf()` and
`buffer.includes()` for numbers in that they will be
truncated to uint8s.

Add tests for that behaviour.

Fixes: nodejs#7591
evanlucas pushed a commit that referenced this issue Jul 15, 2016
Hopefully clarify the behaviour of `buffer.indexOf()` and
`buffer.includes()` for numbers in that they will be
truncated to uint8s.

Add tests for that behaviour.

Fixes: #7591
PR-URL: #7611
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
evanlucas pushed a commit that referenced this issue Jul 20, 2016
Hopefully clarify the behaviour of `buffer.indexOf()` and
`buffer.includes()` for numbers in that they will be
truncated to uint8s.

Add tests for that behaviour.

Fixes: #7591
PR-URL: #7611
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
addaleax added a commit to addaleax/node that referenced this issue Sep 9, 2016
Hopefully clarify the behaviour of `buffer.indexOf()` and
`buffer.includes()` for numbers in that they will be
truncated to uint8s.

Add tests for that behaviour.

Fixes: nodejs#7591
PR-URL: nodejs#7611
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
MylesBorins pushed a commit that referenced this issue Sep 9, 2016
Hopefully clarify the behaviour of `buffer.indexOf()` and
`buffer.includes()` for numbers in that they will be
truncated to uint8s.

Add tests for that behaviour.

Fixes: #7591
PR-URL: #7611
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
MylesBorins pushed a commit that referenced this issue Sep 28, 2016
Hopefully clarify the behaviour of `buffer.indexOf()` and
`buffer.includes()` for numbers in that they will be
truncated to uint8s.

Add tests for that behaviour.

Fixes: #7591
PR-URL: #7611
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
rvagg pushed a commit that referenced this issue Oct 18, 2016
Hopefully clarify the behaviour of `buffer.indexOf()` and
`buffer.includes()` for numbers in that they will be
truncated to uint8s.

Add tests for that behaviour.

Fixes: #7591
PR-URL: #7611
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
MylesBorins pushed a commit that referenced this issue Oct 26, 2016
Hopefully clarify the behaviour of `buffer.indexOf()` and
`buffer.includes()` for numbers in that they will be
truncated to uint8s.

Add tests for that behaviour.

Fixes: #7591
PR-URL: #7611
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
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

3 participants