From ba64f485c881052c980648bed0b4ce29fb6dc19c Mon Sep 17 00:00:00 2001 From: Smit Soni Date: Wed, 9 Jun 2021 06:28:35 -0700 Subject: [PATCH] Fix parameter of io error helper function `set_last_error_from_io_error` works with only the error kind, and discards the payload. Fix its signature to make it explicit. --- src/helpers.rs | 19 +++++++++++-------- src/shims/env.rs | 22 +++++++++------------- src/shims/posix/fs.rs | 10 +++++----- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index e9bedd1a118..8bfc65111d4 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -460,15 +460,15 @@ fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar> { this.read_scalar(&errno_place.into())?.check_init() } - /// Sets the last OS error using a `std::io::Error`. This function tries to produce the most + /// Sets the last OS error using a `std::io::ErrorKind`. This function tries to produce the most /// similar OS error from the `std::io::ErrorKind` and sets it as the last OS error. - fn set_last_error_from_io_error(&mut self, e: std::io::Error) -> InterpResult<'tcx> { + fn set_last_error_from_io_error(&mut self, err_kind: std::io::ErrorKind) -> InterpResult<'tcx> { use std::io::ErrorKind::*; let this = self.eval_context_mut(); let target = &this.tcx.sess.target; let target_os = &target.os; let last_error = if target.families.contains(&"unix".to_owned()) { - this.eval_libc(match e.kind() { + this.eval_libc(match err_kind { ConnectionRefused => "ECONNREFUSED", ConnectionReset => "ECONNRESET", PermissionDenied => "EPERM", @@ -484,18 +484,21 @@ fn set_last_error_from_io_error(&mut self, e: std::io::Error) -> InterpResult<'t AlreadyExists => "EEXIST", WouldBlock => "EWOULDBLOCK", _ => { - throw_unsup_format!("io error {} cannot be transformed into a raw os error", e) + throw_unsup_format!( + "io error {:?} cannot be transformed into a raw os error", + err_kind + ) } })? } else if target.families.contains(&"windows".to_owned()) { // FIXME: we have to finish implementing the Windows equivalent of this. this.eval_windows( "c", - match e.kind() { + match err_kind { NotFound => "ERROR_FILE_NOT_FOUND", _ => throw_unsup_format!( - "io error {} cannot be transformed into a raw os error", - e + "io error {:?} cannot be transformed into a raw os error", + err_kind ), }, )? @@ -521,7 +524,7 @@ fn try_unwrap_io_result>( match result { Ok(ok) => Ok(ok), Err(e) => { - self.eval_context_mut().set_last_error_from_io_error(e)?; + self.eval_context_mut().set_last_error_from_io_error(e.kind())?; Ok((-1).into()) } } diff --git a/src/shims/env.rs b/src/shims/env.rs index 9a68cf7bd53..2ce0fbfdc94 100644 --- a/src/shims/env.rs +++ b/src/shims/env.rs @@ -1,7 +1,7 @@ use std::convert::TryFrom; use std::env; use std::ffi::{OsStr, OsString}; -use std::io::{Error, ErrorKind}; +use std::io::ErrorKind; use rustc_data_structures::fx::FxHashMap; use rustc_mir::interpret::Pointer; @@ -324,8 +324,7 @@ fn getcwd( if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op { this.reject_in_isolation("getcwd", reject_with)?; - let err = Error::new(ErrorKind::NotFound, "rejected due to isolation"); - this.set_last_error_from_io_error(err)?; + this.set_last_error_from_io_error(ErrorKind::NotFound)?; return Ok(Scalar::null_ptr(&*this.tcx)); } @@ -340,7 +339,7 @@ fn getcwd( let erange = this.eval_libc("ERANGE")?; this.set_last_error(erange)?; } - Err(e) => this.set_last_error_from_io_error(e)?, + Err(e) => this.set_last_error_from_io_error(e.kind())?, } Ok(Scalar::null_ptr(&*this.tcx)) @@ -357,8 +356,7 @@ fn GetCurrentDirectoryW( if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op { this.reject_in_isolation("GetCurrentDirectoryW", reject_with)?; - let err = Error::new(ErrorKind::NotFound, "rejected due to isolation"); - this.set_last_error_from_io_error(err)?; + this.set_last_error_from_io_error(ErrorKind::NotFound)?; return Ok(0); } @@ -369,7 +367,7 @@ fn GetCurrentDirectoryW( match env::current_dir() { Ok(cwd) => return Ok(windows_check_buffer_size(this.write_path_to_wide_str(&cwd, buf, size)?)), - Err(e) => this.set_last_error_from_io_error(e)?, + Err(e) => this.set_last_error_from_io_error(e.kind())?, } Ok(0) } @@ -384,8 +382,7 @@ fn chdir(&mut self, path_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> { if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op { this.reject_in_isolation("chdir", reject_with)?; - let err = Error::new(ErrorKind::NotFound, "rejected due to isolation"); - this.set_last_error_from_io_error(err)?; + this.set_last_error_from_io_error(ErrorKind::NotFound)?; return Ok(-1); } @@ -395,7 +392,7 @@ fn chdir(&mut self, path_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, i32> { match env::set_current_dir(path) { Ok(()) => Ok(0), Err(e) => { - this.set_last_error_from_io_error(e)?; + this.set_last_error_from_io_error(e.kind())?; Ok(-1) } } @@ -413,8 +410,7 @@ fn SetCurrentDirectoryW( if let IsolatedOp::Reject(reject_with) = this.machine.isolated_op { this.reject_in_isolation("SetCurrentDirectoryW", reject_with)?; - let err = Error::new(ErrorKind::NotFound, "rejected due to isolation"); - this.set_last_error_from_io_error(err)?; + this.set_last_error_from_io_error(ErrorKind::NotFound)?; return Ok(0); } @@ -424,7 +420,7 @@ fn SetCurrentDirectoryW( match env::set_current_dir(path) { Ok(()) => Ok(1), Err(e) => { - this.set_last_error_from_io_error(e)?; + this.set_last_error_from_io_error(e.kind())?; Ok(0) } } diff --git a/src/shims/posix/fs.rs b/src/shims/posix/fs.rs index ca7a91a0f94..fbef9f30407 100644 --- a/src/shims/posix/fs.rs +++ b/src/shims/posix/fs.rs @@ -634,7 +634,7 @@ fn fcntl(&mut self, args: &[OpTy<'tcx, Tag>]) -> InterpResult<'tcx, i32> { match dup_result { Ok(dup_fd) => Ok(fh.insert_fd_with_min_fd(dup_fd, start)), Err(e) => { - this.set_last_error_from_io_error(e)?; + this.set_last_error_from_io_error(e.kind())?; Ok(-1) } } @@ -707,7 +707,7 @@ fn read(&mut self, fd: i32, buf: Scalar, count: u64) -> InterpResult<'tcx, Ok(read_bytes) } Err(e) => { - this.set_last_error_from_io_error(e)?; + this.set_last_error_from_io_error(e.kind())?; Ok(-1) } } @@ -1118,7 +1118,7 @@ fn opendir(&mut self, name_op: &OpTy<'tcx, Tag>) -> InterpResult<'tcx, Scalar { - this.set_last_error_from_io_error(e)?; + this.set_last_error_from_io_error(e.kind())?; Ok(Scalar::null_ptr(this)) } } @@ -1462,7 +1462,7 @@ fn readlink( Ok(path_bytes.len().try_into().unwrap()) } Err(e) => { - this.set_last_error_from_io_error(e)?; + this.set_last_error_from_io_error(e.kind())?; Ok(-1) } } @@ -1526,7 +1526,7 @@ fn from_meta<'tcx, 'mir>( let metadata = match metadata { Ok(metadata) => metadata, Err(e) => { - ecx.set_last_error_from_io_error(e)?; + ecx.set_last_error_from_io_error(e.kind())?; return Ok(None); } };