Skip to content

Commit 6212904

Browse files
committed
Don't use posix_spawn() if PATH was modified in the environment.
The expected behavior is that the environment's PATH should be used to find the process. posix_spawn() could be used if we iterated PATH to search for the binary to execute. For now just skip posix_spawn() if PATH is modified.
1 parent 00dac20 commit 6212904

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ impl Command {
184184
let maybe_env = self.env.capture_if_changed();
185185
maybe_env.map(|env| construct_envp(env, &mut self.saw_nul))
186186
}
187+
pub fn env_saw_path(&self) -> bool {
188+
self.env.have_changed_path()
189+
}
187190

188191
pub fn setup_io(&self, default: Stdio, needs_stdin: bool)
189192
-> io::Result<(StdioPipes, ChildPipes)> {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,7 @@ impl Command {
256256
if self.get_cwd().is_some() ||
257257
self.get_gid().is_some() ||
258258
self.get_uid().is_some() ||
259+
self.env_saw_path() ||
259260
self.get_closures().len() != 0 {
260261
return Ok(None)
261262
}

src/libstd/sys_common/process.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,15 @@ impl EnvKey for DefaultEnvKey {}
4747
#[derive(Clone, Debug)]
4848
pub struct CommandEnv<K> {
4949
clear: bool,
50+
saw_path: bool,
5051
vars: BTreeMap<K, Option<OsString>>
5152
}
5253

5354
impl<K: EnvKey> Default for CommandEnv<K> {
5455
fn default() -> Self {
5556
CommandEnv {
5657
clear: false,
58+
saw_path: false,
5759
vars: Default::default()
5860
}
5961
}
@@ -108,9 +110,11 @@ impl<K: EnvKey> CommandEnv<K> {
108110

109111
// The following functions build up changes
110112
pub fn set(&mut self, key: &OsStr, value: &OsStr) {
113+
self.maybe_saw_path(&key);
111114
self.vars.insert(key.to_owned().into(), Some(value.to_owned()));
112115
}
113116
pub fn remove(&mut self, key: &OsStr) {
117+
self.maybe_saw_path(&key);
114118
if self.clear {
115119
self.vars.remove(key);
116120
} else {
@@ -121,4 +125,12 @@ impl<K: EnvKey> CommandEnv<K> {
121125
self.clear = true;
122126
self.vars.clear();
123127
}
128+
pub fn have_changed_path(&self) -> bool {
129+
self.saw_path || self.clear
130+
}
131+
fn maybe_saw_path(&mut self, key: &OsStr) {
132+
if !self.saw_path && key.to_os_string() == OsString::from("PATH") {
133+
self.saw_path = true;
134+
}
135+
}
124136
}

0 commit comments

Comments
 (0)