From 33eaa94f90932b282cd81ff17ff6de287c34d4c7 Mon Sep 17 00:00:00 2001 From: Nikolai Vazquez Date: Thu, 28 Sep 2017 08:07:49 -0400 Subject: Remove mem::transmute used in OsStr conversions --- src/libstd/ffi/os_str.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 'src/libstd') diff --git a/src/libstd/ffi/os_str.rs b/src/libstd/ffi/os_str.rs index a40a9329ed9..e7558c3c022 100644 --- a/src/libstd/ffi/os_str.rs +++ b/src/libstd/ffi/os_str.rs @@ -260,7 +260,8 @@ impl OsString { /// ``` #[stable(feature = "into_boxed_os_str", since = "1.20.0")] pub fn into_boxed_os_str(self) -> Box { - unsafe { mem::transmute(self.inner.into_box()) } + let rw = Box::into_raw(self.inner.into_box()) as *mut OsStr; + unsafe { Box::from_raw(rw) } } } @@ -394,7 +395,7 @@ impl OsStr { } fn from_inner(inner: &Slice) -> &OsStr { - unsafe { mem::transmute(inner) } + unsafe { &*(inner as *const Slice as *const OsStr) } } /// Yields a [`&str`] slice if the `OsStr` is valid Unicode. @@ -511,8 +512,8 @@ impl OsStr { /// [`OsString`]: struct.OsString.html #[stable(feature = "into_boxed_os_str", since = "1.20.0")] pub fn into_os_string(self: Box) -> OsString { - let inner: Box = unsafe { mem::transmute(self) }; - OsString { inner: Buf::from_box(inner) } + let boxed = unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) }; + OsString { inner: Buf::from_box(boxed) } } /// Gets the underlying byte representation. @@ -520,14 +521,15 @@ impl OsStr { /// Note: it is *crucial* that this API is private, to avoid /// revealing the internal, platform-specific encodings. fn bytes(&self) -> &[u8] { - unsafe { mem::transmute(&self.inner) } + &self.inner.inner } } #[stable(feature = "box_from_os_str", since = "1.17.0")] impl<'a> From<&'a OsStr> for Box { fn from(s: &'a OsStr) -> Box { - unsafe { mem::transmute(s.inner.into_box()) } + let rw = Box::into_raw(s.inner.into_box()) as *mut OsStr; + unsafe { Box::from_raw(rw) } } } @@ -548,7 +550,8 @@ impl From for Box { #[stable(feature = "box_default_extra", since = "1.17.0")] impl Default for Box { fn default() -> Box { - unsafe { mem::transmute(Slice::empty_box()) } + let rw = Box::into_raw(Slice::empty_box()) as *mut OsStr; + unsafe { Box::from_raw(rw) } } } -- cgit 1.4.1-3-g733a5