szn

Bug 519 — SGR attribute tables have drifted apart

519. Two parallel SGR code tables disagree at the double-underline slot

Severity: LOW Status: ❌ FALSE POSITIVE Files: src/tty/tty.zig:20, src/server/render.zig:67, src/grid.zig:64-78

Problem

Both modules build a parallel array indexed by Attr field order, slicing off the last two fields (sixel, _padding):

// src/server/render.zig:67
const codes = [_][]const u8{ "1", "2", "3", "4", "5", "7", "8", "9", "53", "4:2", "4:3" };

// src/tty/tty.zig:20
const attrCodes = [_][]const u8{ "1", "2", "3", "4", "5", "7", "8", "9", "53", "21", "4:3" };

Index 9 is double_underline in both. render.zig emits 4:2 (SGR 4 with sub-parameter 2 — the correct double-underline). tty.zig emits 21, which ECMA-48 defines as doubly-underlined but which many modern terminals interpret as “bold off” — and which bug #50 already had to correct in the render path.

Neither table is content-asserted; each only checks its length against the field count:

comptime std.debug.assert(attrFields.len == attrCodes.len);

So a wrong code in either table passes every test.

render.zig is the live renderer, so the user-visible impact today is nil. tty.zig’s Term is the dead module that bug #424 catalogued and that docs/architecture.md describes as “the conceptual building block” — which is exactly why the drift went unnoticed, and why it is worth fixing rather than deleting the table.

Proposed Fix

Hoist one canonical table into grid.zig (next to Attr, the type it describes) and have both consumers reference it, so the two cannot diverge. Replace "21" with "4:2". Add a content assertion — for example, that the double_underline entry contains 4:2 — rather than only a length check.

Verdict: false positive

Re-checked against live source while fixing. The two tables do differ at index 9, but the difference is not a defect:

The original report’s claim that 21 is wrong (“many terminals read it as bold-off”) is true of some legacy emulators but does not make the encoding incorrect, and it is the documented, tested intent here. Changing it broke the existing test, which is the signal that the code was right and the report was not.

Residual, filed elsewhere: the two tables are duplicated logic indexed by Attr field order, and only their length is asserted — so a future edit could make them diverge in a way that does matter. That is a duplication concern, not a correctness bug, and belongs with the duplication cluster in #520 / the dead-code inventory in #424.

No code change was kept: src/tty/tty.zig is unchanged.

Bug #50 (double/curly underline both rendering as SGR 4), bug #424 (dead-code inventory that listed Term), bug #500 (Term.setAttributes SGR reset).