Skip to content

Commit 7cbb58c

Browse files
authored
Auto merge of #36018 - durka:patch-28, r=steveklabnik
strengthen doc warning about CString::from_raw Saw unsound code using this function on IRC.
2 parents 22d15ea + 0d3d23b commit 7cbb58c

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/libstd/ffi/c_str.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,14 @@ impl CString {
228228

229229
/// Retakes ownership of a `CString` that was transferred to C.
230230
///
231+
/// Additionally, the length of the string will be recalculated from the pointer.
232+
///
233+
/// # Safety
234+
///
231235
/// This should only ever be called with a pointer that was earlier
232-
/// obtained by calling `into_raw` on a `CString`. Additionally, the length
233-
/// of the string will be recalculated from the pointer.
236+
/// obtained by calling `into_raw` on a `CString`. Other usage (e.g. trying to take
237+
/// ownership of a string that was allocated by foreign code) is likely to lead
238+
/// to undefined behavior or allocator corruption.
234239
#[stable(feature = "cstr_memory", since = "1.4.0")]
235240
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
236241
let len = libc::strlen(ptr) + 1; // Including the NUL byte

0 commit comments

Comments
 (0)