substr on a member of a temporary must not produce Rust that panics with a dangling pointer

perfloop-oss/cpp2rust · Premature Release

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

Verdict

VERIFIED · settled 2026-11-07 · merged as Cpp2Rust/cpp2rust#614

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

Hypothesis

The submitter reports that taking a substring from a string member of a temporary object produces refcount Rust that panics with a dangling pointer. The generated owner may be dropped before the substring reads the field, although C++ keeps the temporary alive through the enclosing full expression. This run has not reproduced the reported failure; its prevalence is unknown.

The edit subject is ConverterRefCount::ConvertPointerReceiver at cpp2rust/converter/models/converter_refcount.cpp:403-410, at accepted revision 35175877340944e7645791b221558d45e69fd6b8. It explicitly handles a bare MaterializeTemporaryExpr after ignoring implicit casts, but otherwise uses ordinary object conversion. Boxed field address conversion emits field_ref!(...).as_pointer() at the same file:1838-1855. The macro introduces a block at libcc2rs/src/field.rs:589-599, and later field access upgrades a weak root at that file:1242-1245. The precise concern is a non-arrow member chain rooted at a temporary, not substring range arithmetic or a change to the unsafe model.

For makeS().s.substr(7, 6), the submitter reports this emitted shape: `match (field_ref!(Rc::new(RefCell::new(({ makeS_1() }))).as_pointer(), s).as_pointer(), 7_usize, 6_usize) { (__source, ..) => __source.with(...) }`. They say the owning Rc drops at the end of the field_ref! block, leaving __source dangling. Their lit test prints `member-of-temp [member] 6` natively and at base eeabae779177, but generated Rust at the reported head panics with `ub: dangling pointer` at src/field.rs:1244. These are attributed, unconfirmed results for the Case to check. The submitter attributes the regression to upstream PR #577, merged as 7cab049a498a, and reports that it remains here and on upstream main.

The smallest useful check is a native-versus-translated lit reproduction of that member expression, inspecting the generated owner scope and comparing output, length and panic outcome. Native/base success with a refcount-head dangling-pointer panic would confirm the reported regression. Equivalent head output with a demonstrably live owner would reject that reproducer. Extend coverage to nested members and chained substr on a temporary in both models. The submitter also reports that #577 fixed six previously failing substr shapes; all six must keep passing, alongside unchanged bare-temporary, lvalue and unsafe-model behavior. The requested full check suite is the check target at CMakeLists.txt:131-163, including runtime and macro tests plus lit unit, UB and multi-file tests.

Fresh upstream reads confirm that https://github.com/Cpp2Rust/cpp2rust/pull/577 merged at 7cab049a498aecdb6bb67d65985d14a3df426135. It addressed duplicated operand effects from https://github.com/Cpp2Rust/cpp2rust/issues/575 by capturing operands and borrowing after argument evaluation. Its temporary, pointer, field and chained receiver controls in tests/unit/string_substr.cpp:20-56 do not cover the requested value member of a temporary. The current accepted rule at rules/string/tgt_refcount.rs:28-34 directly calls a0.with, with generic operand binding in converter.cpp:5132-5205, so the exact quoted expansion must also be checked at this revision.

The current open-PR page shows converter overlap with https://github.com/Cpp2Rust/cpp2rust/pull/583, an open try_emplace argument-binding proposal, not an accepted replacement. Its selected converter hunks do not change this receiver routine. The remaining difference is temporary-member ownership while retaining #577's repaired behavior. The upstream page and selected patches are bounded evidence; persistence on upstream main has not been independently reproduced here.

Change to test: Give refcount non-arrow member-chain pointer receivers rooted at temporaries their own temporary Rc through the enclosing statement or match; leave bare temporaries, lvalues and unsafe conversion unchanged. Add lit coverage for member, nested-member and chained substr on temporaries in both models, preserve #577's six repaired shapes, and run the full check suite.

Where it lives

perfloop-oss/cpp2rust · cpp2rust/converter/converter.cpp

Evidence

The check fails before this change and passes with it: `When substr is called on a std::string field of a temporary struct returned by a function, either directly, through a field of a nested struct, through parentheses, with position and count arguments that read the counter the function increments, or followed by a second substr, the refcount and unsafe translations each print the same substring, length and call count as the native program, without panicking with ub: dangling pointer.`

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

Timeline