Skip to content

Commit 0d3d23b

Browse files
durkaAlex Burka
authored and
Alex Burka
committed
add stronger warning to CString::from_raw
1 parent 739d571 commit 0d3d23b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/libstd/ffi/c_str.rs

Lines changed: 7 additions & 2 deletions
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)