Skip to content

Commit

Permalink
fix(tlv): handle underruns and overruns during writes
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak authored Nov 24, 2023
1 parent 24d6f80 commit 846b2a1
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/lib/type/TlvIo.mts
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,21 @@ class TlvIo implements IoType {
context.root = context.root ?? null;

this.#tagType.write(stream, value.tag, context);

this.#lengthType.write(stream, value.length, context);
const expectedEndOffset = stream.offset + value.length;

const valueType = this.#valueCallback(value.tag, value.length);
if (valueType && valueType.write) {
valueType.write(stream, value.value, context);
} else {
stream.writeBytes(value.value);
}

// Account for underruns and overruns
if (stream.offset !== expectedEndOffset) {
stream.offset = expectedEndOffset;
}
}
}

Expand Down

0 comments on commit 846b2a1

Please sign in to comment.