From 35793ee7cc91881f82eb9751a957fddaf16dbd90 Mon Sep 17 00:00:00 2001 From: Qwerasd Date: Mon, 24 Jun 2024 20:31:42 -0400 Subject: [PATCH] page integrity checks: detect zombie styles --- src/terminal/page.zig | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/terminal/page.zig b/src/terminal/page.zig index 82776b3ec..f48a0ed0c 100644 --- a/src/terminal/page.zig +++ b/src/terminal/page.zig @@ -237,6 +237,7 @@ pub const Page = struct { MissingStyle, UnmarkedStyleRow, MismatchedStyleRef, + ZombieStyles, InvalidStyleCount, InvalidSpacerTailLocation, InvalidSpacerHeadLocation, @@ -429,6 +430,37 @@ pub const Page = struct { } } } + + // Verify there are no zombie styles, that is, styles in the + // set with ref counts > 0, which are not present in the page. + { + const styles_table = self.styles.table.ptr(self.memory)[0..self.styles.layout.table_cap]; + const styles_items = self.styles.items.ptr(self.memory)[0..self.styles.layout.cap]; + + var zombies: usize = 0; + + for (styles_table) |id| { + if (id == 0) continue; + const item = styles_items[id]; + if (item.meta.ref == 0) continue; + + const expected = styles_seen.get(id) orelse 0; + if (expected > 0) continue; + + if (item.meta.ref > expected) { + zombies += 1; + } + } + + // Just 1 zombie style might be the cursor style, so ignore it. + if (zombies > 1) { + log.warn( + "page integrity violation zombie styles count={}", + .{zombies}, + ); + return IntegrityError.ZombieStyles; + } + } } /// Clone the contents of this page. This will allocate new memory