Skip to content

Commit 70aa463

Browse files
authored
add stronger warning to CString::from_raw
1 parent eaf71f8 commit 70aa463

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

src/libstd/ffi/c_str.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,11 @@ impl CString {
225225
/// Retakes ownership of a `CString` that was transferred to C.
226226
///
227227
/// This should only ever be called with a pointer that was earlier
228-
/// obtained by calling `into_raw` on a `CString`. Additionally, the length
229-
/// of the string will be recalculated from the pointer.
228+
/// obtained by calling `into_raw` on a `CString`. In particular, using this method
229+
/// to create a `CString` pointing at memory that will be freed by other code
230+
/// (such as a C library) will lead to undefined behavior!
231+
///
232+
/// Additionally, the length of the string will be recalculated from the pointer.
230233
#[stable(feature = "cstr_memory", since = "1.4.0")]
231234
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
232235
let len = libc::strlen(ptr) + 1; // Including the NUL byte

0 commit comments

Comments
 (0)