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
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/v8.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,8 @@ class DefaultDeserializer extends Deserializer {
} else {
// Copy to an aligned buffer first.
const copy = Buffer.allocUnsafe(byteLength);
bufferBinding.copy(this.buffer, copy, 0, offset, offset + byteLength);
bufferBinding.copy(this.buffer, copy, 0,
byteOffset, byteOffset + byteLength);
return new ctor(copy.buffer,
copy.byteOffset,
byteLength / BYTES_PER_ELEMENT);
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-v8-serdes.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,11 @@ const objects = [
assert.deepStrictEqual(buf, ser.releaseBuffer());
assert.strictEqual(des.getWireFormatVersion(), 0x0d);
}

{
// 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

assert.deepStrictEqual(v8.deserialize(buf),
new Uint16Array([0xdead, 0xbeef]));
}