From 0592e4fa1a2b97abb2fad963c5996017dacf905a Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 14 Dec 2022 21:24:43 -0800 Subject: [PATCH] terminal: osc parser gracefully handles input data larger than buffer --- src/terminal/osc.zig | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/terminal/osc.zig b/src/terminal/osc.zig index 494938d58..8a0a1a1b6 100644 --- a/src/terminal/osc.zig +++ b/src/terminal/osc.zig @@ -149,6 +149,12 @@ pub const Parser = struct { /// Consume the next character c and advance the parser state. pub fn next(self: *Parser, c: u8) void { + // If our buffer is full then we're invalid. + if (self.buf_idx >= self.buf.len) { + self.state = .invalid; + return; + } + // We store everything in the buffer so we can do a better job // logging if we get to an invalid command. self.buf[self.buf_idx] = c; @@ -498,3 +504,14 @@ test "OSC: get/set clipboard" { try testing.expect(cmd.clipboard_contents.kind == 's'); try testing.expect(std.mem.eql(u8, "?", cmd.clipboard_contents.data)); } + +test "OSC: longer than buffer" { + const testing = std.testing; + + var p: Parser = .{}; + + const input = "a" ** (Parser.MAX_BUF + 2); + for (input) |ch| p.next(ch); + + try testing.expect(p.end() == null); +}