terminal: osc parser gracefully handles input data larger than buffer

This commit is contained in:
Mitchell Hashimoto
2022-12-14 21:24:43 -08:00
parent c7a28fab20
commit 0592e4fa1a

View File

@ -149,6 +149,12 @@ pub const Parser = struct {
/// Consume the next character c and advance the parser state. /// Consume the next character c and advance the parser state.
pub fn next(self: *Parser, c: u8) void { 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 // We store everything in the buffer so we can do a better job
// logging if we get to an invalid command. // logging if we get to an invalid command.
self.buf[self.buf_idx] = c; 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(cmd.clipboard_contents.kind == 's');
try testing.expect(std.mem.eql(u8, "?", cmd.clipboard_contents.data)); 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);
}