Skip to content

Commit 637b954

Browse files
committed
fix(input): bail OSC code accumulation past 22 to prevent overflow
The numeric OSC code loop accumulated digits with no cap, so a long digit run (e.g. ESC ] 9999...) would overflow the int accumulator. Since 22 is the only code parse_osc accepts and the accumulator only grows, bail with PARSE_ERR as soon as it passes 22. Addresses review feedback from @dreyfus92 on #101.
1 parent 4b618f3 commit 637b954

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

src/input.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,11 @@ static int parse_osc(struct InputState *st, struct InputEvent *ev) {
632632
if (code == -1)
633633
code = 0;
634634
code = code * 10 + (st->buf[i] - '0');
635+
/* 22 is the only accepted code; the accumulator only grows as digits
636+
* arrive, so once it passes 22 no valid completion exists. Bail here so a
637+
* long digit run can never overflow `code`. */
638+
if (code > 22)
639+
return PARSE_ERR;
635640
i++;
636641
}
637642
if (i >= st->len)

test/input.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,15 @@ describe("input", () => {
763763
});
764764
});
765765

766+
it("rejects an unbounded digit run without overflowing", () => {
767+
// A long run of digits must not overflow the code accumulator; anything
768+
// that grows past 22 can never match, so it is dropped as an error.
769+
let result = input.scan(str("\x1b]" + "9".repeat(64) + ";x\x1b\\"));
770+
expect(
771+
result.events.some((e) => e.type === "pointershape"),
772+
).toBe(false);
773+
});
774+
766775
it("parses a reply interleaved with other input", () => {
767776
let result = input.scan(str("a\x1b]22;default\x1b\\b"));
768777
expect(result.events.length).toBe(3);

0 commit comments

Comments
 (0)