Skip to content

Avoid overlapping borrows in repeated-character append - #580

Merged
nunoplopes merged 3 commits into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-r6avpgfenj
Nov 2, 2026
Merged

nunoplopes merged 3 commits into
Cpp2Rust:masterfrom
perfloop-oss:perfloop-pr-open-r6avpgfenj

Conversation

@perfloop-agent

@perfloop-agent perfloop-agent commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor

Repeated-character string append now reads the target length before borrowing the receiver for resizing. This avoids a checked-borrow panic for strings whose data pointer is used, including calls that ignore the result.

  • Save the post-pop length plus the append count in a local before resizing in rules/string/tgt_refcount.rs.
  • Keep the trailing-zero update and the existing result clone.
  • Use the size type explicitly in the fixture's modulo expressions so its generated-source expectations do not depend on the standard library's type spelling for sizeof.

Runtime borrow checking and the unsafe-model rule are unchanged. This repairs the receiver's own length read; it does not change append's return-reference semantics or operand evaluation.

Testing

tests/unit/string_repeat_append.cpp checks size, receiver bytes and the trailing zero over a bounded grid of initial lengths, counts and byte values, with and without shrinking first. It includes empty strings, zero counts, embedded NULs and growth, plus unboxed-local, reference and pointer receivers. The scoped lit runner passes four tests with no failures or skips, comparing native C++ with both output models and checking their generated-source expectations. The existing string fixture checks the surrounding string rules.

Coverage is bounded; application frequency remains unmeasured.

4 recorded checks pass with this change.

  • Scoped string lit tests
  • Rules clippy
  • String rule and expected-output rustfmt
  • Append fixture clang-format

Case and evidence


Made by Perfloop. Human sponsor: Tomás Senart.

Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Tomás Senart <ts@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Tomás Senart <ts@perfloop.ai>
Signed-off-by: Perfloop Agent <agent@perfloop.ai>
Signed-off-by: Tomás Senart <ts@perfloop.ai>
@nunoplopes
nunoplopes merged commit 5a7ee97 into Cpp2Rust:master Nov 2, 2026
9 checks passed
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.

2 participants