Skip to content

Commit 25ed2b6

Browse files
committed
Add OsString from/to bytes helper functions
1 parent c77f0ab commit 25ed2b6

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::ty::{self, layout::{self, Size, Align}};
45
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
@@ -305,3 +306,25 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
305306
self.eval_libc(name)?.to_i32()
306307
}
307308
}
309+
310+
311+
pub fn bytes_to_os_string<'tcx>(bytes: Vec<u8>) -> InterpResult<'tcx, OsString> {
312+
if cfg!(unix) {
313+
Ok(std::os::unix::ffi::OsStringExt::from_vec(bytes))
314+
} else {
315+
std::str::from_utf8(&bytes)
316+
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes).into())
317+
.map(OsString::from)
318+
}
319+
}
320+
321+
pub fn os_string_to_bytes<'tcx>(os_string: OsString) -> InterpResult<'tcx, Vec<u8>> {
322+
if cfg!(unix) {
323+
Ok(std::os::unix::ffi::OsStringExt::into_vec(os_string))
324+
} else {
325+
os_string
326+
.into_string()
327+
.map_err(|os_string| err_unsup_format!("{:?} is not a valid utf-8 string", os_string).into())
328+
.map(|s| s.into_bytes())
329+
}
330+
}

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

@@ -132,7 +132,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
132132
match env::current_dir() {
133133
Ok(cwd) => {
134134
// It is not clear what happens with non-utf8 paths here
135-
let mut bytes = cwd.display().to_string().into_bytes();
135+
let mut bytes = helpers::os_string_to_bytes(cwd.into())?;
136136
// If `size` is smaller or equal than the `bytes.len()`, writing `bytes` plus the
137137
// required null terminator to memory using the `buf` pointer would cause an
138138
// overflow. The desired behavior in this case is to return null.
@@ -162,16 +162,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
162162
throw_unsup_format!("`chdir` not available when isolation is enabled")
163163
}
164164

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

174-
match env::set_current_dir(path) {
168+
match env::set_current_dir(path?) {
175169
Ok(()) => Ok(0),
176170
Err(e) => {
177171
this.consume_io_error(e)?;

src/shims/fs.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
6767
options.create(true);
6868
}
6969

70-
let path_bytes = this
70+
let bytes = this
7171
.memory()
7272
.read_c_str(this.read_scalar(path_op)?.not_undef()?)?;
73-
let path = std::str::from_utf8(path_bytes)
74-
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", path_bytes))?;
73+
let path: std::path::PathBuf = helpers::bytes_to_os_string(bytes.to_vec())?.into();
7574

7675
let fd = options.open(path).map(|file| {
7776
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)