szn

Bug 526 — CBT divides by tab_stop unguarded

526. The #463 zero guard was added to TAB but not to CBT

Severity: LOW Status: ✅ FIXED Files: src/input.zig:690-696, src/screen.zig:759

Verification status: identified by static analysis; not reproduced.

Problem

// src/input.zig:690-696 (abridged)
'Z' => {
    const count = @min(self.paramDefault(0, 1), 1000);
    var i: u32 = 0;
    while (i < count) : (i += 1) {
        if (self.screen.cursor.x > 0) {
            const ts = self.screen.tab_stop;
            const remainder = self.screen.cursor.x % ts;

Bug #463 (“Screen TAB handling underflows on zero width and can divide by zero”) added a guard to the TAB path:

// src/screen.zig:759
if (ts == 0) break;

The CBT path performs the same modulo without it. tab_stop defaults to 8 and is only assigned 0 by tests, so this is unreachable in production today — but it is a latent trap and the two handlers of the same concept now disagree about whether tab_stop can be zero.

Proposed Fix

const ts = self.screen.tab_stop;
if (ts == 0) break;

Or, better, make tab_stop a non-zero type so the invariant is enforced once rather than checked at each use site.

Fix

// src/input.zig — CBT
const ts = self.screen.tab_stop;
if (ts == 0) break;   // bug #526, matching the #463 guard in the TAB path
const remainder = self.screen.cursor.x % ts;

Regression test

CBT (CSI Z) survives a zero tab_stop — bug #526 (src/input.zig) sets tab_stop = 0, feeds CSI 1 Z, and asserts the cursor does not move. Without the guard that is a divide-by-zero trap in Debug and ReleaseSafe.

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

Bugs #463 (TAB zero-width underflow / divide-by-zero), #321 (uncapped CSI Z loop — the @min(count, 1000) cap above is that fix).