Skip to content

Commit ea9046b

Browse files
committed
fix env update, and expand test
1 parent 18f0339 commit ea9046b

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

src/shims/env.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
8181
if let Some((name, value)) = new {
8282
let var_ptr = alloc_env_var_as_c_str(&name, &value, &mut this);
8383
if let Some(var) = this.machine.env_vars.map.insert(name.to_owned(), var_ptr) {
84-
this.update_environ()?;
8584
this.memory
8685
.deallocate(var, None, MiriMemoryKind::Machine.into())?;
8786
}
87+
this.update_environ()?;
8888
Ok(0)
8989
} else {
9090
Ok(-1)
@@ -100,14 +100,14 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriEvalContextExt<'mir, 'tcx
100100
let name = this.read_os_str_from_c_str(name_ptr)?.to_owned();
101101
if !name.is_empty() && !name.to_string_lossy().contains('=') {
102102
success = Some(this.machine.env_vars.map.remove(&name));
103-
this.update_environ()?;
104103
}
105104
}
106105
if let Some(old) = success {
107106
if let Some(var) = old {
108107
this.memory
109108
.deallocate(var, None, MiriMemoryKind::Machine.into())?;
110109
}
110+
this.update_environ()?;
111111
Ok(0)
112112
} else {
113113
Ok(-1)

tests/run-pass/env.rs

+19-4
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,26 @@
33
use std::env;
44

55
fn main() {
6+
// Test that miri environment is isolated when communication is disabled.
7+
// (`MIRI_ENV_VAR_TEST` is set by the test harness.)
8+
assert_eq!(env::var("MIRI_ENV_VAR_TEST"), Err(env::VarError::NotPresent));
9+
10+
// Test base state.
11+
println!("{:#?}", env::vars().collect::<Vec<_>>());
612
assert_eq!(env::var("MIRI_TEST"), Err(env::VarError::NotPresent));
13+
14+
// Set the variable.
715
env::set_var("MIRI_TEST", "the answer");
816
assert_eq!(env::var("MIRI_TEST"), Ok("the answer".to_owned()));
9-
// Test that miri environment is isolated when communication is disabled.
10-
assert!(env::var("MIRI_ENV_VAR_TEST").is_err());
11-
// Test that the new variable is in the `std::env::Vars` iterator
12-
assert!(env::vars().any(|(name, value)| name == "MIRI_TEST"))
17+
println!("{:#?}", env::vars().collect::<Vec<_>>());
18+
19+
// Change the variable.
20+
env::set_var("MIRI_TEST", "42");
21+
assert_eq!(env::var("MIRI_TEST"), Ok("42".to_owned()));
22+
println!("{:#?}", env::vars().collect::<Vec<_>>());
23+
24+
// Remove the variable.
25+
env::remove_var("MIRI_TEST");
26+
assert_eq!(env::var("MIRI_TEST"), Err(env::VarError::NotPresent));
27+
println!("{:#?}", env::vars().collect::<Vec<_>>());
1328
}

tests/run-pass/env.stdout

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[]
2+
[
3+
(
4+
"MIRI_TEST",
5+
"the answer",
6+
),
7+
]
8+
[
9+
(
10+
"MIRI_TEST",
11+
"42",
12+
),
13+
]
14+
[]

0 commit comments

Comments
 (0)