Skip to content

Commit 33a19b4

Browse files
committed
v8: fix offsets for TypedArray deserialization
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 <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 56e881d commit 33a19b4

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

lib/v8.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ class DefaultDeserializer extends Deserializer {
177177
} else {
178178
// Copy to an aligned buffer first.
179179
const copy = Buffer.allocUnsafe(byteLength);
180-
bufferBinding.copy(this.buffer, copy, 0, offset, offset + byteLength);
180+
bufferBinding.copy(this.buffer, copy, 0,
181+
byteOffset, byteOffset + byteLength);
181182
return new ctor(copy.buffer,
182183
copy.byteOffset,
183184
byteLength / BYTES_PER_ELEMENT);

test/parallel/test-v8-serdes.js

+9
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,12 @@ const objects = [
118118
assert.deepStrictEqual(buf, ser.releaseBuffer());
119119
assert.strictEqual(des.getWireFormatVersion(), 0x0d);
120120
}
121+
122+
{
123+
// Unaligned Uint16Array read, with padding in the underlying array buffer.
124+
let buf = Buffer.alloc(32 + 9);
125+
buf.write('ff0d5c0404addeefbe', 32, 'hex');
126+
buf = buf.slice(32);
127+
assert.deepStrictEqual(v8.deserialize(buf),
128+
new Uint16Array([0xdead, 0xbeef]));
129+
}

0 commit comments

Comments
 (0)