Skip to content

Commit 29ee480

Browse files
committed
fix: correct limits for integer deserialization and add tests
1 parent cee07c2 commit 29ee480

2 files changed

Lines changed: 41 additions & 5 deletions

File tree

packages/core/src/codecs/octetstream-codec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,11 @@ export default class OctetstreamCodec implements ContentCodec {
371371
const limit = Math.pow(2, signed ? length - 1 : length) - 1;
372372
// throw error on overflow
373373
if (signed) {
374-
if (value < -limit - 1 || value >= limit) {
374+
if (value < -limit - 1 || value > limit) {
375375
throw new Error("Integer overflow when representing signed " + value + " in " + length + " bit(s)");
376376
}
377377
} else {
378-
if (value < 0 || value >= limit) {
378+
if (value < 0 || value > limit) {
379379
throw new Error("Integer overflow when representing unsigned " + value + " in " + length + " bit(s)");
380380
}
381381
}

packages/core/test/ContentSerdesTest.ts

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,30 @@ class SerdesOctetTests {
578578
body = await content.toBuffer();
579579
expect(body).to.deep.equal(Buffer.from([0x80, 0x44]));
580580

581+
content = await ContentSerdes.valueToContent(
582+
255,
583+
{ type: "uint8" },
584+
`application/octet-stream;byteSeq=${Endianness.LITTLE_ENDIAN};length=1;`
585+
);
586+
body = await content.toBuffer();
587+
expect(body).to.deep.equal(Buffer.from([0xff]));
588+
589+
content = await ContentSerdes.valueToContent(
590+
127,
591+
{ type: "int8" },
592+
`application/octet-stream;signed=true;length=1;`
593+
);
594+
body = await content.toBuffer();
595+
expect(body).to.deep.equal(Buffer.from([0x7f]));
596+
597+
content = await ContentSerdes.valueToContent(
598+
-128,
599+
{ type: "int8" },
600+
`application/octet-stream;signed=true;length=1;`
601+
);
602+
body = await content.toBuffer();
603+
expect(body).to.deep.equal(Buffer.from([0x80]));
604+
581605
content = ContentSerdes.valueToContent(
582606
2345,
583607
{ type: "integer", "ex:bitLength": 24 },
@@ -742,11 +766,23 @@ class SerdesOctetTests {
742766
}
743767

744768
@test "value to OctetStream should throw"() {
745-
// @ts-ignore new dataschema types are not yet supported in the td type definitions
746-
expect(() => ContentSerdes.valueToContent(2345, { type: "int8" }, "application/octet-stream")).to.throw(
769+
expect(() => ContentSerdes.valueToContent(256, { type: "uint8" }, "application/octet-stream")).to.throw(
770+
Error,
771+
"Integer overflow when representing unsigned 256 in 8 bit(s)"
772+
);
773+
expect(() => ContentSerdes.valueToContent(-1, { type: "uint8" }, "application/octet-stream")).to.throw(
774+
Error,
775+
"Integer overflow when representing unsigned -1 in 8 bit(s)"
776+
);
777+
expect(() => ContentSerdes.valueToContent(128, { type: "int8" }, "application/octet-stream")).to.throw(
778+
Error,
779+
"Integer overflow when representing signed 128 in 8 bit(s)"
780+
);
781+
expect(() => ContentSerdes.valueToContent(-129, { type: "int8" }, "application/octet-stream")).to.throw(
747782
Error,
748-
"Integer overflow when representing signed 2345 in 8 bit(s)"
783+
"Integer overflow when representing signed -129 in 8 bit(s)"
749784
);
785+
750786
// @ts-ignore new dataschema types are not yet supported in the td type definitions
751787
expect(() => ContentSerdes.valueToContent(23450000, { type: "int16" }, "application/octet-stream")).to.throw(
752788
Error,

0 commit comments

Comments
 (0)