Moved-from strings make unsafe substr(0, 0) panic in debug builds

perfloop-oss/cpp2rust · Uncatalogued performance

https://perfloop.ai/t/oss/case_pqkwkc8aqd

Verdict

VERIFIED · settled 2026-11-14 · merged as Cpp2Rust/cpp2rust#616

What happened: The check fails before this change and passes with it.

Hypothesis

Unsafe translated code can panic when it takes a zero-length substring of a string that has just been moved from. In C++, a moved-from string is still valid, so `substr(0, 0)` must return an empty string. The translated Rust instead hits an integer underflow in debug builds. A bounded translator test confirms this panic. How often consumers do this, and how far the problem spreads to other string operations, is not measured.

Cause: the unsafe model stores a C++ string as a `Vec<c_char>` whose last element is a terminating zero, so an empty string is `vec![0]`. The move rules use `std::mem::take`; the emitted code shows `let mut target = std::mem::take(&mut s);` for move construction. That leaves `s` as a zero-length vector with no terminator. The unsafe `f1` substring expansion then computes `(*a0).len() - 1`. In debug builds this underflows and panics. The refcount `f1` uses `saturating_sub(1)` instead, so the same input passes there.

Evidence: I extended property `prop_mxvgdp814e` with a public-translator differential check that runs through the repository's own lit format. Toolchain: LLVM 22.1.8, Rust 1.99.0, accepted revision 242534e. The grid has 223 calls per model and build: - one default-empty control; - 142 zero-count calls at every valid position of the 40 words of length 0 to 3 over bytes {0, 97, 255}; - 80 position-zero calls after one move construction or move assignment.

Results: - Unsafe release (opt-level 3, no overflow checks) and both refcount builds completed all 223 calls with matching output. - Unsafe debug (opt-level 0, overflow checks on) passed the control and all 142 fresh-string calls. - It then panicked on the first moved-from case with `attempt to subtract with overflow` at the `len() - 1` line, while native C++ printed its marker, completed and returned an empty string. - The smallest replay is: move-construct from an empty string, then call `substr(0, 0)` on the source. It is stored on the property, and a rerun failed on it without enumerating the grid.

Scope: - Verified: only zero-count substrings of moved-from strings in unsafe debug builds. - Inferred from source, not run: other string rules that compute `len() - 1` on such a vector may also break. Examples are size (`f2`) in both models and `f15`/`f26` in unsafe. In release builds they would wrap to a huge value instead of panicking. - Why the existing suite misses it: the lit harness compiles generated Rust only at opt-level 3, so debug overflow checks never run there. - Not established: production frequency, other moved-from operations, and any performance effect.

Proof target for the Case: add an ordinary lit fixture covering zero-count substr and size after move construction and move assignment. Compare it with native C++, and also compile the generated Rust with overflow checks enabled. The replay must complete without panic, return size zero with a terminating zero, and leave the moved-to payload intact. Fresh-string substring behavior must not change. The stored property check is one way to do this; its layout is provisional, not a requirement.

Upstream and prior work: - Merged https://github.com/Cpp2Rust/cpp2rust/pull/577 changed only substring operand evaluation and kept range clipping as it was. - The string-file hunks in open https://github.com/Cpp2Rust/cpp2rust/pull/579 change only stream-reading `f8`. - Open https://github.com/Cpp2Rust/cpp2rust/pull/583 changes converter argument capture. - Prior findings `hyp_kkz68hhb2t` (overflow from very large counts at nonzero positions), `hyp_84wcez8rs6` (operand evaluation) and `hyp_9zagtxn6w7` (terminator growth) cover different mechanisms. - The user:4 closures of substring receiver-lifetime cases were administrative holds. They concern temporaries, not moved-from receivers.

Change to test: Restore the terminating-zero invariant when a string is moved from: make the string move rules leave a valid empty string (`vec![0]`), not an empty vector, so substr, size and other length-based rules stay well defined. If that broader change is rejected, at least make the unsafe substring rule clamp the logical length the way the refcount rule already does.

Where it lives

perfloop-oss/cpp2rust

Evidence

The check fails before this change and passes with it: `After std::string move construction and move assignment, programs translated by cpp2rust in both the unsafe and refcount models, with the generated Rust compiled both at opt-level 3 and with opt-level 0, overflow checks and debug assertions, complete without panic and print the same stdout, stderr and exit status as native C++ for tests/unit/string_moved_from.cpp: the moved-from string has size 0, a terminating zero and an empty substr(0, 0), and the moved-to string keeps its contents.`

translated string-move loops · 13 sample pairs

metric baseline candidate paired median change confidence range required result
rotate_refcount_ms 40.2 87.55 +116.8% (+46.96) +46.04 to +52.77 loss ≤ stated 2.5 × |baseline median| PASSED
rotate_unsafe_ms 5.1 15.97 +212.4% (+10.83) +10.32 to +12.47 loss ≤ stated 3.5 × |baseline median| PASSED
push_refcount_ms 54.9 64.82 +17.1% (+9.405) +3.264 to +12.1 loss ≤ stated 0.6 × |baseline median| PASSED

translated string returns · 24 sample pairs

metric baseline candidate paired median change confidence range required result
returns_refcount_ms 46.5 46.46 −0.292 −15.25 to +1.485 loss ≤ 0.05 × |baseline median| PASSED
returns_refcount_allocations 40000003 40000003 0 0 to 0 loss ≤ min(0, 0.05 × |baseline median|) PASSED
returns_unsafe_ms 46.47 46.48 +0.142 −0.939 to +1.858 loss ≤ 0.05 × |baseline median| PASSED
returns_unsafe_allocations 40000002 40000002 0 0 to 0 loss ≤ min(0, 0.05 × |baseline median|) PASSED

translated string-containing record returns · 14 sample pairs

metric baseline candidate paired median change confidence range required result
record_refcount_ms 51.32 66.2 +29.2% (+15.01) +12.63 to +19.12 loss ≤ stated 0.7 × |baseline median| PASSED
record_refcount_allocations 6000003 8000003 +33.3% (+2000000) +2000000 to +2000000 loss ≤ stated 0.4 × |baseline median| PASSED
record_unsafe_ms 7.043 8.914 +26.7% (+1.881) +0.03 to +2.266 loss ≤ stated 0.8 × |baseline median| PASSED
record_unsafe_allocations 4000002 6000002 +50% (+2000000) +2000000 to +2000000 loss ≤ stated 0.6 × |baseline median| PASSED

Checks: 2 of 2 passed. Verification: no defect found.

Timeline