Skip to content

Commit 48c20b6

Browse files
committed
Remove mem::transmute used in Path conversions
1 parent 627998e commit 48c20b6

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

src/libstd/path.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,10 @@ fn iter_after<A, I, J>(mut iter: I, mut prefix: J) -> Option<I>
317317

318318
// See note at the top of this module to understand why these are used:
319319
fn os_str_as_u8_slice(s: &OsStr) -> &[u8] {
320-
unsafe { mem::transmute(s) }
320+
unsafe { &*(s as *const OsStr as *const [u8]) }
321321
}
322322
unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr {
323-
mem::transmute(s)
323+
&*(s as *const [u8] as *const OsStr)
324324
}
325325

326326
// Detect scheme on Redox
@@ -1334,15 +1334,16 @@ impl PathBuf {
13341334
/// [`Path`]: struct.Path.html
13351335
#[stable(feature = "into_boxed_path", since = "1.20.0")]
13361336
pub fn into_boxed_path(self) -> Box<Path> {
1337-
unsafe { mem::transmute(self.inner.into_boxed_os_str()) }
1337+
let rw = Box::into_raw(self.inner.into_boxed_os_str()) as *mut Path;
1338+
unsafe { Box::from_raw(rw) }
13381339
}
13391340
}
13401341

13411342
#[stable(feature = "box_from_path", since = "1.17.0")]
13421343
impl<'a> From<&'a Path> for Box<Path> {
13431344
fn from(path: &'a Path) -> Box<Path> {
1344-
let boxed: Box<OsStr> = path.inner.into();
1345-
unsafe { mem::transmute(boxed) }
1345+
let rw = Box::into_raw(Box::from(&path.inner)) as *mut Path;
1346+
unsafe { Box::from_raw(rw) }
13461347
}
13471348
}
13481349

@@ -1589,7 +1590,7 @@ impl Path {
15891590
/// ```
15901591
#[stable(feature = "rust1", since = "1.0.0")]
15911592
pub fn new<S: AsRef<OsStr> + ?Sized>(s: &S) -> &Path {
1592-
unsafe { mem::transmute(s.as_ref()) }
1593+
unsafe { &*(s.as_ref() as *const OsStr as *const Path) }
15931594
}
15941595

15951596
/// Yields the underlying [`OsStr`] slice.
@@ -2312,8 +2313,8 @@ impl Path {
23122313
/// [`PathBuf`]: struct.PathBuf.html
23132314
#[stable(feature = "into_boxed_path", since = "1.20.0")]
23142315
pub fn into_path_buf(self: Box<Path>) -> PathBuf {
2315-
let inner: Box<OsStr> = unsafe { mem::transmute(self) };
2316-
PathBuf { inner: OsString::from(inner) }
2316+
let rw = Box::into_raw(self) as *mut OsStr;
2317+
unsafe { Box::from_raw(rw) }
23172318
}
23182319
}
23192320

0 commit comments

Comments
 (0)