Skip to content

Commit

Permalink
Workaround bug in globwalk.
Browse files Browse the repository at this point in the history
As identified in VirusTotal#280 there is a bug in globwalk when using relative paths. Fix
it by trying to canonicalize the path, and if that fails falls back to using the
relative path. This is fine because the reason canonicalization failed is
because the relative path was to a place that doesn't exist, and that does not
trigger the bug (it errors out before we get there). We intentionally want this
behavior so we get the nicer error reporting with colors. ;)

Fixes VirusTotal#280.
  • Loading branch information
wxsBSD committed Jan 9, 2025
1 parent cd61206 commit 7742534
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions cli/src/commands/scan.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;
use std::cmp::min;
use std::fs::File;
use std::fs::{canonicalize, File};
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex;
Expand Down Expand Up @@ -313,10 +313,25 @@ pub fn exec_scan(args: &ArgMatches) -> anyhow::Result<()> {

let rules_ref = &rules;

// Canonicalize the path to deal with a bug in globwalk.
//
// https://github.com/VirusTotal/yara-x/issues/280
// https://github.com/Gilnaa/globwalk/issues/28
//
// Note that if the path can not be canonicalized it is because it doesn't
// exist, and we are resorting to using the non-canonicalized path, which in
// this case is fine because we will get an error before globwalk crashes.
// We intentionally do not error out here because we want the nice error
// reporting provided later.
let canonicalized_path = match canonicalize(target_path) {
Ok(p) => p,
_ => target_path.to_owned(),
};

let mut w = if scan_list {
walk::ParWalker::file_list(target_path)
walk::ParWalker::file_list(&canonicalized_path)
} else {
walk::ParWalker::path(target_path)
walk::ParWalker::path(&canonicalized_path)
};

if let Some(num_threads) = num_threads {
Expand Down

0 comments on commit 7742534

Please sign in to comment.