Skip to content

Commit

Permalink
Fix for Windows: do not run binaries from CWD
Browse files Browse the repository at this point in the history
This fixes a bug on Windows where `Command::new` would also run
executables from the current working directory, possibly resulting in
accidental runs of programs called `less`.
  • Loading branch information
sharkdp committed Jul 12, 2021
1 parent 3617c98 commit bf2b2df
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
29 changes: 29 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ path_abs = { version = "0.5", default-features = false }
clircle = "0.3"
bugreport = "0.4"
dirs-next = { version = "2.0.0", optional = true }
grep-cli = "0.1.6"

[dependencies.git2]
version = "0.13"
Expand Down
4 changes: 3 additions & 1 deletion src/less.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::ffi::OsStr;
use std::process::Command;

pub fn retrieve_less_version(less_path: &dyn AsRef<OsStr>) -> Option<usize> {
let cmd = Command::new(less_path).arg("--version").output().ok()?;
let resolved_path = grep_cli::resolve_binary(less_path.as_ref()).ok()?;

let cmd = Command::new(resolved_path).arg("--version").output().ok()?;
parse_less_version(&cmd.stdout)
}

Expand Down
9 changes: 8 additions & 1 deletion src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,14 @@ impl OutputType {
return Err(ErrorKind::InvalidPagerValueBat.into());
}

let mut p = Command::new(&pager.bin);
let resolved_path = match grep_cli::resolve_binary(&pager.bin) {
Ok(path) => path,
Err(_) => {
return Ok(OutputType::stdout());
}
};

let mut p = Command::new(resolved_path);
let args = pager.args;

if pager.kind == PagerKind::Less {
Expand Down

0 comments on commit bf2b2df

Please sign in to comment.