Skip to content

Commit bc2e718

Browse files
committed
Add OsString from/to bytes helper functions
1 parent 089c7e8 commit bc2e718

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;
@@ -349,3 +350,25 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
349350
Ok(())
350351
}
351352
}
353+
354+
355+
pub fn bytes_to_os_string<'tcx>(bytes: Vec<u8>) -> InterpResult<'tcx, OsString> {
356+
if cfg!(unix) {
357+
Ok(std::os::unix::ffi::OsStringExt::from_vec(bytes))
358+
} else {
359+
std::str::from_utf8(&bytes)
360+
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes).into())
361+
.map(OsString::from)
362+
}
363+
}
364+
365+
pub fn os_string_to_bytes<'tcx>(os_string: OsString) -> InterpResult<'tcx, Vec<u8>> {
366+
if cfg!(unix) {
367+
Ok(std::os::unix::ffi::OsStringExt::into_vec(os_string))
368+
} else {
369+
os_string
370+
.into_string()
371+
.map_err(|os_string| err_unsup_format!("{:?} is not a valid utf-8 string", os_string).into())
372+
.map(|s| s.into_bytes())
373+
}
374+
}

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

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

159159
this.check_no_isolation("chdir")?;
160160

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

170-
match env::set_current_dir(path) {
164+
match env::set_current_dir(path?) {
171165
Ok(()) => Ok(0),
172166
Err(e) => {
173167
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)