Skip to content

Commit 4f4880f

Browse files
committed
fix: don't strip prefix is path is exactly "./" or ".\".
1 parent 45da681 commit 4f4880f

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

cli/src/tests/mod.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,4 +293,23 @@ fn cli_issue_280() {
293293
.arg(".")
294294
.assert()
295295
.success();
296+
297+
// Handle special case of just ./ for path argument.
298+
Command::cargo_bin("yr")
299+
.unwrap()
300+
.arg("scan")
301+
.arg("src/tests/testdata/foo.yar")
302+
.arg("./")
303+
.assert()
304+
.success();
305+
306+
// Handle special case of just .\ for path argument.
307+
#[cfg(target_os = "windows")]
308+
Command::cargo_bin("yr")
309+
.unwrap()
310+
.arg("scan")
311+
.arg("src/tests/testdata/foo.yar")
312+
.arg(r#".\"#)
313+
.assert()
314+
.success();
296315
}

cli/src/walk.rs

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -199,21 +199,19 @@ impl<'a> Walker<'a> {
199199
F: FnMut(&Path) -> anyhow::Result<()>,
200200
E: FnMut(anyhow::Error) -> anyhow::Result<()>,
201201
{
202-
// Strip the ./ prefix (.\ in Windows), if present. This is a
203-
// workaround for a bug in globwalk that causes a panic.
202+
// Strip the ./ prefix (.\ in Windows), if present. Except for ".",
203+
// "./" and ".\". This is a workaround for a bug in globwalk that
204+
// causes a panic.
204205
// https://github.com/VirusTotal/yara-x/issues/280
205206
// https://github.com/Gilnaa/globwalk/issues/28
206-
//
207-
// Only perform the strip if the path is not exactly "." - this allows
208-
// users to run "yr scan rules.yara ." to scan all the files in the
209-
// current directory.
210-
let path = if self.path.as_os_str().ne(".") {
211-
#[cfg(not(target_os = "windows"))]
212-
let path = self.path.strip_prefix("./").unwrap_or(self.path);
213-
214-
#[cfg(target_os = "windows")]
215-
let path = self.path.strip_prefix(r#".\"#).unwrap_or(self.path);
216-
path
207+
let path = if self.path.as_os_str().len() > 2 {
208+
self.path
209+
.strip_prefix(if cfg!(target_os = "windows") {
210+
r#".\"#
211+
} else {
212+
"./"
213+
})
214+
.unwrap_or(self.path)
217215
} else {
218216
self.path
219217
};

0 commit comments

Comments
 (0)