szn

Bug 462 — Grid.insertChars/clearArea width-1 underflows when width is 0

462. u32 width-1 wraps on zero-width grids

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


Root Cause Analysis

In src/grid.zig:469 (num==0 early-return does not save the initializer when width==0):

var i = self.width - 1;
while (i >= x + num) : (i -= 1) { line.cells.items[i] = ...; }

In clearArea:510-512:

const max_x = if (y == ey) @min(ex, self.width - 1) else self.width - 1;
@memset(line.cells.items[x .. max_x + 1], ...);

width==0 → 0-1 == 0xFFFFFFFF, immediate OOB; max_x+1 can wrap to 0 and slice x..0 panics.

Proposed Fix

Guard if (self.width == 0) return; and use width -| 1 in both functions. Audit sibling deleteChars/eraseChars which already use -|.