提交 f45283b9 编写于 作者: G Guillaume Gomez 提交者: GitHub

Rollup merge of #36018 - durka:patch-28, r=steveklabnik

strengthen doc warning about CString::from_raw

Saw unsound code using this function on IRC.
......@@ -228,9 +228,14 @@ pub unsafe fn from_vec_unchecked(mut v: Vec<u8>) -> CString {
/// Retakes ownership of a `CString` that was transferred to C.
///
/// Additionally, the length of the string will be recalculated from the pointer.
///
/// # Safety
///
/// This should only ever be called with a pointer that was earlier
/// obtained by calling `into_raw` on a `CString`. Additionally, the length
/// of the string will be recalculated from the pointer.
/// obtained by calling `into_raw` on a `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.
#[stable(feature = "cstr_memory", since = "1.4.0")]
pub unsafe fn from_raw(ptr: *mut c_char) -> CString {
let len = libc::strlen(ptr) + 1; // Including the NUL byte
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册