Skip to content

Commit fe9ecb5

Browse files
committed
Follow-up to reviews from RalfJung
1. Fix 'fn convert_path_separator' in src/shims/os_str.rs 2. Fix 'fn set_last_error_from_io_error' in src/helpers.rs 3. Minor comment fix for 'fn SetCurrentDirectoryW' in src/shims/env.rs
1 parent 1141b21 commit fe9ecb5

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

src/helpers.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
414414
use std::io::ErrorKind::*;
415415
let this = self.eval_context_mut();
416416
let target = &this.tcx.sess.target.target;
417+
let target_os = &target.target_os;
417418
let last_error = if target.options.target_family == Some("unix".to_owned()) {
418419
this.eval_libc(match e.kind() {
419420
ConnectionRefused => "ECONNREFUSED",
@@ -434,15 +435,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
434435
throw_unsup_format!("io error {} cannot be transformed into a raw os error", e)
435436
}
436437
})?
437-
} else {
438+
} else if target_os == "windows" {
438439
// FIXME: we have to finish implementing the Windows equivalent of this.
439440
this.eval_windows(match e.kind() {
440441
NotFound => "ERROR_FILE_NOT_FOUND",
441-
_ => throw_unsup_format!(
442-
"setting the last OS error from an io::Error is yet unsupported for {}.",
443-
target.target_os
444-
)
442+
_ => throw_unsup_format!("io error {} cannot be transformed into a raw os error", e)
445443
})?
444+
} else {
445+
throw_unsup_format!("setting the last OS error from an io::Error is unsupported for {}.", target_os)
446446
};
447447
this.set_last_error(last_error)
448448
}

src/shims/env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
357357
fn SetCurrentDirectoryW (
358358
&mut self,
359359
path_op: OpTy<'tcx, Tag> // LPCTSTR
360-
) -> InterpResult<'tcx, i32> { // Returns BOOL(i32 in Windows)
360+
) -> InterpResult<'tcx, i32> { // Returns BOOL (i32 in Windows)
361361
let this = self.eval_context_mut();
362362
this.assert_target_os("windows", "SetCurrentDirectoryW");
363363

src/shims/os_str.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
179179
let this = self.eval_context_ref();
180180
let os_str: &'a OsStr = this.read_os_str_from_c_str(scalar)?;
181181

182-
Ok(match convert_path_separator(os_str, &this.tcx.sess.target.target.target_os) {
182+
Ok(match convert_path_separator(os_str, &this.tcx.sess.target.target.target_os, false) {
183183
Cow::Borrowed(x) => Cow::Borrowed(Path::new(x)),
184184
Cow::Owned(y) => Cow::Owned(PathBuf::from(y)),
185185
})
@@ -190,7 +190,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
190190
let this = self.eval_context_ref();
191191
let os_str: OsString = this.read_os_str_from_wide_str(scalar)?;
192192

193-
Ok(PathBuf::from(&convert_path_separator(&os_str, &this.tcx.sess.target.target.target_os)))
193+
Ok(PathBuf::from(&convert_path_separator(&os_str, &this.tcx.sess.target.target.target_os, false)))
194194
}
195195

196196
/// Write a Path to the machine memory (as a null-terminated sequence of bytes),
@@ -202,7 +202,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
202202
size: u64,
203203
) -> InterpResult<'tcx, (bool, u64)> {
204204
let this = self.eval_context_mut();
205-
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os);
205+
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, true);
206206
this.write_os_str_to_c_str(&os_str, scalar, size)
207207
}
208208

@@ -215,35 +215,40 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
215215
size: u64,
216216
) -> InterpResult<'tcx, (bool, u64)> {
217217
let this = self.eval_context_mut();
218-
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os);
218+
let os_str = convert_path_separator(path.as_os_str(), &this.tcx.sess.target.target.target_os, true);
219219
this.write_os_str_to_wide_str(&os_str, scalar, size)
220220
}
221221
}
222222

223223
/// Perform path separator conversion if needed.
224+
/// if direction == true, Convert from 'host' to 'target'.
225+
/// if direction == false, Convert from 'target' to 'host'.
224226
fn convert_path_separator<'a>(
225227
os_str: &'a OsStr,
226228
target_os: &str,
229+
direction: bool,
227230
) -> Cow<'a, OsStr> {
228231
#[cfg(windows)]
229232
return if target_os == "windows" {
230233
// Windows-on-Windows, all fine.
231234
Cow::Borrowed(os_str)
232235
} else {
233-
// Unix target, Windows host. Need to convert host '\\' to target '/'.
236+
// Unix target, Windows host.
237+
let (from, to) = if direction { ('\\', '/') } else { ('/', '\\') };
234238
let converted = os_str
235239
.encode_wide()
236-
.map(|wchar| if wchar == '\\' as u16 { '/' as u16 } else { wchar })
240+
.map(|wchar| if wchar == from as u16 { to as u16 } else { wchar })
237241
.collect::<Vec<_>>();
238242
Cow::Owned(OsString::from_wide(&converted))
239243
};
240244
#[cfg(unix)]
241245
return if target_os == "windows" {
242-
// Windows target, Unix host. Need to convert host '/' to target '\'.
246+
// Windows target, Unix host.
247+
let (from, to) = if direction { ('/', '\\') } else { ('\\', '/') };
243248
let converted = os_str
244249
.as_bytes()
245250
.iter()
246-
.map(|&wchar| if wchar == '/' as u8 { '\\' as u8 } else { wchar })
251+
.map(|&wchar| if wchar == from as u8 { to as u8 } else { wchar })
247252
.collect::<Vec<_>>();
248253
Cow::Owned(OsString::from_vec(converted))
249254
} else {

0 commit comments

Comments
 (0)