Skip to content

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

@vsemozhetbyt

Description

@vsemozhetbyt
  • 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().

Metadata

Metadata

Assignees

No one assigned

    Labels

    bufferIssues and PRs related to the buffer subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions