szn

Bug 65 — Use-after-free / double-free via errdefer in Grid.scrollUp()

65. Use-after-free / double-free via errdefer in Grid.scrollUp()

File: src/grid.zig:163–167 Severity: CRITICAL Status: ✅ FIXED — allocate new_line before swapping old_line out; history append after grid is safe.

var line = self.getLine(0).*;         // copy by value, shares cells pointer with grid
line.dirty = false;
errdefer line.deinit(self.allocator);  // registered on stack copy

try self.history.append(self.allocator, line);  // if succeeds, 3 copies share same cells

After append succeeds, the errdefer is still active. If any subsequent operation fails (e.g. new_line.cells.resize at line 174), the errdefer fires and frees line.cells.items. But the history entry also points to that same buffer because append copies the struct by value. This creates a dangling pointer in history. When Grid.deinit later iterates history and calls deinit on each entry, it double-frees the already-freed buffer.