szn

Bug 455 — Grid.setHistoryLimit double-frees history lines on compaction OOM

455. OOM path leaves freed GridLines live in the history array

Severity: HIGH Status: ✅ FIXED Files: src/grid.zig


Symptoms

Only triggers on allocation failure while shrinking history-limit with a rotated ring (history_start > 0). Double deinit of the same cells buffers → allocator canary / use-after-free in safe builds.


Root Cause Analysis

In src/grid.zig:165-192:

// shrink: deinit excess oldest entries, set history_count = new_limit
self.history_start = ...; self.history_count = new_limit;
if (self.history_start > 0 and ...) {
    const temp = self.allocator.alloc(GridLine, self.history_count) catch null;
    if (temp) |buf| { ...; self.history.items.len = self.history_count; ... }
    // OOM (null): len stays long, count short, freed structs remain
}

Grid.deinit:199 loops for (self.history.items) (full len, not count) and deinits again.

Proposed Fix

On compaction failure either truncate history.items.len = history_count after moving live entries without temp, or re-compact in place / leave history_start rotated but consistent so deinit only visits live entries. Test with a failing allocator at the compaction point.