Skip to content

Commit

Permalink
add check to ensure ex:bitOffset is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
derwehr committed Apr 4, 2024
1 parent 7415c62 commit d4a3888
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/core/src/codecs/octetstream-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -543,8 +543,18 @@ export default class OctetstreamCodec implements ContentCodec {
throw new Error("Missing 'length' parameter necessary for write");
}

const length = parseInt(parameters.length);
const offset = schema["ex:bitOffset"] !== undefined ? parseInt(schema["ex:bitOffset"]) : 0;
result = result ?? Buffer.alloc(parseInt(parameters.length));

if (isNaN(offset) || offset < 0) {
throw new Error("ex:bitOffset must be a non-negative number");
}

Check warning on line 551 in packages/core/src/codecs/octetstream-codec.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/codecs/octetstream-codec.ts#L550-L551

Added lines #L550 - L551 were not covered by tests

if (offset > length * 8) {
throw new Error(`ex:bitOffset ${offset} exceeds length ${length}`);
}

Check warning on line 555 in packages/core/src/codecs/octetstream-codec.ts

View check run for this annotation

Codecov / codecov/patch

packages/core/src/codecs/octetstream-codec.ts#L554-L555

Added lines #L554 - L555 were not covered by tests

result = result ?? Buffer.alloc(length);
for (const propertyName in schema.properties) {
if (Object.hasOwnProperty.call(value, propertyName) === false) {
throw new Error(`Missing property '${propertyName}'`);
Expand Down

0 comments on commit d4a3888

Please sign in to comment.