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

byteOffset argument in buffer indexOf / lastIndexOf has some contradictions in code vs doc vs test #9801

Closed
vsemozhetbyt opened this issue Nov 26, 2016 · 12 comments
Labels
buffer Issues and PRs related to the buffer subsystem.

Comments

@vsemozhetbyt
Copy link
Contributor

vsemozhetbyt commented Nov 26, 2016

  • Version: 7.2.0
  • Platform: Windows 7
  • Subsystem: buffer

Some contradictions in status quo:

  1. buffer.lastIndexOf() code example in doc:
const utf16Buffer = Buffer.from('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2');

// Prints: 6
console.log(utf16Buffer.lastIndexOf('\u03a3', null, 'ucs2'));

Actually, it prints -1 now.

  1. buffer.js coerces byteOffset null to 0. However, in the next block it checks if byteOffset is null to make it default byteOffset if so.

  2. test-buffer-indexof.js expects null byteOffset to return -1, i.e. it expects null byteOffset not to be converted into the default byteOffset.

Maybe the fix steps could be these:

  1. buffer.js should not coerce null to Number.
  2. test-buffer-indexof.js should expect null byteOffset to be converted into the default byteOffset.
  3. Doc should clarify which argument types and values trigger default. Maybe something like position remarks in the fs doc for fs.read() and fs.write().
@vsemozhetbyt
Copy link
Contributor Author

It seems this big PR concerns all points.

@mscdex mscdex added the buffer Issues and PRs related to the buffer subsystem. label Nov 26, 2016
@silverwind
Copy link
Contributor

I agree, this certainly looks like a bug. A offset of null and undefined should result in the same behaviour. Wanna file a PR to fix it in both code and tests?

@vsemozhetbyt
Copy link
Contributor Author

vsemozhetbyt commented Dec 4, 2016

@silverwind #4846 was a big complex PR. Maybe the author wants to do this? This way all the method system will be taken into account. ping @dcposch

@vsemozhetbyt
Copy link
Contributor Author

Maybe cc #4846 reviewers? @jasnell, @trevnorris.

@dcposch
Copy link
Contributor

dcposch commented Dec 7, 2016

@vsemozhetbyt @silverwind I have time to look at this today. PR coming soon!

@dcposch
Copy link
Contributor

dcposch commented Dec 7, 2016

So it looks like I did that in order to match the behavior of String:

var s = "abcdef"
var b = new Buffer("abcdef")
s.indexOf('b', null) // prints 1
b.indexOf('b', null) // prints 1
s.lastIndexOf('b', null) // prints -1
b.lastIndexOf('b', null) // prints -1

...in other words, both String and Buffer coerce an offset of null to zero.

Introducing inconsistency between String and Buffer seems bad. I'll send a PR that fixes the documentation and makes the tests more explicit, while leaving the behavior as is. Thoughts?

@dcposch
Copy link
Contributor

dcposch commented Dec 7, 2016

After a bit more investigating, it looks like String and Buffer both coerce the offset argument to a number, then use the default offset if the result is NaN. (Using the default offset means searching the whole String or Buffer.)

The bad news is that this leads to some really weird, unintuitive behavior. The good news is that at least they're consistent with each other and with Javascript's unary + operator:

var s = "abcdef"
var b = new Buffer("abcdef")

console.log('OFFSETS THAT COERCE TO NaN')
console.log(+{}) // prints NaN, so this will turn into the default offset
console.log(+undefined) // same here
// The following statements all print 1, searching the whole String or Buffer
s.lastIndexOf('b')
s.lastIndexOf('b', undefined)
s.lastIndexOf('b', {})
b.lastIndexOf('b')
b.lastIndexOf('b', undefined)
b.lastIndexOf('b', {})

console.log('OFFSETS THAT COERCE TO 0')
console.log(+null) // print 0
console.log(+[]) // same here
// The following statements all print -1, because they search from offset 0
s.lastIndexOf('b', null)
s.lastIndexOf('b', [])
b.lastIndexOf('b', null)
b.lastIndexOf('b', [])

@dcposch
Copy link
Contributor

dcposch commented Dec 7, 2016

Looks like my mistake in the docs was fixed here: 6050bbe

Thanks @vsemozhetbyt

@vsemozhetbyt
Copy link
Contributor Author

vsemozhetbyt commented Dec 7, 2016

So, if this is the desired behavior, this comment needs to be fixed:

https://github.com/nodejs/node/blob/master/lib/buffer.js#L593

@trevnorris
Copy link
Contributor

@vsemozhetbyt good catch. mind commenting directly on that line of the commit that the comment should be removed?

@vsemozhetbyt
Copy link
Contributor Author

@trevnorris If I get it right, it seems the comment has been changed already.

@trevnorris
Copy link
Contributor

@vsemozhetbyt thanks for pointing that out.

silverwind pushed a commit that referenced this issue Jan 24, 2017
PR-URL: #10162
Fixes: #9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
silverwind pushed a commit that referenced this issue Jan 24, 2017
PR-URL: #10162
Fixes: #9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
targos pushed a commit that referenced this issue Jan 28, 2017
Test type coercion for non-number offset arguments. Verify that Buffer
and String behave the same way.

PR-URL: #10162
Fixes: #9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
targos pushed a commit that referenced this issue Jan 28, 2017
PR-URL: #10162
Fixes: #9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
targos pushed a commit that referenced this issue Jan 28, 2017
PR-URL: #10162
Fixes: #9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this issue Jan 30, 2017
Test type coercion for non-number offset arguments. Verify that Buffer
and String behave the same way.

PR-URL: nodejs#10162
Fixes: nodejs#9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this issue Jan 30, 2017
PR-URL: nodejs#10162
Fixes: nodejs#9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this issue Jan 30, 2017
PR-URL: nodejs#10162
Fixes: nodejs#9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this issue Jan 30, 2017
Test type coercion for non-number offset arguments. Verify that Buffer
and String behave the same way.

PR-URL: nodejs#10162
Fixes: nodejs#9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this issue Jan 30, 2017
PR-URL: nodejs#10162
Fixes: nodejs#9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
italoacasas pushed a commit to italoacasas/node that referenced this issue Jan 30, 2017
PR-URL: nodejs#10162
Fixes: nodejs#9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
jasnell pushed a commit that referenced this issue Mar 8, 2017
Test type coercion for non-number offset arguments. Verify that Buffer
and String behave the same way.

PR-URL: #10162
Fixes: #9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
jasnell pushed a commit that referenced this issue Mar 8, 2017
PR-URL: #10162
Fixes: #9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
jasnell pushed a commit that referenced this issue Mar 8, 2017
PR-URL: #10162
Fixes: #9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
MylesBorins pushed a commit that referenced this issue Mar 9, 2017
Test type coercion for non-number offset arguments. Verify that Buffer
and String behave the same way.

PR-URL: #10162
Fixes: #9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
MylesBorins pushed a commit that referenced this issue Mar 9, 2017
PR-URL: #10162
Fixes: #9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
MylesBorins pushed a commit that referenced this issue Mar 9, 2017
PR-URL: #10162
Fixes: #9801
Reviewed-By: Roman Reiss <me@silverwind.io>
Reviewed-By: Stephen Belanger <admin@stephenbelanger.com>
Reviewed-By: Trevor Norris <trev.norris@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@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

5 participants