From c01a9f583f9348c9dae2b3299c8f92b2313e7201 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Wed, 14 Dec 2022 19:20:05 -0800 Subject: [PATCH] zero-width chars are ignored if they're at col 0 This can cause a crash, and is impossible since zero-width chars are always attached to a prior character. Word-wrapping doesn't come into play here because this check happens prior to the wrapping. --- src/terminal/Terminal.zig | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/terminal/Terminal.zig b/src/terminal/Terminal.zig index 5766e9f1b..5540d9532 100644 --- a/src/terminal/Terminal.zig +++ b/src/terminal/Terminal.zig @@ -566,6 +566,15 @@ pub fn print(self: *Terminal, c: u21) !void { // Attach zero-width characters to our cell as grapheme data. if (width == 0) { + // If we're at cell zero, then this is malformed data and we don't + // print anything or even store this. Zero-width characters are ALWAYS + // attached to some other non-zero-width character at the time of + // writing. + if (self.screen.cursor.x == 0) { + log.warn("zero-width character with no prior character, ignoring", .{}); + return; + } + // Find our previous cell const row = self.screen.getRow(.{ .active = self.screen.cursor.y }); const prev: usize = prev: { @@ -1391,6 +1400,18 @@ test "Terminal: input with no control characters" { } } +test "Terminal: zero-width character at start" { + var t = try init(testing.allocator, 80, 80); + defer t.deinit(testing.allocator); + + // This used to crash the terminal. This is not allowed so we should + // just ignore it. + try t.print(0x200D); + + try testing.expectEqual(@as(usize, 0), t.screen.cursor.y); + try testing.expectEqual(@as(usize, 0), t.screen.cursor.x); +} + test "Terminal: soft wrap" { var t = try init(testing.allocator, 3, 80); defer t.deinit(testing.allocator);