Non-final switch default arm reordering silently corrupts fallthrough control flow
perfloop-oss/cpp2rust · Uncatalogued correctness
https://perfloop.ai/t/oss/case_zn6xpkfecf
Verdict
VERIFIED · settled 2026-11-26 · merged as Cpp2Rust/cpp2rust#685
What happened: The check fails before this change and passes with it.
Hypothesis
When translating a C or C++ switch statement whose default branch is not the last arm, the translator silently moves the default arm to the end of the generated Rust switch macro. Because the generated switch state machine executes fallthrough in the order the arms are emitted, this silent reordering breaks control flow in both the default refcount model and the unsafe model whenever fallthrough occurs around a non-final default branch. Translated programs compile cleanly with zero warnings yet execute the wrong branches at runtime. Real-world reach is rare_input (labelled engineering prior: most C/C++ switches place default last, though placing default first or in the middle with fallthrough—or omitting a redundant break on the final case arm after an earlier default—is valid standard C and C++ used in parsers, state machines, and protocol decoders; production prevalence is not measured).
In cpp2rust/converter/converter.cpp, Converter::VisitSwitchStmt (lines 4521-4572) unconditionally defers arm.is_default_case in the emission loop (lines 4556-4569) and emits default_arm last so the Rust wildcard pattern `_ =>` does not shadow later cases. When needs_switch_macro is true (any arm has fallthrough or a goto label), VisitSwitchStmt emits `switch!(match ...)`, which libcc2rs-macros/src/switch.rs and libcc2rs-macros/src/state_machine.rs lower into a GotoStateMachine where arm i falls through to arm i+1 in emitted order. Moving default_arm to the end creates three silent miscompilations: - A non-final default arm that falls through into the following case exits the switch instead of entering that case. - A case arm that falls through into a middle default arm skips default and jumps directly into the case after default. - Even when default has an explicit break, if the final case arm omits its redundant C/C++ break, moving default after it causes that final case to spuriously fall through into default.
A bounded exhaustive public-CLI differential search over all 24 3-arm switch shapes (default at position 0, 1, or 2 times all 2^3 break/fallthrough combinations) across 3 selector inputs {1, 2, 99} and both output models (144 observations total) observed 44 silent runtime mismatches: 22/72 in refcount mode (10 with default first, 12 with default middle, 0/24 with default last) and 22/72 in unsafe mode (identical distribution). In the reduced deterministic counterexample (`default:` falling through into `case 1:`), native C++ returns trace 91 for selector 99, while both emitted refcount and unsafe Rust binaries compile cleanly and return 9 because `_ =>` was emitted after `__v if __v == 1 =>`. Existing unit tests (`tests/unit/switch_default_first.cpp`, `tests/unit/switch_default_middle.cpp`, `tests/unit/switch_default_then_case.cpp`, `tests/unit/switch_fallthrough_default.cpp`) missed this because every non-final default test ended all arms with explicit `break;` and the fallthrough-to-default test placed `default:` last.
The independent proof target is the attached bounded exhaustive differential check (`tests/property/switch_default_fallthrough/check.py`, provisional layout), which replays the reduced counterexample first and then verifies all 144 bounded observations across refcount and unsafe models. Matching native C++ traces across all 144 observations confirms the repair; any mismatch or compilation failure refutes it.
Upstream and prior-finding overlap check: open pull requests #679 (assignment target evaluation) and #683 (rule-preprocessor semantics) do not touch switch translation; prior findings hyp_fe3wb2tna0 and hyp_pbq805r6jw concern nested goto_block!/switch! macro exit propagation and double-walk inside libcc2rs-macros rather than default-arm reordering in Converter::VisitSwitchStmt.
Change to test: Preserve source arm order for switch fallthrough when a default arm is not last, while still keeping the wildcard dispatch arm last during match dispatch (or emitting explicit goto/break transitions so fallthrough never skips or spuriously enters default). Keep non-macro matches and default-last switches unchanged.
Where it lives
perfloop-oss/cpp2rust · cpp2rust/converter/converter.cpp
Evidence
The check fails before this change and passes with it: `For all 24 three-arm switches with default at each position and every break/fallthrough combination, selectors 1, 2 and 99, plus the reduced default-to-case witness, the public CLI's refcount and unsafe translations compile and execute the same traces as native C++.`
Checks: 5 of 5 passed. Verification: no defect found.