Skip to content

Commit

Permalink
Rollup merge of rust-lang#41120 - clarcharr:c_str_transmute, r=alexcr…
Browse files Browse the repository at this point in the history
…ichton

Remove some CStr transmutes.

Because dedicated methods exist for these, we don't have to add other transmutes.
  • Loading branch information
frewsxcv committed Apr 7, 2017
2 parents 2a92fe2 + 9ffb545 commit 25518db
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/libstd/ffi/c_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ impl ops::Deref for CString {
type Target = CStr;

fn deref(&self) -> &CStr {
unsafe { mem::transmute(self.as_bytes_with_nul()) }
unsafe { CStr::from_bytes_with_nul_unchecked(self.as_bytes_with_nul()) }
}
}

Expand Down Expand Up @@ -583,7 +583,8 @@ impl CStr {
#[stable(feature = "rust1", since = "1.0.0")]
pub unsafe fn from_ptr<'a>(ptr: *const c_char) -> &'a CStr {
let len = libc::strlen(ptr);
mem::transmute(slice::from_raw_parts(ptr, len as usize + 1))
let ptr = ptr as *const u8;
CStr::from_bytes_with_nul_unchecked(slice::from_raw_parts(ptr, len as usize + 1))
}

/// Creates a C string wrapper from a byte slice.
Expand Down

0 comments on commit 25518db

Please sign in to comment.