Skip to content

Commit

Permalink
fix(cbor): incorrect decoding with subarrays
Browse files Browse the repository at this point in the history
  • Loading branch information
BlackAsLight committed Jan 12, 2025
1 parent 2604b37 commit 2ebfa11
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 7 deletions.
14 changes: 7 additions & 7 deletions cbor/_common_decode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ function calcLength(
const view = new DataView(input.buffer);
switch (aI) {
case 24:
return [view.getUint8(offset), offset + 1];
return [view.getUint8(offset + input.byteOffset), offset + 1];
case 25:
return [view.getUint16(offset), offset + 2];
return [view.getUint16(offset + input.byteOffset), offset + 2];
case 26:
return [view.getUint32(offset), offset + 4];
return [view.getUint32(offset + input.byteOffset), offset + 4];
default: // Only possible for it to be 27
return [view.getBigUint64(offset), offset + 8];
return [view.getBigUint64(offset + input.byteOffset), offset + 8];
}
}

Expand Down Expand Up @@ -315,11 +315,11 @@ function decodeSeven(
const view = new DataView(input.buffer);
switch (aI) {
case 25:
return [view.getFloat16(offset), offset + 2];
return [view.getFloat16(offset + input.byteOffset), offset + 2];

Check warning on line 318 in cbor/_common_decode.ts

View check run for this annotation

Codecov / codecov/patch

cbor/_common_decode.ts#L318

Added line #L318 was not covered by tests
case 26:
return [view.getFloat32(offset), offset + 4];
return [view.getFloat32(offset + input.byteOffset), offset + 4];

Check warning on line 320 in cbor/_common_decode.ts

View check run for this annotation

Codecov / codecov/patch

cbor/_common_decode.ts#L320

Added line #L320 was not covered by tests
case 27:
return [view.getFloat64(offset), offset + 8];
return [view.getFloat64(offset + input.byteOffset), offset + 8];
default:
throw new RangeError(
`Cannot decode value (0b111_${aI.toString(2).padStart(5, "0")})`,
Expand Down
32 changes: 32 additions & 0 deletions cbor/decode_cbor_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,3 +814,35 @@ Deno.test("decodeCbor() rejecting tagNumber 259 due to invalid indefinite length
"More bytes were expected",
);
});

Deno.test("decodeCbor() correctly decoding with subarrays", () => {
let encodedData = encodeCbor(0);
let buffer = new Uint8Array(encodedData.length + 7);
buffer.set(encodedData, 7);
assertEquals(decodeCbor(buffer.subarray(7)), 0);

encodedData = encodeCbor(24);
buffer = new Uint8Array(encodedData.length + 7);
buffer.set(encodedData, 7);
assertEquals(decodeCbor(buffer.subarray(7)), 24);

encodedData = encodeCbor(2 ** 8);
buffer = new Uint8Array(encodedData.length + 7);
buffer.set(encodedData, 7);
assertEquals(decodeCbor(buffer.subarray(7)), 2 ** 8);

encodedData = encodeCbor(2 ** 16);
buffer = new Uint8Array(encodedData.length + 7);
buffer.set(encodedData, 7);
assertEquals(decodeCbor(buffer.subarray(7)), 2 ** 16);

encodedData = encodeCbor(2 ** 32);
buffer = new Uint8Array(encodedData.length + 7);
buffer.set(encodedData, 7);
assertEquals(decodeCbor(buffer.subarray(7)), 2n ** 32n);

encodedData = encodeCbor(3.14);
buffer = new Uint8Array(encodedData.length + 7);
buffer.set(encodedData, 7);
assertEquals(decodeCbor(buffer.subarray(7)), 3.14);
});

0 comments on commit 2ebfa11

Please sign in to comment.