Skip to content

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

Merged
merged 1 commit into from
Apr 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions library/alloc/src/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,14 @@ impl CString {
/// # Safety
///
/// This should only ever be called with a pointer that was earlier
/// 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 the memory it points to must not be accessed
/// through any other pointer during the lifetime of reconstructed `CString`.
/// 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.
///
/// This function does not validate ownership of the raw pointer's memory.
/// A double-free may occur if the function is called twice on the same raw pointer.
/// Additionally, the caller must ensure the pointer is not dangling.
///
/// It should be noted that the length isn't just "recomputed," but that
/// the recomputed length must match the original length from the
Expand Down
4 changes: 4 additions & 0 deletions library/alloc/src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,10 @@ impl str {
/// Converts a boxed slice of bytes to a boxed string slice without checking
/// that the string contains valid UTF-8.
///
/// # Safety
///
/// * The provided bytes must contain a valid UTF-8 sequence.
///
/// # Examples
///
/// ```
Expand Down
Loading