Skip to content

Commit

Permalink
Auto merge of #2915 - RalfJung:as_os_str_bytes, r=RalfJung
Browse files Browse the repository at this point in the history
use as_os_str_bytes

Make use of the new operations recently added (tracking issue: rust-lang/rust#111544). At least the "host OsStr to target bytes" direction now works even for non-utf-8 strings on all hosts!
  • Loading branch information
bors committed Jun 3, 2023
2 parents 18662da + a357c8f commit 949b1dc
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 2 additions & 17 deletions src/shims/os_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -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)
}

Expand Down
5 changes: 2 additions & 3 deletions src/shims/unix/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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]
Expand Down

0 comments on commit 949b1dc

Please sign in to comment.