Skip to content

Commit ace3416

Browse files
committed
Write name and value for each env var
1 parent 631d5fa commit ace3416

File tree

1 file changed

+9
-7
lines changed

1 file changed

+9
-7
lines changed

src/shims/env.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,19 @@ impl EnvVars {
1616
) {
1717
if ecx.machine.communicate {
1818
for (name, value) in std::env::vars() {
19-
let value = alloc_env_value(value.as_bytes(), ecx.memory_mut());
20-
ecx.machine.env_vars.map.insert(name.into_bytes(), value);
19+
let var_ptr = alloc_env_var(name.as_bytes(), value.as_bytes(), ecx.memory_mut());
20+
ecx.machine.env_vars.map.insert(name.into_bytes(), var_ptr);
2121
}
2222
}
2323
}
2424
}
2525

26-
fn alloc_env_value<'mir, 'tcx>(
27-
bytes: &[u8],
26+
fn alloc_env_var<'mir, 'tcx>(
27+
name: &[u8],
28+
value: &[u8],
2829
memory: &mut Memory<'mir, 'tcx, Evaluator<'tcx>>,
2930
) -> Pointer<Tag> {
31+
let bytes = [name, b"=", value].concat();
3032
let tcx = {memory.tcx.tcx};
3133
let length = bytes.len() as u64;
3234
// `+1` for the null terminator.
@@ -57,7 +59,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
5759
let name_ptr = this.read_scalar(name_op)?.not_undef()?;
5860
let name = this.memory().read_c_str(name_ptr)?;
5961
Ok(match this.machine.env_vars.map.get(name) {
60-
Some(&var) => Scalar::Ptr(var),
62+
Some(var_ptr) => Scalar::Ptr(var_ptr.offset(Size::from_bytes(name.len() as u64 + 1), this)?),
6163
None => Scalar::ptr_null(&*this.tcx),
6264
})
6365
}
@@ -80,8 +82,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8082
}
8183
}
8284
if let Some((name, value)) = new {
83-
let value_copy = alloc_env_value(&value, this.memory_mut());
84-
if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), value_copy) {
85+
let var_ptr = alloc_env_var(&name, &value, this.memory_mut());
86+
if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), var_ptr) {
8587
this.memory_mut().deallocate(var, None, MiriMemoryKind::Env.into())?;
8688
}
8789
Ok(0)

0 commit comments

Comments
 (0)