We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
unsafe
1 parent 76d733e commit 298f8eeCopy full SHA for 298f8ee
src/libstd/ffi/c_str.rs
@@ -599,7 +599,11 @@ impl CString {
599
/// ```
600
#[stable(feature = "into_boxed_c_str", since = "1.20.0")]
601
pub fn into_boxed_c_str(self) -> Box<CStr> {
602
- unsafe { Box::from_raw(Box::into_raw(self.into_inner()) as *mut CStr) }
+ let ptr: *mut [u8] = Box::into_raw(self.into_inner());
603
+ // SAFETY: Casting away the length information is fine as `CStr`'s length computation works
604
+ // by counting the elements up to the first null. Since this `CString` must be null
605
+ // terminated, the `CStr` can properly compute its length
606
+ unsafe { Box::from_raw(ptr as *mut CStr) }
607
}
608
609
/// Bypass "move out of struct which implements [`Drop`] trait" restriction.
0 commit comments