Skip to content

Commit

Permalink
don't leak UnixEnvVars impl details into get_env_var
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Apr 29, 2024
1 parent ec878af commit 92715f5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
10 changes: 1 addition & 9 deletions src/shims/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,15 +106,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let this = self.eval_context_ref();
match &this.machine.env_vars {
EnvVars::Uninit => return Ok(None),
EnvVars::Unix(vars) => {
let var_ptr = vars.get(this, name)?;
if let Some(ptr) = var_ptr {
let var = this.read_os_str_from_c_str(ptr)?;
Ok(Some(var.to_owned()))
} else {
Ok(None)
}
}
EnvVars::Unix(vars) => vars.get(this, name),
EnvVars::Windows(vars) => vars.get(name),
}
}
Expand Down
22 changes: 18 additions & 4 deletions src/shims/unix/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ impl<'tcx> UnixEnvVars<'tcx> {
self.environ.ptr()
}

/// Implementation detail for [`InterpCx::get_env_var`]. This basically does `getenv`, complete
/// with the reads of the environment, but returns an [`OsString`] instead of a pointer.
pub(crate) fn get<'mir>(
fn get_ptr<'mir>(
&self,
ecx: &InterpCx<'mir, 'tcx, MiriMachine<'mir, 'tcx>>,
name: &OsStr,
Expand All @@ -91,6 +89,22 @@ impl<'tcx> UnixEnvVars<'tcx> {
)?;
Ok(Some(var_ptr))
}

/// Implementation detail for [`InterpCx::get_env_var`]. This basically does `getenv`, complete
/// with the reads of the environment, but returns an [`OsString`] instead of a pointer.
pub(crate) fn get<'mir>(
&self,
ecx: &InterpCx<'mir, 'tcx, MiriMachine<'mir, 'tcx>>,
name: &OsStr,
) -> InterpResult<'tcx, Option<OsString>> {
let var_ptr = self.get_ptr(ecx, name)?;
if let Some(ptr) = var_ptr {
let var = ecx.read_os_str_from_c_str(ptr)?;
Ok(Some(var.to_owned()))
} else {
Ok(None)
}
}
}

fn alloc_env_var<'mir, 'tcx>(
Expand Down Expand Up @@ -137,7 +151,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
let name_ptr = this.read_pointer(name_op)?;
let name = this.read_os_str_from_c_str(name_ptr)?;

let var_ptr = this.machine.env_vars.unix().get(this, name)?;
let var_ptr = this.machine.env_vars.unix().get_ptr(this, name)?;
Ok(var_ptr.unwrap_or_else(Pointer::null))
}

Expand Down

0 comments on commit 92715f5

Please sign in to comment.