The signature of CString::new is CString::new(buf: *const i8, owns_buffer: bool). The problem with this is that if the new CString does own the buffer it's going to free it. In which case buf should be a *mut i8 not a *const i8. However making buf: *mut i8 doesn't account for the case where the CString doesn't own the buffer and the buffer is immutable.
To fix this CString::new should be split into two functions:
unsafe fn CString::new(buf: *const i8) -> CString;
unsafe fn CString::new_owned(buf: *mut i8) -> CString;
The signature of
CString::newisCString::new(buf: *const i8, owns_buffer: bool). The problem with this is that if the newCStringdoes own the buffer it's going to free it. In which casebufshould be a*mut i8not a*const i8. However makingbuf: *mut i8doesn't account for the case where theCStringdoesn't own the buffer and the buffer is immutable.To fix this
CString::newshould be split into two functions: