From 82e20bfe14b05c3b3224cb1feb529c655a1e74e3 Mon Sep 17 00:00:00 2001 From: sharkdp Date: Tue, 21 Apr 2020 14:09:05 +0200 Subject: [PATCH] Fix bug for file with invalid-utf8 filenames --- src/bin/bat/app.rs | 10 +++------- tests/examples/test-invalid-utf8-Ã(.rs | 4 ++++ tests/integration_tests.rs | 12 ++++++++++++ 3 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 tests/examples/test-invalid-utf8-Ã(.rs diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index 7c72f213..4a04f952 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -244,24 +244,20 @@ impl App { } None => Box::new(std::iter::repeat(None)), }; - let files: Option> = self - .matches - .values_of_os("FILE") - .map(|values| values.map(|fname| fname.to_str()).collect()) - .unwrap_or(None); + let files: Option> = 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> = 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); diff --git a/tests/examples/test-invalid-utf8-Ã(.rs b/tests/examples/test-invalid-utf8-Ã(.rs new file mode 100644 index 00000000..bcd68d2b --- /dev/null +++ b/tests/examples/test-invalid-utf8-Ã(.rs @@ -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); +} diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index c589c925..c318aa47 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -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 &[