From 4f4880fbd15122ff58dbc8df84fa31f54804856b Mon Sep 17 00:00:00 2001 From: "Victor M. Alvarez" Date: Sat, 18 Jan 2025 16:11:17 +0100 Subject: [PATCH] fix: don't strip prefix is path is exactly "./" or ".\". --- cli/src/tests/mod.rs | 19 +++++++++++++++++++ cli/src/walk.rs | 24 +++++++++++------------- 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/cli/src/tests/mod.rs b/cli/src/tests/mod.rs index 5bbe28ce..81f767fe 100644 --- a/cli/src/tests/mod.rs +++ b/cli/src/tests/mod.rs @@ -293,4 +293,23 @@ fn cli_issue_280() { .arg(".") .assert() .success(); + + // Handle special case of just ./ for path argument. + Command::cargo_bin("yr") + .unwrap() + .arg("scan") + .arg("src/tests/testdata/foo.yar") + .arg("./") + .assert() + .success(); + + // Handle special case of just .\ for path argument. + #[cfg(target_os = "windows")] + Command::cargo_bin("yr") + .unwrap() + .arg("scan") + .arg("src/tests/testdata/foo.yar") + .arg(r#".\"#) + .assert() + .success(); } diff --git a/cli/src/walk.rs b/cli/src/walk.rs index f7486f64..92c5bc5a 100644 --- a/cli/src/walk.rs +++ b/cli/src/walk.rs @@ -199,21 +199,19 @@ impl<'a> Walker<'a> { F: FnMut(&Path) -> anyhow::Result<()>, E: FnMut(anyhow::Error) -> anyhow::Result<()>, { - // Strip the ./ prefix (.\ in Windows), if present. This is a - // workaround for a bug in globwalk that causes a panic. + // Strip the ./ prefix (.\ in Windows), if present. Except for ".", + // "./" and ".\". This is a workaround for a bug in globwalk that + // causes a panic. // https://github.com/VirusTotal/yara-x/issues/280 // https://github.com/Gilnaa/globwalk/issues/28 - // - // Only perform the strip if the path is not exactly "." - this allows - // users to run "yr scan rules.yara ." to scan all the files in the - // current directory. - let path = if self.path.as_os_str().ne(".") { - #[cfg(not(target_os = "windows"))] - let path = self.path.strip_prefix("./").unwrap_or(self.path); - - #[cfg(target_os = "windows")] - let path = self.path.strip_prefix(r#".\"#).unwrap_or(self.path); - path + let path = if self.path.as_os_str().len() > 2 { + self.path + .strip_prefix(if cfg!(target_os = "windows") { + r#".\"# + } else { + "./" + }) + .unwrap_or(self.path) } else { self.path };