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.
This commit is contained in:
Mitchell Hashimoto
2022-12-14 19:20:05 -08:00
parent 9f92b5aa0a
commit c01a9f583f

View File

@ -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);