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

doc: Buffer buf.readInt32LE(offset[, noAssert]) example incorrect #5889

Closed
bendiy opened this issue Mar 24, 2016 · 2 comments
Closed

doc: Buffer buf.readInt32LE(offset[, noAssert]) example incorrect #5889

bendiy opened this issue Mar 24, 2016 · 2 comments
Labels
doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors.

Comments

@bendiy
Copy link

bendiy commented Mar 24, 2016

  • Version: 4.2.1
  • Platform: Ubuntu 14.04 64-bit
  • Subsystem: Buffer

The example for buf.readInt32LE(offset[, noAssert]) appears to be incorrect.

const buf = new Buffer([1,-2,3,4]);

buf.readInt32BE();
  // returns 33424132
buf.readInt32LE(1);
  // returns 67370497

Running locally in a REPL:

$ node
> const buf = new Buffer([1,-2,3,4]);
undefined
> buf.readInt32BE();
33424132
> buf.readInt32LE(1);
RangeError: index out of range
    at checkOffset (buffer.js:619:11)
    at Buffer.readInt32LE (buffer.js:772:5)
    at repl:1:5
    at REPLServer.defaultEval (repl.js:164:27)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:393:12)
    at emitOne (events.js:82:20)
    at REPLServer.emit (events.js:169:7)
    at REPLServer.Interface._onLine (readline.js:210:10)
>

The offset check is something like offset + 3 < buffer.length, so 4 < 4 fails as it should.

@cjihrig
Copy link
Contributor

cjihrig commented Mar 24, 2016

Looks like you're right. It should be buf.readInt32LE();

@cjihrig cjihrig added good first issue Issues that are suitable for first-time contributors. doc Issues and PRs related to the documentations. labels Mar 24, 2016
@cjihrig
Copy link
Contributor

cjihrig commented Mar 24, 2016

Also the documentation of the offset argument should probably be 0 <= offset < buf.length - 4

EDIT: Nevermind

evanlucas pushed a commit that referenced this issue Mar 30, 2016
Update example of readInt32LE method. buf.readInt32LE(1) is supposed to
throw an error as it has only four elements and it tries to read 32
bits from three bytes.

Fixes: #5889
PR-URL: #5890
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
evanlucas pushed a commit that referenced this issue Mar 31, 2016
Update example of readInt32LE method. buf.readInt32LE(1) is supposed to
throw an error as it has only four elements and it tries to read 32
bits from three bytes.

Fixes: #5889
PR-URL: #5890
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
MylesBorins pushed a commit that referenced this issue Apr 9, 2016
Update example of readInt32LE method. buf.readInt32LE(1) is supposed to
throw an error as it has only four elements and it tries to read 32
bits from three bytes.

Fixes: #5889
PR-URL: #5890
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. good first issue Issues that are suitable for first-time contributors.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants