From f445ee269f338f1458d1352a0c796f63f5481d37 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Tue, 23 Jan 2024 11:52:56 -0800 Subject: [PATCH] terminal: ignore change window title requests with invalid UTF-8 --- src/terminal/stream.zig | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/terminal/stream.zig b/src/terminal/stream.zig index bb25b7c10..a3400421f 100644 --- a/src/terminal/stream.zig +++ b/src/terminal/stream.zig @@ -978,6 +978,11 @@ pub fn Stream(comptime Handler: type) type { switch (cmd) { .change_window_title => |title| { if (@hasDecl(T, "changeWindowTitle")) { + if (!std.unicode.utf8ValidateSlice(title)) { + log.warn("change title request: invalid utf-8, ignoring request", .{}); + return; + } + try self.handler.changeWindowTitle(title); return; } else log.warn("unimplemented OSC callback: {}", .{cmd}); @@ -1627,6 +1632,30 @@ test "stream: XTSHIFTESCAPE" { try testing.expect(s.handler.escape.? == true); } +test "stream: change window title with invalid utf-8" { + const H = struct { + seen: bool = false, + + pub fn changeWindowTitle(self: *@This(), title: []const u8) !void { + _ = title; + + self.seen = true; + } + }; + + { + var s: Stream(H) = .{ .handler = .{} }; + try s.nextSlice("\x1b]2;abc\x1b\\"); + try testing.expect(s.handler.seen); + } + + { + var s: Stream(H) = .{ .handler = .{} }; + try s.nextSlice("\x1b]2;abc\xc0\x1b\\"); + try testing.expect(!s.handler.seen); + } +} + test "stream: insert characters" { const H = struct { const Self = @This();