Skip to content

Commit 61da8b8

Browse files
committed
Add OsString from/to bytes helper functions
1 parent ad6af7a commit 61da8b8

File tree

4 files changed

+30
-25
lines changed

4 files changed

+30
-25
lines changed

src/helpers.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::mem;
2+
use std::ffi::OsString;
23

34
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
45
use rustc::mir;
@@ -345,3 +346,25 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
345346
Ok(())
346347
}
347348
}
349+
350+
351+
pub fn bytes_to_os_string<'tcx>(bytes: Vec<u8>) -> InterpResult<'tcx, OsString> {
352+
if cfg!(unix) {
353+
Ok(std::os::unix::ffi::OsStringExt::from_vec(bytes))
354+
} else {
355+
std::str::from_utf8(&bytes)
356+
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes).into())
357+
.map(OsString::from)
358+
}
359+
}
360+
361+
pub fn os_string_to_bytes<'tcx>(os_string: OsString) -> InterpResult<'tcx, Vec<u8>> {
362+
if cfg!(unix) {
363+
Ok(std::os::unix::ffi::OsStringExt::into_vec(os_string))
364+
} else {
365+
os_string
366+
.into_string()
367+
.map_err(|os_string| err_unsup_format!("{:?} is not a valid utf-8 string", os_string).into())
368+
.map(|s| s.into_bytes())
369+
}
370+
}

src/shims/env.rs

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::collections::HashMap;
22
use std::env;
3-
use std::path::Path;
43

54
use crate::stacked_borrows::Tag;
65
use crate::*;
6+
77
use rustc::ty::layout::Size;
88
use rustc_mir::interpret::{Memory, Pointer};
99

@@ -128,7 +128,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
128128
match env::current_dir() {
129129
Ok(cwd) => {
130130
// It is not clear what happens with non-utf8 paths here
131-
let mut bytes = cwd.display().to_string().into_bytes();
131+
let mut bytes = helpers::os_string_to_bytes(cwd.into())?;
132132
// If `size` is smaller or equal than the `bytes.len()`, writing `bytes` plus the
133133
// required null terminator to memory using the `buf` pointer would cause an
134134
// overflow. The desired behavior in this case is to return null.
@@ -156,16 +156,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
156156

157157
this.check_no_isolation("chdir")?;
158158

159-
let path_bytes = this
160-
.memory
161-
.read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
162-
163-
let path = Path::new(
164-
std::str::from_utf8(path_bytes)
165-
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?,
166-
);
159+
let bytes = this.memory.read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
160+
let path = helpers::bytes_to_os_string(bytes.to_vec());
167161

168-
match env::set_current_dir(path) {
162+
match env::set_current_dir(path?) {
169163
Ok(()) => Ok(0),
170164
Err(e) => {
171165
this.consume_io_error(e)?;

src/shims/fs.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
9494
throw_unsup_format!("unsupported flags {:#x}", flag & !mirror);
9595
}
9696

97-
let path_bytes = this
97+
let bytes = this
9898
.memory
9999
.read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
100-
let path = std::str::from_utf8(path_bytes)
101-
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?;
100+
let path: std::path::PathBuf = helpers::bytes_to_os_string(bytes.to_vec())?.into();
102101

103102
let fd = options.open(path).map(|file| {
104103
let mut fh = &mut this.machine.file_handler;

tests/compile-fail/chdir_invalid_path.rs

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)