szn

Bug 525 — origin-mode row addition can overflow

525. row + r[0] is not saturating in cursorPosition

Severity: LOW Status: ✅ FIXED Files: src/screen.zig:1143-1148, src/screen.zig:1137

Verification status: identified by static analysis; not reproduced.

Problem

// src/screen.zig:1143-1148
const y: u32 = if (self.mode.origin) blk: {
    if (self.scroll_region) |r| {
        break :blk @min(row + r[0], r[1]);
    }
    break :blk @min(row, self.grid.height -| 1);
} else @min(row, self.grid.height -| 1);

row derives from paramDefault(0, 1) -| 1 (input.zig:666), which saturates at maxInt(u32) - 1 for an oversized parameter. The addition row + r[0] is plain +, not +|, so it traps in Debug and ReleaseSafe builds for a large CSI <row> H while origin mode and a scroll region are both active.

The sibling cursorLine gets this right:

// src/screen.zig:1137
const y = y +| origin_y;

so the two functions disagree on the same arithmetic.

Today the branch is unreachable because mode.origin is never set — see bug #523. Wiring DECOM without fixing this converts a latent trap into a remotely triggerable panic (a program only has to emit CSI ?6h, set a scroll region, then send a large CUP).

Proposed Fix

break :blk @min(row +| r[0], r[1]);

Fix this in the same change as bug #523.

Fix

// src/screen.zig — cursorPosition
break :blk @min(row +| r[0], r[1]);

Saturating, matching the sibling cursorLine (screen.zig:1137).

Regression test

origin-mode CUP with a saturated row clamps instead of overflowing — bug #525 (src/input.zig) enables DECOM inside a scroll region and feeds CSI 4294967295;1H. The row parameter saturates at maxInt(u32)-1, so the non-saturating add trapped in Debug and ReleaseSafe before the fix.

Fixed in the same change as bug #523, which is what makes the branch reachable — wiring DECOM without this would have turned a latent trap into an input-triggerable panic.

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

Bugs #484 and #485 were untrusted-parameter overflow panics of exactly this shape; #289 and #463 are the saturating-arithmetic precedents.