From 01f060b6da675d800d9874e5256f4a069e58d442 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Wed, 4 Dec 2019 10:16:08 +0100 Subject: [PATCH] avoid allocation in read_os_string_from_c_string --- src/helpers.rs | 13 ++++++++----- src/machine.rs | 4 ++-- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/helpers.rs b/src/helpers.rs index e7da88f2bbf..9f75c9b7fbe 100644 --- a/src/helpers.rs +++ b/src/helpers.rs @@ -1,5 +1,5 @@ use std::{mem, iter}; -use std::ffi::{OsStr, OsString}; +use std::ffi::OsStr; use syntax::source_map::DUMMY_SP; use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX}; @@ -453,9 +453,12 @@ fn try_unwrap_io_result>( /// Helper function to read an OsString from a null-terminated sequence of bytes, which is what /// the Unix APIs usually handle. - fn read_os_string_from_c_string(&mut self, scalar: Scalar) -> InterpResult<'tcx, OsString> { - let bytes = self.eval_context_mut().memory.read_c_str(scalar)?; - Ok(bytes_to_os_str(bytes)?.into()) + fn read_os_string_from_c_string<'a>(&'a self, scalar: Scalar) -> InterpResult<'tcx, &'a OsStr> + where 'tcx: 'a, 'mir: 'a + { + let this = self.eval_context_ref(); + let bytes = this.memory.read_c_str(scalar)?; + bytes_to_os_str(bytes) } /// Helper function to write an OsStr as a null-terminated sequence of bytes, which is what @@ -501,7 +504,7 @@ fn os_str_to_bytes<'tcx, 'a>(os_str: &'a OsStr) -> InterpResult<'tcx, &'a [u8]> } #[cfg(not(target_os = "unix"))] -fn bytes_to_os_str<'tcx, 'a>(bytes: &'a[u8]) -> InterpResult<'tcx, &'a OsStr> { +fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> { let s = std::str::from_utf8(bytes) .map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes))?; Ok(&OsStr::new(s)) diff --git a/src/machine.rs b/src/machine.rs index edabc3bccbc..55ac7f780fa 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -140,8 +140,8 @@ pub(crate) fn new(communicate: bool) -> Self { /// A little trait that's useful to be inherited by extension traits. pub trait MiriEvalContextExt<'mir, 'tcx> { - fn eval_context_ref(&self) -> &MiriEvalContext<'mir, 'tcx>; - fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'mir, 'tcx>; + fn eval_context_ref<'a>(&'a self) -> &'a MiriEvalContext<'mir, 'tcx>; + fn eval_context_mut<'a>(&'a mut self) -> &'a mut MiriEvalContext<'mir, 'tcx>; } impl<'mir, 'tcx> MiriEvalContextExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> { #[inline(always)]