Skip to content

Initialize empty std::string values with a null terminator - #478

Merged
nunoplopes merged 2 commits into
masterfrom
copilot/fix-std-string-initialization
Oct 7, 2026
Merged

nunoplopes merged 2 commits into
masterfrom
copilot/fix-std-string-initialization

Conversation

Copilot AI commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor

The std::string rules model strings as null-terminated vectors, but default initialization produced an empty vector. Operations such as length() then incorrectly assume a terminator exists.

  • Type rules: Initialize default strings with vec![0] in both unsafe and refcount models.
  • Regression coverage: Add a default-constructed string case that checks length().
std::string empty;
assert(empty.length() == 0);

Copilot AI linked an issue Oct 7, 2026 that may be closed by this pull request
Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix incorrect std::string rule initialization Initialize empty std::string values with a null terminator Oct 7, 2026
Copilot AI requested a review from nunoplopes October 7, 2026 18:46
@nunoplopes
nunoplopes marked this pull request as ready for review October 7, 2026 18:48
@nunoplopes
nunoplopes merged commit f467a98 into master Oct 7, 2026
9 checks passed
@nunoplopes
nunoplopes deleted the copilot/fix-std-string-initialization branch October 7, 2026 19:54
joaotgouveia pushed a commit to joaotgouveia/cpp2rust that referenced this pull request Oct 27, 2026
…t#478)

The `std::string` rules model strings as null-terminated vectors, but
default initialization produced an empty vector. Operations such as
`length()` then incorrectly assume a terminator exists.

- **Type rules:** Initialize default strings with `vec![0]` in both
unsafe and refcount models.
- **Regression coverage:** Add a default-constructed string case that
checks `length()`.

```cpp
std::string empty;
assert(empty.length() == 0);
```

<!-- START COPILOT CODING AGENT SUFFIX -->

- Fixes Cpp2Rust#455

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Nuno Lopes <nuno.lopes@tecnico.ulisboa.pt>
nunoplopes pushed a commit that referenced this pull request Nov 15, 2026
String take expansions in both models now leave `vec![0]`, reusing the
default initializer from #478. Previously, `std::mem::take` left no
terminator: `size()` wrapped and the unsafe substring length could
underflow. C++ leaves a moved-from string valid with an unspecified
value; choosing empty matches libstdc++ and libc++.

Implicit returns of locals and by-value string parameters keep `take`,
avoiding a replacement allocation; destructors inspecting that returned
local or parameter still see the old empty vector. Returns that move
from reference parameters still reset the source. Explicit `return
std::move(x)` still resets x and pays the allocation, because a local
destructor can observe x after an explicit move.

**Cost:** each reset allocates a one-byte buffer. Generated memberwise
move constructors for user records also pay this per moved string field,
including when returning a record. The cost is not confined to move-only
loops.

**Remaining:** moves made by the runtime's generic move remain unfixed:
`std::pair<std::string,T>` (including moved-pair `push_back`),
`std::array<std::string,N>`, and `std::map::operator[]` with a moved
key.

`tests/unit/string_moved_from.cpp` checks size, substring, terminators,
moved-to payloads and return paths against native C++.

[Case and evidence](<https://app.perfloop.ai/t/oss/case_pqkwkc8aqd>)

---

Made by [Perfloop](https://app.perfloop.ai). Human sponsor: [Tomás
Senart](https://github.com/tsenart).

---------

Signed-off-by: Tomás Senart <ts@perfloop.ai>
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.

Incorrect std::string rule initialization

2 participants