Skip to content

Commit 893f539

Browse files
author
Alex Gaynor
committed
Fixes rust-lang#55775 -- fixed regression in Command::exec's handling of PATH.
This restores the previous behavior where if env_clear() or env_remove("PATH") was used we fall back to a default PATH of "/bin:/usr/bin"
1 parent 653da4f commit 893f539

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

src/libstd/sys/unix/process/process_unix.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,15 @@ impl Command {
135135
Some(envp) => {
136136
match envp.get_items().iter().find(|var| var.as_bytes().starts_with(b"PATH=")) {
137137
Some(p) => &p.as_bytes()[5..],
138-
None => return None,
138+
// Chosen to match what glibc does if there's no PATH variable
139+
None => b"/bin:/usr/bin",
139140
}
140141
},
141142
// maybe_envp is None if the process isn't changing the parent's env at all.
142143
None => {
143144
match parent_path.as_ref() {
144145
Some(p) => p.as_bytes(),
145-
None => return None,
146+
None => b"/bin:/usr/bin",
146147
}
147148
},
148149
};

src/test/run-pass/command-exec.rs

+20
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,16 @@ fn main() {
5555
println!("passed");
5656
}
5757

58+
"exec-test6" => {
59+
let err = Command::new("echo").arg("passed").env_clear().exec();
60+
panic!("failed to spawn: {}", err);
61+
}
62+
63+
"exec-test7" => {
64+
let err = Command::new("echo").arg("passed").env_remove("PATH").exec();
65+
panic!("failed to spawn: {}", err);
66+
}
67+
5868
_ => panic!("unknown argument: {}", arg),
5969
}
6070
return
@@ -84,4 +94,14 @@ fn main() {
8494
assert!(output.status.success());
8595
assert!(output.stderr.is_empty());
8696
assert_eq!(output.stdout, b"passed\n");
97+
98+
let output = Command::new(&me).arg("exec-test6").output().unwrap();
99+
assert!(output.status.success());
100+
assert!(output.stderr.is_empty());
101+
assert_eq!(output.stdout, b"passed\n");
102+
103+
let output = Command::new(&me).arg("exec-test7").output().unwrap();
104+
assert!(output.status.success());
105+
assert!(output.stderr.is_empty());
106+
assert_eq!(output.stdout, b"passed\n");
87107
}

0 commit comments

Comments
 (0)