Skip to content

Commit 556517d

Browse files
authored
Merge pull request #15 from tisztamo/master
Fixed float deserialization when TypedArray.byteOffset is nonzero
2 parents 55d25c6 + a1afd3b commit 556517d

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

msgpack-tests.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ function test() {
1212
testData(new Date(0xfffffffff * 1000 + 50));
1313
testData(new Date(-1));
1414
testData(new Uint8Array([1, 2, 3, 200]));
15+
testByteOffset()
1516
logLine("<b>Expected to fail:</b>");
1617
testData(new Uint32Array([1, 2, 3, 200000]));
1718
}
@@ -95,6 +96,27 @@ function testData(data) {
9596
logLine();
9697
}
9798

99+
function testByteOffset() {
100+
logLine("<span>Testing Typed Array byteOffset handling</span>");
101+
const buf = new ArrayBuffer(10);
102+
const arr0 = new Uint8Array(buf);
103+
const arr1 = new Uint8Array(buf, 1);
104+
for (let i = 0; i < arr0.length; i++) {
105+
arr0[i] = 255;
106+
}
107+
for (let i = 0; i < arr1.length; i++) {
108+
arr1[i] = 0;
109+
}
110+
arr1[0] = 0xcb;
111+
if (msgpack.deserialize(arr1) === 0) {
112+
logLine("<span style='color: green;'>Test passed.</span>");
113+
}
114+
else {
115+
logLine("<span style='color: red;'>Test FAILED.</span>");
116+
}
117+
logLine();
118+
}
119+
98120
// Runs several functions in a loop to measure the time spent.
99121
function benchmark() {
100122
// Define the data to test with

msgpack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@
331331
}
332332

333333
function readFloat(size) {
334-
let view = new DataView(array.buffer, pos, size);
334+
let view = new DataView(array.buffer, pos + array.byteOffset, size);
335335
pos += size;
336336
if (size === 4)
337337
return view.getFloat32(0, false);

0 commit comments

Comments
 (0)