szn

Bug 513 — IRM shift under-counts sixel refcounts

513. Insert-mode shift drops a refcount for every marker it moves instead of every marker it destroys

Severity: MEDIUM Status: ✅ FIXED Files: src/screen.zig:954-961, src/screen.zig:824, src/screen.zig:890

Problem

When insert mode is active, writeChar shifts the tail of the line right by one cell:

// src/screen.zig:954-961
if (self.mode.insert) {
    var x = self.grid.width - 1;
    while (x > self.cursor.x) : (x -= 1) {
        // bug #225: decrement refcount for the overwritten cell.
        self.decrementMainGridRef(x, self.cursor.y);
        const prev = self.grid.getCell(x - 1, self.cursor.y);
        self.grid.setCell(x, self.cursor.y, prev);
    }
}

A marker at column p is copied to p + 1. It is not destroyed — it moved. The refcount counts cells carrying a marker, so a move must be balanced: decrement the destination’s old content, then re-increment for the marker that landed there. That is exactly the pattern bug #491 established:

// src/screen.zig:888-890
self.decrementMainGridRef(0, self.cursor.y);
self.grid.setCell(0, self.cursor.y, prev_cell);
self.incrementCellRef(prev_cell); // bug #491: re-increment for moved sixel marker

The IRM loop has the decrement but not the incrementCellRef. Net effect: one lost refcount per moved marker. Only the cell at width - 1 (shifted off the edge) and the cell at cursor.x (overwritten by the new character) are genuinely destroyed; the loop decrements for all width - 1 - cursor.x of them.

Contrast Screen.insertChars (screen.zig:1011-1027), fixed under bug #292, which releases exactly the rightmost num cells — the ones shifted off the edge.

Once a slot’s count hits zero, render.zig:1007 (if (screen.sixel_refcounts[slot] == 0) continue;) stops drawing it, and the slot becomes evictable while live marker cells still point at its id.

Reachable from any application that enables IRM with CSI 4 h (input.zig:956) and writes over a region containing a sixel.

Note on the prior verdict

docs/development/szn-audit-2026-08-31.md filed this as finding H2 and then rejected it as a false positive, on the reasoning that the loop “decrements the refcount for cells it overwrites … matching sibling insertChars”. That reasoning does not survive inspection: insertChars does not decrement every overwritten cell (see #292), and the incrementCellRef re-increment was added later by #491 precisely because moved markers need one. The H2 rejection should be treated as superseded.

Fix

// src/screen.zig — Screen.writeChar, insert-mode shift
const prev = self.grid.getCell(x - 1, self.cursor.y);
self.decrementMainGridRef(x, self.cursor.y);
self.grid.setCell(x, self.cursor.y, prev);
self.incrementCellRef(prev);   // bug #513

The shift now pairs each destination’s decrement with a re-increment for the marker that lands there, which is the same decrement-then-re-increment rule bug #491 established for a relocated marker (screen.zig:824, :890).

Tracing a 5-wide line with markers at columns 0 and 2 and the cursor at column 0:

Step Refcount
Start 2
x=3: prev is the marker from column 2 → re-increment 3
x=2: column 2’s marker is overwritten → decrement 2
x=1: prev is the marker from column 0 → re-increment 3
final: column 0 is overwritten by the new character → decrement 2

The two markers are still on the grid (columns 1 and 3), so 2 is correct. Before the fix the re-increments were missing and the count reached 0, at which point render.zig:1007 stops drawing the image and the slot becomes evictable while live marker cells still reference it.

Regression test

insert mode re-increments refcounts of markers it shifts — bug #513 (src/screen.zig) seeds two marker cells, enables insert mode, writes one character at column 0, and asserts the refcount is still 2 with markers present at columns 1 and 3. Without the fix the count is 0, so this fails on the old code.

Verified with the full suite on Zig 0.16.0 and 0.17.0: 1046/1046 passing on both.