on master help sign in
Fix for Windows: do not run binaries from CWD

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`.

commitbf2b2df9c9e218e35e5a38ce3d03cffb7c363956 authorDavid Peter <mail@david-peter.de> dateMon 12 Jul 2021, 20:03 (5 years ago) parent3617c98
4 files changed+41−2
Cargo.lock+29 Cargo.toml+1 src/less.rs+3−1 src/output.rs+8−1
Cargo.lock+29
@@ -1,5 +1,7 @@
11# This file is automatically @generated by Cargo.
22# It is not intended for manual editing.
3+version = 3
4+
35[[package]]
46name = "adler"
57version = "1.0.2"
@@ -97,6 +99,7 @@ dependencies = [
9799 "error-chain",
98100 "git2",
99101 "globset",
102+ "grep-cli",
100103 "lazy_static",
101104 "nix",
102105 "path_abs",
@@ -487,6 +490,23 @@ dependencies = [
487490 "regex",
488491]
489492
493+[[package]]
494+name = "grep-cli"
495+version = "0.1.6"
496+source = "registry+https://github.com/rust-lang/crates.io-index"
497+checksum = "2dd110c34bb4460d0de5062413b773e385cbf8a85a63fc535590110a09e79e8a"
498+dependencies = [
499+ "atty",
500+ "bstr",
501+ "globset",
502+ "lazy_static",
503+ "log",
504+ "regex",
505+ "same-file",
506+ "termcolor",
507+ "winapi-util",
508+]
509+
490510[[package]]
491511name = "hashbrown"
492512version = "0.9.1"
@@ -1143,6 +1163,15 @@ dependencies = [
11431163 "winapi",
11441164]
11451165
1166+[[package]]
1167+name = "termcolor"
1168+version = "1.1.2"
1169+source = "registry+https://github.com/rust-lang/crates.io-index"
1170+checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
1171+dependencies = [
1172+ "winapi-util",
1173+]
1174+
11461175[[package]]
11471176name = "terminal_size"
11481177version = "0.1.16"
Cargo.toml+1
@@ -51,6 +51,7 @@ path_abs = { version = "0.5", default-features = false }
5151clircle = "0.3"
5252bugreport = "0.4"
5353dirs-next = { version = "2.0.0", optional = true }
54+grep-cli = "0.1.6"
5455
5556[dependencies.git2]
5657version = "0.13"
src/less.rs+3−1
@@ -4,7 +4,9 @@ use std::ffi::OsStr;
44use std::process::Command;
55
66pub fn retrieve_less_version(less_path: &dyn AsRef<OsStr>) -> Option<usize> {
7− let cmd = Command::new(less_path).arg("--version").output().ok()?;
7+ let resolved_path = grep_cli::resolve_binary(less_path.as_ref()).ok()?;
8+
9+ let cmd = Command::new(resolved_path).arg("--version").output().ok()?;
810 parse_less_version(&cmd.stdout)
911}
1012
src/output.rs+8−1
@@ -63,7 +63,14 @@ impl OutputType {
6363 return Err(ErrorKind::InvalidPagerValueBat.into());
6464 }
6565
66− let mut p = Command::new(&pager.bin);
66+ let resolved_path = match grep_cli::resolve_binary(&pager.bin) {
67+ Ok(path) => path,
68+ Err(_) => {
69+ return Ok(OutputType::stdout());
70+ }
71+ };
72+
73+ let mut p = Command::new(resolved_path);
6774 let args = pager.args;
6875
6976 if pager.kind == PagerKind::Less {