Skip to content

Commit 7226f41

Browse files
author
Alex Gaynor
committed
Fixes #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 485397e commit 7226f41

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

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

+13-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ use ptr;
1717
use sys::cvt;
1818
use sys::process::process_common::*;
1919

20+
#[cfg(all(not(target_os = "android"), not(target_env = "musl")))]
21+
const DEFAULT_PATH: &[u8] = b"/bin:/usr/bin";
22+
// Musl has a different default path.
23+
#[cfg(target_env = "musl")]
24+
const DEFAULT_PATH: &[u8] = b"/usr/local/bin:/bin:/usr/bin";
25+
// Android has a different default path.
26+
#[cfg(target_os = "android")]
27+
const DEFAULT_PATH: &[u8] = b"/sbin:/system/sbin:/system/bin:/system/xbin:\
28+
/odm/bin:/vendor/bin:/vendor/xbin";
29+
2030
////////////////////////////////////////////////////////////////////////////////
2131
// Command
2232
////////////////////////////////////////////////////////////////////////////////
@@ -135,14 +145,15 @@ impl Command {
135145
Some(envp) => {
136146
match envp.get_items().iter().find(|var| var.as_bytes().starts_with(b"PATH=")) {
137147
Some(p) => &p.as_bytes()[5..],
138-
None => return None,
148+
// If there's no PATH, fall back to the default (which varies by platform).
149+
None => DEFAULT_PATH,
139150
}
140151
},
141152
// maybe_envp is None if the process isn't changing the parent's env at all.
142153
None => {
143154
match parent_path.as_ref() {
144155
Some(p) => p.as_bytes(),
145-
None => return None,
156+
None => DEFAULT_PATH,
146157
}
147158
},
148159
};

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)