about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-06-03 20:48:06 +0000
committerbors <bors@rust-lang.org>2023-06-03 20:48:06 +0000
commitc1a7783a149d4bbee1ae203af1e88a436bdc2ec9 (patch)
treec24b6019a79b428f50d1550e4985a6cff56bbd6b
parent9667886737588ee5da531cff67eb9ec27594c621 (diff)
parent7444a5030b8b0c5afe144823256914703a316727 (diff)
downloadrust-c1a7783a149d4bbee1ae203af1e88a436bdc2ec9.tar.gz
rust-c1a7783a149d4bbee1ae203af1e88a436bdc2ec9.zip
Auto merge of #2915 - RalfJung:as_os_str_bytes, r=RalfJung
use as_os_str_bytes

Make use of the new operations recently added (tracking issue: https://github.com/rust-lang/rust/issues/111544). At least the "host OsStr to target bytes" direction now works even for non-utf-8 strings on all hosts!
-rw-r--r--src/tools/miri/src/lib.rs1
-rw-r--r--src/tools/miri/src/shims/os_str.rs19
-rw-r--r--src/tools/miri/src/shims/unix/fs.rs5
3 files changed, 5 insertions, 20 deletions
diff --git a/src/tools/miri/src/lib.rs b/src/tools/miri/src/lib.rs
index f711f01f323..e79bb47c78b 100644
--- a/src/tools/miri/src/lib.rs
+++ b/src/tools/miri/src/lib.rs
@@ -8,6 +8,7 @@
 #![feature(nonzero_ops)]
 #![feature(local_key_cell_methods)]
 #![feature(round_ties_even)]
+#![feature(os_str_bytes)]
 // Configure clippy and other lints
 #![allow(
     clippy::collapsible_else_if,
diff --git a/src/tools/miri/src/shims/os_str.rs b/src/tools/miri/src/shims/os_str.rs
index 6bc5b8f39d5..e85ab7a48b3 100644
--- a/src/tools/miri/src/shims/os_str.rs
+++ b/src/tools/miri/src/shims/os_str.rs
@@ -18,27 +18,12 @@ pub enum PathConversion {
 }
 
 #[cfg(unix)]
-pub fn os_str_to_bytes<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, &[u8]> {
-    Ok(os_str.as_bytes())
-}
-
-#[cfg(not(unix))]
-pub fn os_str_to_bytes<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, &[u8]> {
-    // On non-unix platforms the best we can do to transform bytes from/to OS strings is to do the
-    // intermediate transformation into strings. Which invalidates non-utf8 paths that are actually
-    // valid.
-    os_str
-        .to_str()
-        .map(|s| s.as_bytes())
-        .ok_or_else(|| err_unsup_format!("{:?} is not a valid utf-8 string", os_str).into())
-}
-
-#[cfg(unix)]
 pub fn bytes_to_os_str<'tcx>(bytes: &[u8]) -> InterpResult<'tcx, &OsStr> {
     Ok(OsStr::from_bytes(bytes))
 }
 #[cfg(not(unix))]
 pub fn bytes_to_os_str<'tcx>(bytes: &[u8]) -> InterpResult<'tcx, &OsStr> {
+    // We cannot use `from_os_str_bytes_unchecked` here since we can't trust `bytes`.
     let s = std::str::from_utf8(bytes)
         .map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes))?;
     Ok(OsStr::new(s))
@@ -97,7 +82,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
         ptr: Pointer<Option<Provenance>>,
         size: u64,
     ) -> InterpResult<'tcx, (bool, u64)> {
-        let bytes = os_str_to_bytes(os_str)?;
+        let bytes = os_str.as_os_str_bytes();
         self.eval_context_mut().write_c_str(bytes, ptr, size)
     }
 
diff --git a/src/tools/miri/src/shims/unix/fs.rs b/src/tools/miri/src/shims/unix/fs.rs
index d1b09cd7b55..5f1dc45146e 100644
--- a/src/tools/miri/src/shims/unix/fs.rs
+++ b/src/tools/miri/src/shims/unix/fs.rs
@@ -16,7 +16,6 @@ use rustc_target::abi::{Align, Size};
 
 use crate::shims::os_str::bytes_to_os_str;
 use crate::*;
-use shims::os_str::os_str_to_bytes;
 use shims::time::system_time_to_duration;
 
 #[derive(Debug)]
@@ -1339,7 +1338,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
 
                 let mut name = dir_entry.file_name(); // not a Path as there are no separators!
                 name.push("\0"); // Add a NUL terminator
-                let name_bytes = os_str_to_bytes(&name)?;
+                let name_bytes = name.as_os_str_bytes();
                 let name_len = u64::try_from(name_bytes.len()).unwrap();
 
                 let dirent64_layout = this.libc_ty_layout("dirent64");
@@ -1695,7 +1694,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
                     Cow::Borrowed(resolved.as_ref()),
                     crate::shims::os_str::PathConversion::HostToTarget,
                 );
-                let mut path_bytes = crate::shims::os_str::os_str_to_bytes(resolved.as_ref())?;
+                let mut path_bytes = resolved.as_os_str_bytes();
                 let bufsize: usize = bufsize.try_into().unwrap();
                 if path_bytes.len() > bufsize {
                     path_bytes = &path_bytes[..bufsize]