-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Update safety documentation for CString::from_ptr
and str::from_boxed_utf8_unchecked
#137714
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
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
library/alloc/src/ffi/c_str.rs
Outdated
/// obtained by calling [`CString::into_raw`]. Other usage (e.g., trying to take | ||
/// ownership of a string that was allocated by foreign code) is likely to lead | ||
/// to undefined behavior or allocator corruption. | ||
/// obtained by calling [`CString::into_raw`] and this pointer must not be accessed |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not the pointer, but the memory it points too
library/alloc/src/str.rs
Outdated
/// | ||
/// * The provided bytes must contain a valid UTF-8 sequence. | ||
/// | ||
/// * The `Box<[u8]>` must have been allocated via the global allocator. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not a safety requirement. Box<[u8]>
is really Box<[u8], Global>
, so passing a box with a custom allocator to this function is prevented by type check.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right. I will fix these.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One nit and please squash, then lgtm
r? tgross35
CString::from_ptr
and str::from_boxed_utf8_unchecked
Requested reviewer is already assigned to this pull request. Please choose another assignee. |
You should drop me as a coauthor from the commit message, suggesting a comma in review doesn't mean I had anything to do with authoring this patch :) |
Done :) @tgross35 |
Thanks! @bors r+ rollup |
…iaskrgr Rollup of 8 pull requests Successful merges: - rust-lang#137439 (Stabilise `std::ffi::c_str`) - rust-lang#137714 (Update safety documentation for `CString::from_ptr` and `str::from_boxed_utf8_unchecked`) - rust-lang#139031 (Use char::is_whitespace directly in str::trim*) - rust-lang#139090 (fix docs for `Peekable::next_if{_eq}`) - rust-lang#140297 (Update example to use CStr::to_string_lossy) - rust-lang#140330 (Clarified bootstrap optimization "true" argument) - rust-lang#140339 (session: Cleanup `CanonicalizedPath::new`) - rust-lang#140346 (rustc_span: Some hygiene cleanups) r? `@ghost` `@rustbot` modify labels: rollup
Rollup merge of rust-lang#137714 - DiuDiu777:doc-fix, r=tgross35 Update safety documentation for `CString::from_ptr` and `str::from_boxed_utf8_unchecked` ## PR Description This PR addresses missing safety documentation for two APIs: **1. alloc::ffi::CStr::from_raw** - `Alias`: The pointer must not be aliased (accessed via other pointers) during the reconstructed CString's lifetime. - `Owning`: Calling this function twice on the same pointer and creating two objects with overlapping lifetimes, introduces two alive owners of the same memory. This may result in a double-free. - `Dangling`: The prior documentation required the pointer to originate from CString::into_raw, but this constraint is incomplete. A validly sourced pointer can also cause undefined behavior (UB) if it becomes dangling. A simple Poc for this situation: ``` use std::ffi::CString; use std::os::raw::c_char; fn create_dangling() -> *mut c_char { let local_ptr: *mut c_char = { let valid_data = CString::new("valid").unwrap(); valid_data.into_raw() }; unsafe { let _x = CString::from_raw(local_ptr); } local_ptr } fn main() { let dangling = create_dangling(); unsafe {let _y = CString::from_raw(dangling);} // Cause UB! } ``` **2. alloc::str::from_boxed_utf8_unchecked** - `ValidStr`: Bytes must contain a valid UTF-8 sequence.
PR Description
This PR addresses missing safety documentation for two APIs:
1. alloc::ffi::CStr::from_raw
Alias
: The pointer must not be aliased (accessed via other pointers) during the reconstructed CString's lifetime.Owning
: Calling this function twice on the same pointer and creating two objects with overlapping lifetimes, introduces two alive owners of the same memory. This may result in a double-free.Dangling
: The prior documentation required the pointer to originate from CString::into_raw, but this constraint is incomplete. A validly sourced pointer can also cause undefined behavior (UB) if it becomes dangling. A simple Poc for this situation:2. alloc::str::from_boxed_utf8_unchecked
ValidStr
: Bytes must contain a valid UTF-8 sequence.