Severity: MEDIUM (use-after-free of a sixel slot; rare trigger)
Status: 🟢 FIXED
Files: src/screen.zig:771-784, src/screen.zig:833-850
Both variation-selector promotion paths move prev_cell while calling decrementMainGridRef twice:
self.decrementMainGridRef(target_col, prev_y); // source marker destroyed (-1)
self.grid.setCell(target_col, prev_y, self.eraseCell());
self.decrementMainGridRef(0, self.cursor.y); // destination's old marker destroyed (-1 iff marker)
self.grid.setCell(0, self.cursor.y, prev_cell); // moved marker re-added (+1)
Net effect on markers: −1 (one marker destroyed, one moved). Net effect on the refcount: −2. The refcount can therefore hit 0 while a live marker cell still references the slot → the slot gets reused/evicted while referenced (stale image id, overlay corruption, or worse).
Trigger is narrow — variation-selector-always-wide enabled and a sixel marker cell as the VS16 base — but real.
Re-increment the slot refcount when the moved prev_cell.attr.sixel is true (i.e. account for the move as destroy + create, not destroy + destroy). A debug-mode invariant — refcount equals live marker count — would catch this class.
Added incrementCellRef(self: *Screen, cell: Cell) helper and invoked it in both VS16 promotion paths after writing prev_cell to (0, self.cursor.y). If prev_cell.attr.sixel is true, its refcount is re-incremented to account for the newly placed destination marker, matching the live marker count. Added regression tests variation-selector-always-wide preserves sixel refcount on promotion wrap — bug #491 and variation-selector-always-wide preserves sixel refcount on auto-wrapped newline — bug #491.