szn

Bug 278 — insertLines double-decrements sixel refcount of the discarded bottom line

278. insertLines double-decrements sixel refcount of the discarded bottom line

File: src/screen.zig:959–983 Severity: CRITICAL Status: ✅ FIXED — removed the second decrement; the discarded bottom line’s buffer is now decremented exactly once (before the shift). Regression test verifies a two-marker image keeps refcount 1 after insertLines(1).

var row = bottom;
const temp = self.grid.getLine(bottom).*;                      // copies bottom GridLine (shares cells buffer)
self.decrementLineRefs(self.grid.getLineMut(row).cells.items); // decrement #1 — old bottom line
while (row > y) : (row -= 1) {
    self.grid.getLineMut(row).* = self.grid.getLine(row - 1).*;
}
self.grid.getLineMut(y).* = temp;                              // old bottom buffer now at row y
const line = self.grid.getLineMut(y);
self.decrementLineRefs(line.cells.items);                      // decrement #2 — SAME buffer again
@memset(line.cells.items, fill);

temp is a shallow copy of the bottom line’s GridLine (the cells ArrayList points at the same buffer). After the shift, that exact buffer is placed at row y and its sixel marker refcounts are decremented a second time for the same discarded content. Reproduced with a unit test: two marker cells (refcount 2) → after insertLines(1) refcount drops to 0 instead of the correct 1.

Impact: Under-counted refcount makes isImageReferenced() return false for a still-visible image; placeSixelImage then evicts/frees it while cells still reference it → ghost/blank sixel or freed-image dereference in the render path.