Skip to content

terminal/c: fix crash on invalid enum in C API get functions#2

Closed
deblasis wants to merge 1 commit into
mainfrom
fix/c-api-invalid-enum-crash
Closed

terminal/c: fix crash on invalid enum in C API get functions#2
deblasis wants to merge 1 commit into
mainfrom
fix/c-api-invalid-enum-crash

Conversation

@deblasis
Copy link
Copy Markdown
Owner

@deblasis deblasis commented Mar 23, 2026

What

All get functions in the C API crash when called with .invalid and a null output pointer. This affects build_info, cell, render (3 functions), row, and terminal - 7 functions total.

Also colors_get never copies partial palette entries even though the code to do so exists.

Why it crashes

The get functions use inline else on an enum switch to dispatch to a typed helper. inline else generates code for every variant, including .invalid. When .invalid is used with null as the output pointer:

@ptrCast(@alignCast(out))  // out is null

This casts null to a non-optional *void - safety panic. The typed helper would just return .invalid_value without touching the pointer, but we never get there.

Fix

Return early before the switch for .invalid, mark it unreachable in the switch so inline else doesn't generate code for it:

if (data == .invalid) return .invalid_value;

return switch (data) {
    .invalid => unreachable,
    inline else => |comptime_data| getTyped(...),
};

For colors_get: removed the structSizedFieldFits guard around the palette copy. That check requires the full 256-entry array to fit, but the inner logic already calculates how many entries fit and copies only those. The guard was preventing it from ever running for truncated structs.

Testing

All existing tests pass on Windows, macOS (arm64), Linux (x86_64). The get invalid tests that were crashing now pass.

Notes

Draft tracking PR on the fork. Will clean up before submitting upstream.

The `get` functions in the C API crash when called with `.invalid`
and a null output pointer. `inline else` generates branches for all
enum variants including `.invalid`, so `@ptrCast(@aligncast(null))`
converts null to a non-optional pointer — safety panic even though
the typed helper never dereferences it.

Fix by returning early for `.invalid` before the switch and marking
it unreachable in the switch so `inline else` skips it.

Also fix colors_get where `structSizedFieldFits` blocks the partial
palette copy. The inner logic already handles truncated sizes correctly
but the outer check requires the full 256-entry palette to fit, so
partial copies never happen.

Affected functions:
- build_info.get
- cell.get
- render.get, render.row_cells_get, render.row_get
- row.get
- terminal.get

Tested on Windows, macOS (arm64), Linux (x86_64).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@deblasis
Copy link
Copy Markdown
Owner Author

Cherry-picked into Windows stack branch 003-windows/cherry-pick-c-api-fix.

@deblasis
Copy link
Copy Markdown
Owner Author

Closing - upstream fixed the same crash in commit 5828352.

We added an early return before the switch then marked .invalid as unreachable inside it. Mitchell just added .invalid => .invalid_value as a single switch arm. Same fix, half the code, all logic in one place.

We also missed osc.zig which had the same pattern. Upstream caught all 6 files.

Lesson: when handling a new enum variant, add it directly in the switch instead of guarding before it. And grep the whole directory when fixing a repeated pattern, not just the files you already know about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant