Skip to content

Commit 00d537d

Browse files
committed
deny(unsafe_op_in_unsafe_fn) in libstd/path.rs
1 parent a9025c5 commit 00d537d

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

library/std/src/path.rs

+19-20
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
//! [`push`]: PathBuf::push
6161
6262
#![stable(feature = "rust1", since = "1.0.0")]
63+
#![deny(unsafe_op_in_unsafe_fn)]
6364

6465
use crate::borrow::{Borrow, Cow};
6566
use crate::cmp;
@@ -293,7 +294,8 @@ fn os_str_as_u8_slice(s: &OsStr) -> &[u8] {
293294
unsafe { &*(s as *const OsStr as *const [u8]) }
294295
}
295296
unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr {
296-
&*(s as *const [u8] as *const OsStr)
297+
// SAFETY: see the comment of `os_str_as_u8_slice`
298+
unsafe { &*(s as *const [u8] as *const OsStr) }
297299
}
298300

299301
// Detect scheme on Redox
@@ -313,24 +315,21 @@ fn has_physical_root(s: &[u8], prefix: Option<Prefix<'_>>) -> bool {
313315

314316
// basic workhorse for splitting stem and extension
315317
fn split_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
316-
unsafe {
317-
if os_str_as_u8_slice(file) == b".." {
318-
return (Some(file), None);
319-
}
320-
321-
// The unsafety here stems from converting between &OsStr and &[u8]
322-
// and back. This is safe to do because (1) we only look at ASCII
323-
// contents of the encoding and (2) new &OsStr values are produced
324-
// only from ASCII-bounded slices of existing &OsStr values.
325-
326-
let mut iter = os_str_as_u8_slice(file).rsplitn(2, |b| *b == b'.');
327-
let after = iter.next();
328-
let before = iter.next();
329-
if before == Some(b"") {
330-
(Some(file), None)
331-
} else {
332-
(before.map(|s| u8_slice_as_os_str(s)), after.map(|s| u8_slice_as_os_str(s)))
333-
}
318+
if os_str_as_u8_slice(file) == b".." {
319+
return (Some(file), None);
320+
}
321+
322+
// The unsafety here stems from converting between &OsStr and &[u8]
323+
// and back. This is safe to do because (1) we only look at ASCII
324+
// contents of the encoding and (2) new &OsStr values are produced
325+
// only from ASCII-bounded slices of existing &OsStr values.
326+
let mut iter = os_str_as_u8_slice(file).rsplitn(2, |b| *b == b'.');
327+
let after = iter.next();
328+
let before = iter.next();
329+
if before == Some(b"") {
330+
(Some(file), None)
331+
} else {
332+
unsafe { (before.map(|s| u8_slice_as_os_str(s)), after.map(|s| u8_slice_as_os_str(s))) }
334333
}
335334
}
336335

@@ -1701,7 +1700,7 @@ impl Path {
17011700
// The following (private!) function allows construction of a path from a u8
17021701
// slice, which is only safe when it is known to follow the OsStr encoding.
17031702
unsafe fn from_u8_slice(s: &[u8]) -> &Path {
1704-
Path::new(u8_slice_as_os_str(s))
1703+
unsafe { Path::new(u8_slice_as_os_str(s)) }
17051704
}
17061705
// The following (private!) function reveals the byte encoding used for OsStr.
17071706
fn as_u8_slice(&self) -> &[u8] {

0 commit comments

Comments
 (0)