Fix bug for file with invalid-utf8 filenames

This commit is contained in:
sharkdp 2020-04-21 14:09:05 +02:00
parent 47abb192bc
commit 82e20bfe14
3 changed files with 19 additions and 7 deletions

View File

@ -244,24 +244,20 @@ impl App {
}
None => Box::new(std::iter::repeat(None)),
};
let files: Option<Vec<&str>> = self
.matches
.values_of_os("FILE")
.map(|values| values.map(|fname| fname.to_str()).collect())
.unwrap_or(None);
let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
if files.is_none() {
return Ok(vec![InputFile::StdIn(filenames_or_none.nth(0).unwrap())]);
}
let files_or_none: Box<dyn Iterator<Item = _>> = match files {
Some(ref files) => Box::new(files.into_iter().map(|name| Some(OsStr::new(*name)))),
Some(ref files) => Box::new(files.into_iter().map(|name| Some(*name))),
None => Box::new(std::iter::repeat(None)),
};
let mut file_input = Vec::new();
for (input, name) in files_or_none.zip(filenames_or_none) {
if let Some(input) = input {
if input.to_str().unwrap() == "-" {
if input.to_str().unwrap_or_default() == "-" {
file_input.push(InputFile::StdIn(name));
} else {
let mut ofile = OrdinaryFile::from_path(input);

View File

@ -0,0 +1,4 @@
fn print_square(num: f64) {
let result = f64::powf(num, 2.0);
println!("The square of {:.2} is {:.2}.", num, result);
}

View File

@ -628,6 +628,18 @@ fn filename_multiple_err() {
.failure();
}
#[cfg(target_os = "linux")]
#[test]
fn file_with_invalid_utf8_filename() {
use std::ffi::OsStr;
use std::os::unix::ffi::OsStrExt;
bat()
.arg(OsStr::from_bytes(b"test-invalid-utf8-\xC3(.rs"))
.assert()
.success();
}
#[test]
fn do_not_panic_regression_tests() {
for filename in &[