-
Notifications
You must be signed in to change notification settings - Fork 13.3k
CString being noalias is a footgun #136770
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Only by making assumptions about If you base your assumptions on the exceptional case instead of the general case then you'll run into problems... so that intuition seems wrong. |
@the8472
This doesn't actually say anything about noalias. In particular, it doesn't say whether you're allowed to move the |
Hrm right, I'm not sure if the extra grace that Vec provides is guaranteed or just a "let's not gratuitously break people who relied on this". But my point stands, the norm is that moving invalidates and you need an exception to do this. If your intuition is that this is generally ok then the intuition needs fixing. I think a better argument might be saying that this is useful and asking for a guarantee. But that'd need a better demonstration what this is useful for, why does it get moved? |
See also rust-lang/rfcs#3712 |
At least
|
For what it's worth, I've found an instance of rust/library/std/src/sys/pal/unix/process/process_common.rs Lines 417 to 419 in 021fb9c
Because the |
I've created a new Zulip topic on this issue. |
For what it's worth, this fails Stacked Borrows but is not necessarily UB as the exact model for Rust is still undetermined. cc @RalfJung |
Good catch! We have generally strived to keep Cc @rust-lang/opsem |
Internally,
String
is implemented as aVec<u8>
. However,CString
is implemented as aBox<[u8]>
.This means that
CString
is currently treated asnoalias
, likeBox
(but notVec
) is. (See rust-lang/unsafe-code-guidelines#326.) This means that there are additional rules of how pointers pointing into theCString
buffer can be used, and violating these rules lead to UB. As far as I can tell, the fact that this applies toCString
is not documented anywhere.I don't know if this has produced any issues in practice, but I believe that this is an undesirable footgun that should be eliminated (e.g., by changing
CString
to useNonNull<[u8]>
instead), especially sinceCString
is intended to be turned into a pointer and passed to FFI, potentially resulting in subtle UB that Miri can't detect.For example, consider the following code, which is a stand-in for how a program with C FFI might use
CString
:This code might seem intuitively fine to many Rust users, but it causes UB according to Miri.
Error from Miri
(It's debatable whether
CString
not having extra capacity is good or not, but that's a separate issue.)@rustbot labels +A-FFI +A-box +T-libs-api +T-opsem
Meta
The Miri error was from running the code on the playground with rust version
1.86.0-nightly (2025-02-08 43ca9d18e333797f0aa3)
.The text was updated successfully, but these errors were encountered: