From a357c8fd9429b63cc2fbbd3b22cff40339a421b0 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Sat, 3 Jun 2023 22:46:06 +0200 Subject: [PATCH] use as_os_str_bytes --- src/lib.rs | 1 + src/shims/os_str.rs | 19 ++----------------- src/shims/unix/fs.rs | 5 ++--- 3 files changed, 5 insertions(+), 20 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index f711f01f32..e79bb47c78 100644 --- a/src/lib.rs +++ b/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/shims/os_str.rs b/src/shims/os_str.rs index 6bc5b8f39d..e85ab7a48b 100644 --- a/src/shims/os_str.rs +++ b/src/shims/os_str.rs @@ -17,28 +17,13 @@ pub enum PathConversion { TargetToHost, } -#[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>, 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/shims/unix/fs.rs b/src/shims/unix/fs.rs index d1b09cd7b5..5f1dc45146 100644 --- a/src/shims/unix/fs.rs +++ b/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]