terminal: DECALN must use clearRows to clear protected memory

This commit is contained in:
Mitchell Hashimoto
2024-09-05 14:26:13 -07:00
parent f0149722e6
commit a6031efa04
2 changed files with 52 additions and 3 deletions

View File

@ -268,6 +268,9 @@ pub fn assertIntegrity(self: *const Screen) void {
) orelse unreachable; ) orelse unreachable;
assert(self.cursor.x == pt.active.x); assert(self.cursor.x == pt.active.x);
assert(self.cursor.y == pt.active.y); assert(self.cursor.y == pt.active.y);
// Assert our current page.
self.cursor.page_pin.page.data.assertIntegrity();
} }
} }

View File

@ -2272,8 +2272,13 @@ pub fn decaln(self: *Terminal) !void {
// Move our cursor to the top-left // Move our cursor to the top-left
self.setCursorPos(1, 1); self.setCursorPos(1, 1);
// Erase the display which will deallocate graphemes, styles, etc. // Use clearRows instead of eraseDisplay because we must NOT respect
self.eraseDisplay(.complete, false); // protected attributes here.
self.screen.clearRows(
.{ .active = .{} },
null,
false,
);
// Fill with Es by moving the cursor but reset it after. // Fill with Es by moving the cursor but reset it after.
while (true) { while (true) {
@ -2285,7 +2290,9 @@ pub fn decaln(self: *Terminal) !void {
.content_tag = .codepoint, .content_tag = .codepoint,
.content = .{ .codepoint = 'E' }, .content = .{ .codepoint = 'E' },
.style_id = self.screen.cursor.style_id, .style_id = self.screen.cursor.style_id,
.protected = self.screen.cursor.protected,
// DECALN does not respect protected state. Verified with xterm.
.protected = false,
}); });
// If we have a ref-counted style, increase // If we have a ref-counted style, increase
@ -2298,6 +2305,9 @@ pub fn decaln(self: *Terminal) !void {
row.styled = true; row.styled = true;
} }
// We messed with the page so assert its integrity here.
page.assertIntegrity();
self.screen.cursorMarkDirty(); self.screen.cursorMarkDirty();
if (self.screen.cursor.y == self.rows - 1) break; if (self.screen.cursor.y == self.rows - 1) break;
self.screen.cursorDown(1); self.screen.cursorDown(1);
@ -8030,6 +8040,42 @@ test "Terminal: decaln preserves color" {
} }
} }
test "Terminal: DECALN resets graphemes with protected mode" {
const alloc = testing.allocator;
var t = try init(alloc, .{ .cols = 3, .rows = 3 });
defer t.deinit(alloc);
// Add protected mode. A previous version of DECALN accidentally preserved
// protected mode which left dangling managed memory.
t.setProtectedMode(.iso);
// This is: 👨👩👧 (which may or may not render correctly)
t.modes.set(.grapheme_cluster, true);
try t.print(0x1F468);
try t.print(0x200D);
try t.print(0x1F469);
try t.print(0x200D);
try t.print(0x1F467);
try t.decaln();
try testing.expectEqual(@as(usize, 0), t.screen.cursor.y);
try testing.expectEqual(@as(usize, 0), t.screen.cursor.x);
try testing.expect(t.screen.cursor.protected);
try testing.expect(t.screen.protected_mode == .iso);
for (0..t.rows) |y| try testing.expect(t.isDirty(.{ .active = .{
.x = 0,
.y = @intCast(y),
} }));
{
const str = try t.plainString(testing.allocator);
defer testing.allocator.free(str);
try testing.expectEqualStrings("EEE\nEEE\nEEE", str);
}
}
test "Terminal: insertBlanks" { test "Terminal: insertBlanks" {
// NOTE: this is not verified with conformance tests, so these // NOTE: this is not verified with conformance tests, so these
// tests might actually be verifying wrong behavior. // tests might actually be verifying wrong behavior.