Skip to content

Commit 6e75a50

Browse files
committed
deny(unsafe_op_in_unsafe_fn) in libstd/path.rs
1 parent 9491f18 commit 6e75a50

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/libstd/path.rs

+19-20
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
//! [`OsStr`]: ../../std/ffi/struct.OsStr.html
6969
7070
#![stable(feature = "rust1", since = "1.0.0")]
71+
#![deny(unsafe_op_in_unsafe_fn)]
7172

7273
use crate::borrow::{Borrow, Cow};
7374
use crate::cmp;
@@ -301,7 +302,8 @@ fn os_str_as_u8_slice(s: &OsStr) -> &[u8] {
301302
unsafe { &*(s as *const OsStr as *const [u8]) }
302303
}
303304
unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr {
304-
&*(s as *const [u8] as *const OsStr)
305+
// SAFETY: see the comment of `os_str_as_u8_slice`
306+
unsafe { &*(s as *const [u8] as *const OsStr) }
305307
}
306308

307309
// Detect scheme on Redox
@@ -321,24 +323,21 @@ fn has_physical_root(s: &[u8], prefix: Option<Prefix<'_>>) -> bool {
321323

322324
// basic workhorse for splitting stem and extension
323325
fn split_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
324-
unsafe {
325-
if os_str_as_u8_slice(file) == b".." {
326-
return (Some(file), None);
327-
}
328-
329-
// The unsafety here stems from converting between &OsStr and &[u8]
330-
// and back. This is safe to do because (1) we only look at ASCII
331-
// contents of the encoding and (2) new &OsStr values are produced
332-
// only from ASCII-bounded slices of existing &OsStr values.
333-
334-
let mut iter = os_str_as_u8_slice(file).rsplitn(2, |b| *b == b'.');
335-
let after = iter.next();
336-
let before = iter.next();
337-
if before == Some(b"") {
338-
(Some(file), None)
339-
} else {
340-
(before.map(|s| u8_slice_as_os_str(s)), after.map(|s| u8_slice_as_os_str(s)))
341-
}
326+
if os_str_as_u8_slice(file) == b".." {
327+
return (Some(file), None);
328+
}
329+
330+
// The unsafety here stems from converting between &OsStr and &[u8]
331+
// and back. This is safe to do because (1) we only look at ASCII
332+
// contents of the encoding and (2) new &OsStr values are produced
333+
// only from ASCII-bounded slices of existing &OsStr values.
334+
let mut iter = os_str_as_u8_slice(file).rsplitn(2, |b| *b == b'.');
335+
let after = iter.next();
336+
let before = iter.next();
337+
if before == Some(b"") {
338+
(Some(file), None)
339+
} else {
340+
unsafe { (before.map(|s| u8_slice_as_os_str(s)), after.map(|s| u8_slice_as_os_str(s))) }
342341
}
343342
}
344343

@@ -1755,7 +1754,7 @@ impl Path {
17551754
// The following (private!) function allows construction of a path from a u8
17561755
// slice, which is only safe when it is known to follow the OsStr encoding.
17571756
unsafe fn from_u8_slice(s: &[u8]) -> &Path {
1758-
Path::new(u8_slice_as_os_str(s))
1757+
unsafe { Path::new(u8_slice_as_os_str(s)) }
17591758
}
17601759
// The following (private!) function reveals the byte encoding used for OsStr.
17611760
fn as_u8_slice(&self) -> &[u8] {

0 commit comments

Comments
 (0)