Skip to content

Commit 450ebd9

Browse files
authored
Merge pull request #1219 from derwehr/master
fix(octet-stream): correct limits for integer serialization and add tests
2 parents af74c9d + 47e9408 commit 450ebd9

2 files changed

Lines changed: 54 additions & 10 deletions

File tree

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,12 +371,20 @@ 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) {
375-
throw new Error("Integer overflow when representing signed " + value + " in " + length + " bit(s)");
374+
if (value < -limit - 1 || value > limit) {
375+
throw new Error(
376+
"Integer overflow when representing " + value + " as a signed integer using " + length + " bit(s)"
377+
);
376378
}
377379
} else {
378-
if (value < 0 || value >= limit) {
379-
throw new Error("Integer overflow when representing unsigned " + value + " in " + length + " bit(s)");
380+
if (value < 0 || value > limit) {
381+
throw new Error(
382+
"Integer overflow when representing " +
383+
value +
384+
" as an unsigned integer using " +
385+
length +
386+
" bit(s)"
387+
);
380388
}
381389
}
382390

packages/core/test/ContentSerdesTest.ts

Lines changed: 42 additions & 6 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,15 +766,27 @@ 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 256 as an unsigned integer using 8 bit(s)"
772+
);
773+
expect(() => ContentSerdes.valueToContent(-1, { type: "uint8" }, "application/octet-stream")).to.throw(
774+
Error,
775+
"Integer overflow when representing -1 as an unsigned integer using 8 bit(s)"
776+
);
777+
expect(() => ContentSerdes.valueToContent(128, { type: "int8" }, "application/octet-stream")).to.throw(
778+
Error,
779+
"Integer overflow when representing 128 as a signed integer using 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 -129 as a signed integer using 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,
753-
"Integer overflow when representing signed 23450000 in 16 bit(s)"
789+
"Integer overflow when representing 23450000 as a signed integer using 16 bit(s)"
754790
);
755791
expect(() => ContentSerdes.valueToContent(2345, { type: "foo" }, "application/octet-stream")).to.throw(
756792
Error,
@@ -768,14 +804,14 @@ class SerdesOctetTests {
768804
{ type: "integer", "ex:bitOffset": 0, "ex:bitLength": 10 },
769805
"application/octet-stream"
770806
)
771-
).to.throw(Error, "Integer overflow when representing signed -2345 in 10 bit(s)");
807+
).to.throw(Error, "Integer overflow when representing -2345 as a signed integer using 10 bit(s)");
772808
expect(() =>
773809
ContentSerdes.valueToContent(
774810
-32769,
775811
{ type: "integer", "ex:bitOffset": 0, "ex:bitLength": 16 },
776812
"application/octet-stream"
777813
)
778-
).to.throw(Error, "Integer overflow when representing signed -32769 in 16 bit(s)");
814+
).to.throw(Error, "Integer overflow when representing -32769 as a signed integer using 16 bit(s)");
779815
expect(() =>
780816
ContentSerdes.valueToContent(
781817
{ flag1: true, flag2: false, numberProperty: 99, stringProperty: "Web" },

0 commit comments

Comments
 (0)