Skip to content

Commit 01f060b

Browse files
committed
avoid allocation in read_os_string_from_c_string
1 parent 808ac8f commit 01f060b

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/helpers.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::{mem, iter};
2-
use std::ffi::{OsStr, OsString};
2+
use std::ffi::OsStr;
33

44
use syntax::source_map::DUMMY_SP;
55
use rustc::hir::def_id::{DefId, CRATE_DEF_INDEX};
@@ -453,9 +453,12 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
453453

454454
/// Helper function to read an OsString from a null-terminated sequence of bytes, which is what
455455
/// the Unix APIs usually handle.
456-
fn read_os_string_from_c_string(&mut self, scalar: Scalar<Tag>) -> InterpResult<'tcx, OsString> {
457-
let bytes = self.eval_context_mut().memory.read_c_str(scalar)?;
458-
Ok(bytes_to_os_str(bytes)?.into())
456+
fn read_os_string_from_c_string<'a>(&'a self, scalar: Scalar<Tag>) -> InterpResult<'tcx, &'a OsStr>
457+
where 'tcx: 'a, 'mir: 'a
458+
{
459+
let this = self.eval_context_ref();
460+
let bytes = this.memory.read_c_str(scalar)?;
461+
bytes_to_os_str(bytes)
459462
}
460463

461464
/// 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]>
501504
}
502505

503506
#[cfg(not(target_os = "unix"))]
504-
fn bytes_to_os_str<'tcx, 'a>(bytes: &'a[u8]) -> InterpResult<'tcx, &'a OsStr> {
507+
fn bytes_to_os_str<'tcx, 'a>(bytes: &'a [u8]) -> InterpResult<'tcx, &'a OsStr> {
505508
let s = std::str::from_utf8(bytes)
506509
.map_err(|_| err_unsup_format!("{:?} is not a valid utf-8 string", bytes))?;
507510
Ok(&OsStr::new(s))

src/machine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ pub type MiriEvalContext<'mir, 'tcx> = InterpCx<'mir, 'tcx, Evaluator<'tcx>>;
140140

141141
/// A little trait that's useful to be inherited by extension traits.
142142
pub trait MiriEvalContextExt<'mir, 'tcx> {
143-
fn eval_context_ref(&self) -> &MiriEvalContext<'mir, 'tcx>;
144-
fn eval_context_mut(&mut self) -> &mut MiriEvalContext<'mir, 'tcx>;
143+
fn eval_context_ref<'a>(&'a self) -> &'a MiriEvalContext<'mir, 'tcx>;
144+
fn eval_context_mut<'a>(&'a mut self) -> &'a mut MiriEvalContext<'mir, 'tcx>;
145145
}
146146
impl<'mir, 'tcx> MiriEvalContextExt<'mir, 'tcx> for MiriEvalContext<'mir, 'tcx> {
147147
#[inline(always)]

0 commit comments

Comments
 (0)