Skip to content

Commit 186d59a

Browse files
committed
Move some helper functions around
1 parent 78bc89b commit 186d59a

File tree

1 file changed

+29
-29
lines changed

1 file changed

+29
-29
lines changed

src/shims/os_str.rs

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,28 @@ fn convert_path_separator<'a>(
6262

6363
impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriEvalContext<'mir, 'tcx> {}
6464
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx> {
65+
66+
#[cfg(unix)]
67+
fn bytes_to_os_str<'a>(&self, bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
68+
Ok(OsStr::from_bytes(bytes))
69+
}
70+
#[cfg(not(unix))]
71+
fn bytes_to_os_str<'a>(&self, bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
72+
let s = std::str::from_utf8(bytes)
73+
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes))?;
74+
Ok(OsStr::new(s))
75+
}
76+
6577
/// Helper function to read an OsString from a null-terminated sequence of bytes, which is what
6678
/// the Unix APIs usually handle.
6779
fn read_os_str_from_c_str<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, &'a OsStr>
6880
where
6981
'tcx: 'a,
7082
'mir: 'a,
7183
{
72-
#[cfg(unix)]
73-
fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
74-
Ok(OsStr::from_bytes(bytes))
75-
}
76-
#[cfg(not(unix))]
77-
fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
78-
let s = std::str::from_utf8(bytes)
79-
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes))?;
80-
Ok(OsStr::new(s))
81-
}
82-
8384
let this = self.eval_context_ref();
8485
let bytes = this.memory.read_c_str(scalar)?;
85-
bytes_to_os_str(bytes)
86+
self.bytes_to_os_str(bytes)
8687
}
8788

8889
/// Helper function to read an OsString from a 0x0000-terminated sequence of u16,
@@ -107,6 +108,22 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
107108
u16vec_to_osstring(u16_vec)
108109
}
109110

111+
#[cfg(unix)]
112+
fn os_str_to_bytes<'a>(&self, os_str: &'a OsStr) -> InterpResult<'tcx, &'a [u8]> {
113+
Ok(os_str.as_bytes())
114+
}
115+
116+
#[cfg(not(unix))]
117+
fn os_str_to_bytes<'a>(&self, os_str: &'a OsStr) -> InterpResult<'tcx, &'a [u8]> {
118+
// On non-unix platforms the best we can do to transform bytes from/to OS strings is to do the
119+
// intermediate transformation into strings. Which invalidates non-utf8 paths that are actually
120+
// valid.
121+
os_str
122+
.to_str()
123+
.map(|s| s.as_bytes())
124+
.ok_or_else(|| err_unsup_format!("{:?} is not a valid utf-8 string", os_str).into())
125+
}
126+
110127
/// Helper function to write an OsStr as a null-terminated sequence of bytes, which is what
111128
/// the Unix APIs usually handle. This function returns `Ok((false, length))` without trying
112129
/// to write if `size` is not large enough to fit the contents of `os_string` plus a null
@@ -251,21 +268,4 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
251268
let os_str = convert_path_separator(Cow::Borrowed(path.as_os_str()), &this.tcx.sess.target.target.target_os, Pathconversion::HostToTarget);
252269
this.write_os_str_to_wide_str(&os_str, scalar, size)
253270
}
254-
255-
#[cfg(unix)]
256-
fn os_str_to_bytes<'a>(&mut self, os_str: &'a OsStr) -> InterpResult<'tcx, &'a [u8]> {
257-
Ok(os_str.as_bytes())
258-
}
259-
260-
#[cfg(not(unix))]
261-
fn os_str_to_bytes<'a>(&mut self, os_str: &'a OsStr) -> InterpResult<'tcx, &'a [u8]> {
262-
// On non-unix platforms the best we can do to transform bytes from/to OS strings is to do the
263-
// intermediate transformation into strings. Which invalidates non-utf8 paths that are actually
264-
// valid.
265-
os_str
266-
.to_str()
267-
.map(|s| s.as_bytes())
268-
.ok_or_else(|| err_unsup_format!("{:?} is not a valid utf-8 string", os_str).into())
269-
}
270-
271271
}

0 commit comments

Comments
 (0)