From 03fd9a970bf1d1760aef500eda4f83c0f13f5962 Mon Sep 17 00:00:00 2001 From: Qwerasd Date: Mon, 10 Feb 2025 11:49:05 -0500 Subject: [PATCH] fix(terminal): properly invalidate over-sized OSCs The `self.complete = false` here is important so we don't accidentally dispatch, for example, a hyperlink command with an empty URI. --- src/terminal/osc.zig | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/terminal/osc.zig b/src/terminal/osc.zig index 10ba5b5e7..90dd079a0 100644 --- a/src/terminal/osc.zig +++ b/src/terminal/osc.zig @@ -272,6 +272,9 @@ pub const Parser = struct { // Maximum length of a single OSC command. This is the full OSC command // sequence length (excluding ESC ]). This is arbitrary, I couldn't find // any definitive resource on how long this should be. + // + // NOTE: This does mean certain OSC sequences such as OSC 8 (hyperlinks) + // won't work if their parameters are larger than fit in the buffer. const MAX_BUF = 2048; pub const State = enum { @@ -425,9 +428,23 @@ 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 our buffer is full then we're invalid, so we set our state + // accordingly and indicate the sequence is incomplete so that we + // don't accidentally issue a command when ending. if (self.buf_idx >= self.buf.len) { + if (self.state != .invalid) { + log.warn( + "OSC sequence too long (> {d}), ignoring. state={}", + .{ self.buf.len, self.state }, + ); + } + self.state = .invalid; + + // We have to do this here because it will never reach the + // switch statement below, since our buf_idx will always be + // too high after this. + self.complete = false; return; }