Skip to content

Add Miri regression test for CString::clone_into unwind safety#158763

Closed
Vastargazing wants to merge 1 commit into
rust-lang:mainfrom
Vastargazing:tests/cstring-clone-into-alloc-error
Closed

Add Miri regression test for CString::clone_into unwind safety#158763
Vastargazing wants to merge 1 commit into
rust-lang:mainfrom
Vastargazing:tests/cstring-clone-into-alloc-error

Conversation

@Vastargazing

@Vastargazing Vastargazing commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

Regression test for the panic-unsoundness fixed in #155707

#70201 added a clone_into override for CStr that moved the target CString's buffer out (leaving it empty) before growing a Vec. On allocation failure, the destination CString could lose its trailing nul terminator, leading to UB.
#155707 fixed this but didn't add a test.

The reviewer suggested a #[global_allocator] wrapper in library/alloctests modeled on vec_deque_alloc_error.rs, but that doesn't reproduce the bug: library tests link std with -C prefer-dynamic, so a global allocator in the test binary doesn't intercept the reallocation inside CString::clone_into, which lives in libstd. Under Miri it does, so this adds a Miri pass test that fails one reallocation under a panicking alloc error hook and checks that the target remains valid.

r? @RalfJung

CString::clone_into reuses the target's allocation by moving the buffer
into a Vec and growing it. If that growth's allocation fails and the
alloc error hook unwinds, the target has to be left as a valid CString,
but nothing covered that path.

Add a Miri test that fails the reallocation under a panicking alloc error
hook and checks that the caught unwind leaves the target's nul terminator
in place.
@rustbot

rustbot commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

miri is developed in its own repository. If possible, consider making this change to rust-lang/miri instead.

cc @rust-lang/miri

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 4, 2026
@rustbot

rustbot commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator

RalfJung is not on the review rotation at the moment.
They may take a while to respond.

@oli-obk

oli-obk commented Jul 4, 2026

Copy link
Copy Markdown
Contributor

miri is developed in its own repository. If possible, consider making this change to rust-lang/miri instead.

Please do this, your change has no rustc change requirements

@oli-obk oli-obk closed this Jul 4, 2026
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 4, 2026

@RalfJung RalfJung Jul 4, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please reference the relevant issue(s) from the test in comments.

Also... does this have to be in the Miri test suite? We run the liballoc test suite in Miri on CI.

View changes since the review

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.

4 participants