terminal/c: fix crash on invalid enum in C API get functions#2
Closed
deblasis wants to merge 1 commit into
Closed
Conversation
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>
Owner
Author
|
Cherry-picked into Windows stack branch |
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. |
This was referenced May 19, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
All
getfunctions in the C API crash when called with.invalidand a null output pointer. This affectsbuild_info,cell,render(3 functions),row, andterminal- 7 functions total.Also
colors_getnever copies partial palette entries even though the code to do so exists.Why it crashes
The
getfunctions useinline elseon an enum switch to dispatch to a typed helper.inline elsegenerates code for every variant, including.invalid. When.invalidis used withnullas the output pointer:This casts null to a non-optional
*void- safety panic. The typed helper would just return.invalid_valuewithout touching the pointer, but we never get there.Fix
Return early before the switch for
.invalid, mark itunreachablein the switch soinline elsedoesn't generate code for it:For
colors_get: removed thestructSizedFieldFitsguard 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 invalidtests that were crashing now pass.Notes
Draft tracking PR on the fork. Will clean up before submitting upstream.