From 739fc746bfbefb67fe46afda8bc4ed0994bc1e11 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Mon, 12 Feb 2024 19:30:04 -0800 Subject: [PATCH] terminal: OSC parses 4 with empty string param without crashing --- src/terminal/osc.zig | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/terminal/osc.zig b/src/terminal/osc.zig index b55b8a199..a220ea031 100644 --- a/src/terminal/osc.zig +++ b/src/terminal/osc.zig @@ -445,8 +445,14 @@ pub const Parser = struct { .color_palette_index => switch (c) { '0'...'9' => {}, - ';' => { - if (std.fmt.parseUnsigned(u8, self.buf[self.buf_start .. self.buf_idx - 1], 10)) |num| { + ';' => blk: { + const str = self.buf[self.buf_start .. self.buf_idx - 1]; + if (str.len == 0) { + self.state = .invalid; + break :blk; + } + + if (std.fmt.parseUnsigned(u8, str, 10)) |num| { self.state = .color_palette_index_end; self.temp_state = .{ .num = num }; } else |err| switch (err) { @@ -1254,3 +1260,15 @@ test "OSC: show desktop notification with title" { try testing.expectEqualStrings(cmd.show_desktop_notification.title, "Title"); try testing.expectEqualStrings(cmd.show_desktop_notification.body, "Body"); } + +test "OSC: empty param" { + const testing = std.testing; + + var p: Parser = .{}; + + const input = "4;;"; + for (input) |ch| p.next(ch); + + const cmd = p.end('\x1b'); + try testing.expect(cmd == null); +}