]> git.lizzy.rs Git - rust.git/commitdiff
Add OsString from/to bytes helper functions
authorChristian Poveda <christianpoveda@protonmail.com>
Sun, 13 Oct 2019 20:26:03 +0000 (15:26 -0500)
committerChristian Poveda <christianpoveda@protonmail.com>
Fri, 18 Oct 2019 14:47:19 +0000 (09:47 -0500)
src/helpers.rs
src/shims/env.rs
src/shims/fs.rs
tests/compile-fail/chdir_invalid_path.rs [deleted file]

index c09d5c823e1bb214f306223ea9dfb7978a65c02a..c2e4131a76d108d8a6af54781c5d5d86015fc624 100644 (file)
@@ -1,4 +1,5 @@
 use std::mem;
+use std::ffi::OsString;
 
 use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
 use rustc::mir;
@@ -345,3 +346,25 @@ fn check_no_isolation(&mut self, name: &str) -> InterpResult<'tcx> {
         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())
+        }
+    }
index 6078ca26e269a17e11ec2432d43094991fc13330..1fe08aeffb7b0ac69781c8d7dd01441f160ad12b 100644 (file)
@@ -1,9 +1,9 @@
 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};
 
@@ -128,7 +128,7 @@ fn getcwd(
         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.
@@ -156,16 +156,10 @@ fn chdir(&mut self, path_op: OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> {
 
         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)?;
index 891474bc3bdb6bd90e8214daa9596896530a716d..cc776295bd49105525a6f1f696eeb41a6885c39c 100644 (file)
@@ -94,11 +94,10 @@ fn open(
             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;
diff --git a/tests/compile-fail/chdir_invalid_path.rs b/tests/compile-fail/chdir_invalid_path.rs
deleted file mode 100644 (file)
index 22b0d72..0000000
+++ /dev/null
@@ -1,11 +0,0 @@
-// 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
-}