Skip to content

Commit ec878af

Browse files
committed
Refactor UnixEnvVars::get so that it can be reused by getenv
1 parent 69ed236 commit ec878af

File tree

4 files changed

+17
-20
lines changed

4 files changed

+17
-20
lines changed

src/shims/env.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,19 @@ impl<'mir, 'tcx: 'mir> EvalContextExt<'mir, 'tcx> for crate::MiriInterpCx<'mir,
102102
pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
103103
/// Try to get an environment variable from the interpreted program's environment. This is
104104
/// useful for implementing shims which are documented to read from the environment.
105-
fn get_var(&mut self, name: &OsStr) -> InterpResult<'tcx, Option<OsString>> {
105+
fn get_env_var(&mut self, name: &OsStr) -> InterpResult<'tcx, Option<OsString>> {
106106
let this = self.eval_context_ref();
107107
match &this.machine.env_vars {
108108
EnvVars::Uninit => return Ok(None),
109-
EnvVars::Unix(vars) => vars.get(this, name),
109+
EnvVars::Unix(vars) => {
110+
let var_ptr = vars.get(this, name)?;
111+
if let Some(ptr) = var_ptr {
112+
let var = this.read_os_str_from_c_str(ptr)?;
113+
Ok(Some(var.to_owned()))
114+
} else {
115+
Ok(None)
116+
}
117+
}
110118
EnvVars::Windows(vars) => vars.get(name),
111119
}
112120
}

src/shims/time.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
140140
DateTime::from_timestamp(sec_since_epoch, 0).expect("Invalid timestamp");
141141

142142
// Figure out what time zone is in use
143-
let tz = this.get_var(OsStr::new("TZ"))?.unwrap_or_else(|| OsString::from("UTC"));
143+
let tz = this.get_env_var(OsStr::new("TZ"))?.unwrap_or_else(|| OsString::from("UTC"));
144144
let tz = match tz.into_string() {
145145
Ok(tz) => Tz::from_str(&tz).unwrap_or(Tz::UTC),
146146
_ => Tz::UTC,

src/shims/unix/env.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ impl<'tcx> UnixEnvVars<'tcx> {
7171
self.environ.ptr()
7272
}
7373

74-
/// Implementation detail for [`InterpCx::get_var`]. This basically does `getenv`, complete
74+
/// Implementation detail for [`InterpCx::get_env_var`]. This basically does `getenv`, complete
7575
/// with the reads of the environment, but returns an [`OsString`] instead of a pointer.
7676
pub(crate) fn get<'mir>(
7777
&self,
7878
ecx: &InterpCx<'mir, 'tcx, MiriMachine<'mir, 'tcx>>,
7979
name: &OsStr,
80-
) -> InterpResult<'tcx, Option<OsString>> {
80+
) -> InterpResult<'tcx, Option<Pointer<Option<Provenance>>>> {
8181
// We don't care about the value as we have the `map` to keep track of everything,
8282
// but we do want to do this read so it shows up as a data race.
8383
let _vars_ptr = ecx.read_pointer(&self.environ)?;
@@ -89,7 +89,7 @@ impl<'tcx> UnixEnvVars<'tcx> {
8989
Size::from_bytes(u64::try_from(name.len()).unwrap().checked_add(1).unwrap()),
9090
ecx,
9191
)?;
92-
ecx.read_os_str_from_c_str(var_ptr).map(|s| Some(s.to_owned()))
92+
Ok(Some(var_ptr))
9393
}
9494
}
9595

@@ -137,19 +137,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
137137
let name_ptr = this.read_pointer(name_op)?;
138138
let name = this.read_os_str_from_c_str(name_ptr)?;
139139

140-
// We don't care about the value as we have the `map` to keep track of everything,
141-
// but we do want to do this read so it shows up as a data race.
142-
let _vars_ptr = this.read_pointer(&this.machine.env_vars.unix().environ)?;
143-
Ok(match this.machine.env_vars.unix().map.get(name) {
144-
Some(var_ptr) => {
145-
// The offset is used to strip the "{name}=" part of the string.
146-
var_ptr.offset(
147-
Size::from_bytes(u64::try_from(name.len()).unwrap().checked_add(1).unwrap()),
148-
this,
149-
)?
150-
}
151-
None => Pointer::null(),
152-
})
140+
let var_ptr = this.machine.env_vars.unix().get(this, name)?;
141+
Ok(var_ptr.unwrap_or_else(Pointer::null))
153142
}
154143

155144
fn setenv(

src/shims/windows/env.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl WindowsEnvVars {
2727
Ok(Self { map: env_vars })
2828
}
2929

30-
/// Implementation detail for [`InterpCx::get_var`].
30+
/// Implementation detail for [`InterpCx::get_env_var`].
3131
pub(crate) fn get<'tcx>(&self, name: &OsStr) -> InterpResult<'tcx, Option<OsString>> {
3232
Ok(self.map.get(name).cloned())
3333
}

0 commit comments

Comments
 (0)