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>
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