From bfdd947bbd5a2d242d0cbd728b3cbf2a30206b74 Mon Sep 17 00:00:00 2001 From: LemonJ <1632798336@qq.com> Date: Thu, 27 Feb 2025 15:18:10 +0800 Subject: [PATCH] fix missing doc in CString::from_raw and str::from_boxed_utf8_unchecked --- library/alloc/src/ffi/c_str.rs | 11 ++++++++--- library/alloc/src/str.rs | 4 ++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index fd93045a5ac4d..5bd5d3ec2c838 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -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 diff --git a/library/alloc/src/str.rs b/library/alloc/src/str.rs index 6fee8d3fe3346..a670bb5008aa6 100644 --- a/library/alloc/src/str.rs +++ b/library/alloc/src/str.rs @@ -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 /// /// ```