Skip to content

Commit

Permalink
fix: don't strip prefix is path is exactly "./" or ".\".
Browse files Browse the repository at this point in the history
  • Loading branch information
plusvic committed Jan 18, 2025
1 parent 45da681 commit 4f4880f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
19 changes: 19 additions & 0 deletions cli/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
24 changes: 11 additions & 13 deletions cli/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand Down

0 comments on commit 4f4880f

Please sign in to comment.