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

v8: fix offsets for TypedArray deserialization #12143

Closed
wants to merge 2 commits into from

Conversation

addaleax
Copy link
Member

Fix the offset calculation for deserializing TypedArrays that are
not aligned in their original buffer.

Since byteOffset refers to the offset into the source Buffer
instance, not its underlying ArrayBuffer, that is what should
be passed to buffer.copy.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines
Affected core subsystem(s)

v8

Fix the offset calculation for deserializing TypedArrays that are
not aligned in their original buffer.

Since `byteOffset` refers to the offset into the source `Buffer`
instance, not its underlying `ArrayBuffer`, that is what should
be passed to `buffer.copy`.
@addaleax addaleax added dont-land-on-v4.x v8 engine Issues and PRs related to the V8 dependency. labels Mar 31, 2017
{
// Unaligned Uint16Array read, with padding in the underlying array buffer.
const buf = Buffer.from('0'.repeat(64) + 'ff0d5c0404addeefbe', 'hex')
.slice(32);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the idea here to create a buffer with a byteOffset > 0? An IMO more reliable and future-proof way is this:

let buf = Buffer.alloc(32 + 9);  // Hope I counted the bytes right!
buf.write('0'.repeat(64) + 'ff0d5c0404addeefbe', 'hex');
buf = buf.slice(32);
assert.strictEqual(buf.byteOffset, 32);  // Deterministic byte offset, unlike Buffer.from().slice()
// ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bnoordhuis Yes, that’s the idea. I’ve updated the test with a variant of your suggestion

@addaleax
Copy link
Member Author

@Fishrock123
Copy link
Contributor

OS X turned this up, not sure if it's related or not.

not ok 1413 sequential/test-fs-readfile-tostring-fail
  ---
  duration_ms: 62.133
  severity: fail
  stack: |-
    timeout
  ...

@bnoordhuis
Copy link
Member

I think the V8 upgrade has made that test flaky. I've seen it fail with other pull requests too.

@addaleax
Copy link
Member Author

addaleax commented Apr 3, 2017

Landed in 33a19b4

@addaleax addaleax closed this Apr 3, 2017
@addaleax addaleax deleted the v8-serdes-unaligned-oob branch April 3, 2017 08:41
addaleax added a commit that referenced this pull request Apr 3, 2017
Fix the offset calculation for deserializing TypedArrays that are
not aligned in their original buffer.

Since `byteOffset` refers to the offset into the source `Buffer`
instance, not its underlying `ArrayBuffer`, that is what should
be passed to `buffer.copy`.

PR-URL: #12143
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
@targos
Copy link
Member

targos commented Apr 3, 2017

The test does not pass on some (big endian?) platforms: https://ci.nodejs.org/job/node-test-commit/8806/

@addaleax
Copy link
Member Author

addaleax commented Apr 3, 2017

@targos eep, I should not have missed that … PR coming in a second

addaleax added a commit to addaleax/node that referenced this pull request Apr 3, 2017
addaleax added a commit that referenced this pull request Apr 3, 2017
Ref: #12143 (comment)
PR-URL: #12186
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
@jasnell jasnell mentioned this pull request Apr 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
v8 engine Issues and PRs related to the V8 dependency.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants