Buffer's indexOf does not seem to handle numbers correctly #7591
Closed
Description
- Version: v4.4.7
- Platform: Linux hostname 3.13.0-91-generic src: redo unaligned access workaround #138-Ubuntu SMP Fri Jun 24 17:00:34 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
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