use std::mem;
+use std::ffi::OsString;
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
use rustc::mir;
Ok(())
}
}
+
+
+pub fn bytes_to_os_string<'tcx>(bytes: Vec<u8>) -> InterpResult<'tcx, OsString> {
+ if cfg!(unix) {
+ Ok(std::os::unix::ffi::OsStringExt::from_vec(bytes))
+ } else {
+ std::str::from_utf8(&bytes)
+ .map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes).into())
+ .map(OsString::from)
+ }
+ }
+
+pub fn os_string_to_bytes<'tcx>(os_string: OsString) -> InterpResult<'tcx, Vec<u8>> {
+ if cfg!(unix) {
+ Ok(std::os::unix::ffi::OsStringExt::into_vec(os_string))
+ } else {
+ os_string
+ .into_string()
+ .map_err(|os_string| err_unsup_format!("{:?} is not a valid utf-8 string", os_string).into())
+ .map(|s| s.into_bytes())
+ }
+ }
use std::collections::HashMap;
use std::env;
-use std::path::Path;
use crate::stacked_borrows::Tag;
use crate::*;
+
use rustc::ty::layout::Size;
use rustc_mir::interpret::{Memory, Pointer};
match env::current_dir() {
Ok(cwd) => {
// It is not clear what happens with non-utf8 paths here
- let mut bytes = cwd.display().to_string().into_bytes();
+ let mut bytes = helpers::os_string_to_bytes(cwd.into())?;
// If `size` is smaller or equal than the `bytes.len()`, writing `bytes` plus the
// required null terminator to memory using the `buf` pointer would cause an
// overflow. The desired behavior in this case is to return null.
this.check_no_isolation("chdir")?;
- let path_bytes = this
- .memory
- .read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
-
- let path = Path::new(
- std::str::from_utf8(path_bytes)
- .map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?,
- );
+ let bytes = this.memory.read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
+ let path = helpers::bytes_to_os_string(bytes.to_vec());
- match env::set_current_dir(path) {
+ match env::set_current_dir(path?) {
Ok(()) => Ok(0),
Err(e) => {
this.consume_io_error(e)?;
throw_unsup_format!("unsupported flags {:#x}", flag & !mirror);
}
- let path_bytes = this
+ let bytes = this
.memory
.read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
- let path = std::str::from_utf8(path_bytes)
- .map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?;
+ let path: std::path::PathBuf = helpers::bytes_to_os_string(bytes.to_vec())?.into();
let fd = options.open(path).map(|file| {
let mut fh = &mut this.machine.file_handler;
+++ /dev/null
-// compile-flags: -Zmiri-disable-isolation
-
-extern {
- pub fn chdir(dir: *const u8) -> i32;
-}
-
-fn main() {
- let path = vec![0xc3u8, 0x28, 0];
- // test that `chdir` errors with invalid utf-8 path
- unsafe { chdir(path.as_ptr()) }; //~ ERROR is not a valid utf-8 string
-}