Skip to content

Commit 44be933

Browse files
authored
Rollup merge of #73963 - hellow554:unsafe_path, r=Mark-Simulacrum
deny(unsafe_op_in_unsafe_fn) in libstd/path.rs The libstd/path.rs part of #73904 . Wraps the two calls to an unsafe fn Initializer::nop() in an unsafe block.
2 parents 4e8a8b4 + 00d537d commit 44be933

File tree

1 file changed

+18
-19
lines changed

1 file changed

+18
-19
lines changed

library/std/src/path.rs

+18-19
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
//! [`push`]: PathBuf::push
5959
6060
#![stable(feature = "rust1", since = "1.0.0")]
61+
#![deny(unsafe_op_in_unsafe_fn)]
6162

6263
#[cfg(test)]
6364
mod tests;
@@ -294,7 +295,8 @@ fn os_str_as_u8_slice(s: &OsStr) -> &[u8] {
294295
unsafe { &*(s as *const OsStr as *const [u8]) }
295296
}
296297
unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr {
297-
&*(s as *const [u8] as *const OsStr)
298+
// SAFETY: see the comment of `os_str_as_u8_slice`
299+
unsafe { &*(s as *const [u8] as *const OsStr) }
298300
}
299301

300302
// Detect scheme on Redox
@@ -314,24 +316,21 @@ fn has_physical_root(s: &[u8], prefix: Option<Prefix<'_>>) -> bool {
314316

315317
// basic workhorse for splitting stem and extension
316318
fn split_file_at_dot(file: &OsStr) -> (Option<&OsStr>, Option<&OsStr>) {
317-
unsafe {
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.
319+
if os_str_as_u8_slice(file) == b".." {
320+
return (Some(file), None);
321+
}
326322

327-
let mut iter = os_str_as_u8_slice(file).rsplitn(2, |b| *b == b'.');
328-
let after = iter.next();
329-
let before = iter.next();
330-
if before == Some(b"") {
331-
(Some(file), None)
332-
} else {
333-
(before.map(|s| u8_slice_as_os_str(s)), after.map(|s| u8_slice_as_os_str(s)))
334-
}
323+
// The unsafety here stems from converting between &OsStr and &[u8]
324+
// and back. This is safe to do because (1) we only look at ASCII
325+
// contents of the encoding and (2) new &OsStr values are produced
326+
// only from ASCII-bounded slices of existing &OsStr values.
327+
let mut iter = os_str_as_u8_slice(file).rsplitn(2, |b| *b == b'.');
328+
let after = iter.next();
329+
let before = iter.next();
330+
if before == Some(b"") {
331+
(Some(file), None)
332+
} else {
333+
unsafe { (before.map(|s| u8_slice_as_os_str(s)), after.map(|s| u8_slice_as_os_str(s))) }
335334
}
336335
}
337336

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

0 commit comments

Comments
 (0)