From: Christian Poveda Date: Tue, 22 Oct 2019 21:57:07 +0000 (-0500) Subject: Make size error distinguishable from other errors X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=fb4cb5bf4af633ca057a84b995bab3b08333df41;p=rust.git Make size error distinguishable from other errors --- diff --git a/src/helpers.rs b/src/helpers.rs index b31e0a2c2eb..79efd95eb6d 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -412,16 +412,25 @@ fn read_os_string_from_c_string(&mut self, scalar: Scalar) -> InterpResult< } /// Helper function to write an OsStr as a null-terminated sequence of bytes, which is what - /// the Unix APIs usually handle. - fn write_os_str_to_c_string(&mut self, os_str: &OsStr, scalar: Scalar, size: u64) -> InterpResult<'tcx> { + /// the Unix APIs usually handle. This function returns `Ok(false)` without trying to write if + /// `size` is not large enough to fit the contents of `os_string` plus a null terminator. It + /// returns `Ok(true)` if the writing process was successful. Otherwise it returns an + /// `InterpError`. + fn write_os_str_to_c_string( + &mut self, + os_str: &OsStr, + scalar: Scalar, + size: u64 + ) -> InterpResult<'tcx, bool> { let bytes = os_str_to_bytes(os_str)?; // If `size` is smaller or equal than `bytes.len()`, writing `bytes` plus the required null // terminator to memory using the `ptr` pointer would cause an overflow. if size <= bytes.len() as u64 { - throw_unsup_format!("OsString of length {} is too large for destination buffer of size {}", bytes.len(), size) + return Ok(false); } // FIXME: We should use `Iterator::chain` instead when rust-lang/rust#65704 lands. - self.eval_context_mut().memory.write_bytes(scalar, [bytes, &[0]].concat()) + self.eval_context_mut().memory.write_bytes(scalar, [bytes, &[0]].concat())?; + Ok(true) } } diff --git a/src/shims/env.rs b/src/shims/env.rs index f88a5bbac72..2dc47d74ffb 100644 --- a/src/shims/env.rs +++ b/src/shims/env.rs @@ -128,7 +128,7 @@ fn getcwd( // If we cannot get the current directory, we return null match env::current_dir() { Ok(cwd) => { - if this.write_os_str_to_c_string(&OsString::from(cwd), buf, size).is_ok() { + if this.write_os_str_to_c_string(&OsString::from(cwd), buf, size)? { return Ok(buf); } let erange = this.eval_libc("ERANGE")?;