Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix handling invalid(?) unicode in filenames. #163

Merged
merged 4 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cli/src/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub use fix::*;
pub use fmt::*;
pub use scan::*;

use std::borrow::Cow;
use std::fs;
use std::io::stdout;
use std::path::PathBuf;
Expand Down Expand Up @@ -211,10 +212,10 @@ impl Component for CompileState {
}
}

fn truncate_with_ellipsis(s: String, max_length: usize) -> String {
fn truncate_with_ellipsis(s: Cow<str>, max_length: usize) -> Cow<str> {
if s.len() <= max_length {
s
} else {
format!("{}...", &s[..max_length - 3])
format!("{}...", &s[..max_length - 3]).into()
}
}
22 changes: 21 additions & 1 deletion cli/src/commands/scan.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::borrow::Cow;
use std::cmp::min;
use std::fs::File;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -526,6 +527,25 @@ impl ScanState {
}
}

// superconsole will not print any string that contains Unicode characters that
// are spaces but are not the ASCII space character, so we replace them all.
// See https://github.com/VirusTotal/yara-x/pull/163 for discussion.
fn replace_whitespace(path: &Path) -> Cow<str> {
let mut s = path.to_string_lossy();
if s.chars().any(|c| c != ' ' && c.is_whitespace()) {
let mut r = String::with_capacity(s.len());
for c in s.chars() {
if c.is_whitespace() {
r.push(' ')
} else {
r.push(c)
}
}
s = Cow::Owned(r);
}
s
}

impl Component for ScanState {
fn draw_unchecked(
&self,
Expand Down Expand Up @@ -566,7 +586,7 @@ impl Component for ScanState {
for (file, start_time) in
self.files_in_progress.lock().unwrap().iter()
{
let path = file.display().to_string();
let path = replace_whitespace(file);
// The length of the elapsed is 7 characters.
let spaces = " "
.repeat(dimensions.width.saturating_sub(path.len() + 7));
Expand Down
Loading