From 5c3c738c4b82a00471cffe67e44a22173404bd4f Mon Sep 17 00:00:00 2001 From: Christian Poveda Date: Thu, 17 Oct 2019 20:29:30 -0500 Subject: [PATCH] Make transformation to OS error explicit --- src/helpers.rs | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index 465fca554c1..7d68d05cdb7 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -361,14 +361,34 @@ fn get_last_error(&mut self) -> InterpResult<'tcx, Scalar> { } /// Sets the last error variable using a `std::io::Error`. It fails if the error cannot be - /// transformed to a raw os error succesfully + /// transformed to a raw os error succesfully. fn set_last_error_from_io_error(&mut self, e: std::io::Error) -> InterpResult<'tcx> { - self.eval_context_mut().set_last_error(Scalar::from_int( - e.raw_os_error().ok_or_else(|| { - err_unsup_format!("The {} error cannot be transformed into a raw os error", e) - })?, - Size::from_bits(32), - )) + use std::io::ErrorKind::*; + let this = self.eval_context_mut(); + let target = &this.tcx.tcx.sess.target.target; + let last_error = if target.options.target_family == Some("unix".to_owned()) { + this.eval_libc(match e.kind() { + ConnectionRefused => "ECONNREFUSED", + ConnectionReset => "ECONNRESET", + PermissionDenied => "EPERM", + BrokenPipe => "EPIPE", + NotConnected => "ENOTCONN", + ConnectionAborted => "ECONNABORTED", + AddrNotAvailable => "EADDRNOTAVAIL", + AddrInUse => "EADDRINUSE", + NotFound => "ENOENT", + Interrupted => "EINTR", + InvalidInput => "EINVAL", + TimedOut => "ETIMEDOUT", + AlreadyExists => "EEXIST", + WouldBlock => "EWOULDBLOCK", + _ => throw_unsup_format!("The {} error cannot be transformed into a raw os error", e) + })? + } else { + // FIXME: we have to implement the windows' equivalent of this. + throw_unsup_format!("Setting the last OS error from an io::Error is unsupported for {}.", target.target_os) + }; + this.set_last_error(last_error) } /// Helper function that consumes an `std::io::Result` and returns an -- 2.44.0