Skip to content

Commit 33eaa94

Browse files
committed
Remove mem::transmute used in OsStr conversions
1 parent 755fd29 commit 33eaa94

File tree

1 file changed

+10
-7
lines changed

1 file changed

+10
-7
lines changed

src/libstd/ffi/os_str.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ impl OsString {
260260
/// ```
261261
#[stable(feature = "into_boxed_os_str", since = "1.20.0")]
262262
pub fn into_boxed_os_str(self) -> Box<OsStr> {
263-
unsafe { mem::transmute(self.inner.into_box()) }
263+
let rw = Box::into_raw(self.inner.into_box()) as *mut OsStr;
264+
unsafe { Box::from_raw(rw) }
264265
}
265266
}
266267

@@ -394,7 +395,7 @@ impl OsStr {
394395
}
395396

396397
fn from_inner(inner: &Slice) -> &OsStr {
397-
unsafe { mem::transmute(inner) }
398+
unsafe { &*(inner as *const Slice as *const OsStr) }
398399
}
399400

400401
/// Yields a [`&str`] slice if the `OsStr` is valid Unicode.
@@ -511,23 +512,24 @@ impl OsStr {
511512
/// [`OsString`]: struct.OsString.html
512513
#[stable(feature = "into_boxed_os_str", since = "1.20.0")]
513514
pub fn into_os_string(self: Box<OsStr>) -> OsString {
514-
let inner: Box<Slice> = unsafe { mem::transmute(self) };
515-
OsString { inner: Buf::from_box(inner) }
515+
let boxed = unsafe { Box::from_raw(Box::into_raw(self) as *mut Slice) };
516+
OsString { inner: Buf::from_box(boxed) }
516517
}
517518

518519
/// Gets the underlying byte representation.
519520
///
520521
/// Note: it is *crucial* that this API is private, to avoid
521522
/// revealing the internal, platform-specific encodings.
522523
fn bytes(&self) -> &[u8] {
523-
unsafe { mem::transmute(&self.inner) }
524+
&self.inner.inner
524525
}
525526
}
526527

527528
#[stable(feature = "box_from_os_str", since = "1.17.0")]
528529
impl<'a> From<&'a OsStr> for Box<OsStr> {
529530
fn from(s: &'a OsStr) -> Box<OsStr> {
530-
unsafe { mem::transmute(s.inner.into_box()) }
531+
let rw = Box::into_raw(s.inner.into_box()) as *mut OsStr;
532+
unsafe { Box::from_raw(rw) }
531533
}
532534
}
533535

@@ -548,7 +550,8 @@ impl From<OsString> for Box<OsStr> {
548550
#[stable(feature = "box_default_extra", since = "1.17.0")]
549551
impl Default for Box<OsStr> {
550552
fn default() -> Box<OsStr> {
551-
unsafe { mem::transmute(Slice::empty_box()) }
553+
let rw = Box::into_raw(Slice::empty_box()) as *mut OsStr;
554+
unsafe { Box::from_raw(rw) }
552555
}
553556
}
554557

0 commit comments

Comments
 (0)