Skip to content

Commit c1a7783

Browse files
committed
Auto merge of rust-lang#2915 - RalfJung:as_os_str_bytes, r=RalfJung
use as_os_str_bytes Make use of the new operations recently added (tracking issue: rust-lang#111544). At least the "host OsStr to target bytes" direction now works even for non-utf-8 strings on all hosts!
2 parents 9667886 + 7444a50 commit c1a7783

File tree

3 files changed

+5
-20
lines changed

3 files changed

+5
-20
lines changed

src/tools/miri/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#![feature(nonzero_ops)]
99
#![feature(local_key_cell_methods)]
1010
#![feature(round_ties_even)]
11+
#![feature(os_str_bytes)]
1112
// Configure clippy and other lints
1213
#![allow(
1314
clippy::collapsible_else_if,

src/tools/miri/src/shims/os_str.rs

+2-17
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,13 @@ pub enum PathConversion {
1717
TargetToHost,
1818
}
1919

20-
#[cfg(unix)]
21-
pub fn os_str_to_bytes<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, &[u8]> {
22-
Ok(os_str.as_bytes())
23-
}
24-
25-
#[cfg(not(unix))]
26-
pub fn os_str_to_bytes<'tcx>(os_str: &OsStr) -> InterpResult<'tcx, &[u8]> {
27-
// On non-unix platforms the best we can do to transform bytes from/to OS strings is to do the
28-
// intermediate transformation into strings. Which invalidates non-utf8 paths that are actually
29-
// valid.
30-
os_str
31-
.to_str()
32-
.map(|s| s.as_bytes())
33-
.ok_or_else(|| err_unsup_format!("{:?} is not a valid utf-8 string", os_str).into())
34-
}
35-
3620
#[cfg(unix)]
3721
pub fn bytes_to_os_str<'tcx>(bytes: &[u8]) -> InterpResult<'tcx, &OsStr> {
3822
Ok(OsStr::from_bytes(bytes))
3923
}
4024
#[cfg(not(unix))]
4125
pub fn bytes_to_os_str<'tcx>(bytes: &[u8]) -> InterpResult<'tcx, &OsStr> {
26+
// We cannot use `from_os_str_bytes_unchecked` here since we can't trust `bytes`.
4227
let s = std::str::from_utf8(bytes)
4328
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes))?;
4429
Ok(OsStr::new(s))
@@ -97,7 +82,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
9782
ptr: Pointer<Option<Provenance>>,
9883
size: u64,
9984
) -> InterpResult<'tcx, (bool, u64)> {
100-
let bytes = os_str_to_bytes(os_str)?;
85+
let bytes = os_str.as_os_str_bytes();
10186
self.eval_context_mut().write_c_str(bytes, ptr, size)
10287
}
10388

src/tools/miri/src/shims/unix/fs.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc_target::abi::{Align, Size};
1616

1717
use crate::shims::os_str::bytes_to_os_str;
1818
use crate::*;
19-
use shims::os_str::os_str_to_bytes;
2019
use shims::time::system_time_to_duration;
2120

2221
#[derive(Debug)]
@@ -1339,7 +1338,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
13391338

13401339
let mut name = dir_entry.file_name(); // not a Path as there are no separators!
13411340
name.push("\0"); // Add a NUL terminator
1342-
let name_bytes = os_str_to_bytes(&name)?;
1341+
let name_bytes = name.as_os_str_bytes();
13431342
let name_len = u64::try_from(name_bytes.len()).unwrap();
13441343

13451344
let dirent64_layout = this.libc_ty_layout("dirent64");
@@ -1695,7 +1694,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
16951694
Cow::Borrowed(resolved.as_ref()),
16961695
crate::shims::os_str::PathConversion::HostToTarget,
16971696
);
1698-
let mut path_bytes = crate::shims::os_str::os_str_to_bytes(resolved.as_ref())?;
1697+
let mut path_bytes = resolved.as_os_str_bytes();
16991698
let bufsize: usize = bufsize.try_into().unwrap();
17001699
if path_bytes.len() > bufsize {
17011700
path_bytes = &path_bytes[..bufsize]

0 commit comments

Comments
 (0)