Skip to content

Commit

Permalink
Fixes rust-lang#55775 -- fixed regression in Command::exec's handling…
Browse files Browse the repository at this point in the history
… 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"
  • Loading branch information
Alex Gaynor committed Nov 13, 2018
1 parent 485397e commit 7226f41
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/libstd/sys/unix/process/process_unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ use ptr;
use sys::cvt;
use sys::process::process_common::*;

#[cfg(all(not(target_os = "android"), not(target_env = "musl")))]
const DEFAULT_PATH: &[u8] = b"/bin:/usr/bin";
// Musl has a different default path.
#[cfg(target_env = "musl")]
const DEFAULT_PATH: &[u8] = b"/usr/local/bin:/bin:/usr/bin";
// Android has a different default path.
#[cfg(target_os = "android")]
const DEFAULT_PATH: &[u8] = b"/sbin:/system/sbin:/system/bin:/system/xbin:\
/odm/bin:/vendor/bin:/vendor/xbin";

////////////////////////////////////////////////////////////////////////////////
// Command
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -135,14 +145,15 @@ impl Command {
Some(envp) => {
match envp.get_items().iter().find(|var| var.as_bytes().starts_with(b"PATH=")) {
Some(p) => &p.as_bytes()[5..],
None => return None,
// If there's no PATH, fall back to the default (which varies by platform).
None => DEFAULT_PATH,
}
},
// maybe_envp is None if the process isn't changing the parent's env at all.
None => {
match parent_path.as_ref() {
Some(p) => p.as_bytes(),
None => return None,
None => DEFAULT_PATH,
}
},
};
Expand Down
20 changes: 20 additions & 0 deletions src/test/run-pass/command-exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ fn main() {
println!("passed");
}

"exec-test6" => {
let err = Command::new("echo").arg("passed").env_clear().exec();
panic!("failed to spawn: {}", err);
}

"exec-test7" => {
let err = Command::new("echo").arg("passed").env_remove("PATH").exec();
panic!("failed to spawn: {}", err);
}

_ => panic!("unknown argument: {}", arg),
}
return
Expand Down Expand Up @@ -84,4 +94,14 @@ fn main() {
assert!(output.status.success());
assert!(output.stderr.is_empty());
assert_eq!(output.stdout, b"passed\n");

let output = Command::new(&me).arg("exec-test6").output().unwrap();
assert!(output.status.success());
assert!(output.stderr.is_empty());
assert_eq!(output.stdout, b"passed\n");

let output = Command::new(&me).arg("exec-test7").output().unwrap();
assert!(output.status.success());
assert!(output.stderr.is_empty());
assert_eq!(output.stdout, b"passed\n");
}

0 comments on commit 7226f41

Please sign in to comment.