on master help sign in
Fix bug for file with invalid-utf8 filenames
82e20bfsharkdp
src/bin/bat/app.rs+3−7
@@ -244,24 +244,20 @@ impl App {
244 }
244 }
245 None => Box::new(std::iter::repeat(None)),
245 None => Box::new(std::iter::repeat(None)),
246 };
246 };
247− let files: Option<Vec<&str>> = self
247+ let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
248− .matches
249− .values_of_os("FILE")
250− .map(|values| values.map(|fname| fname.to_str()).collect())
251− .unwrap_or(None);
252
248
253 if files.is_none() {
249 if files.is_none() {
254 return Ok(vec![InputFile::StdIn(filenames_or_none.nth(0).unwrap())]);
250 return Ok(vec![InputFile::StdIn(filenames_or_none.nth(0).unwrap())]);
255 }
251 }
256 let files_or_none: Box<dyn Iterator<Item = _>> = match files {
252 let files_or_none: Box<dyn Iterator<Item = _>> = match files {
257− Some(ref files) => Box::new(files.into_iter().map(|name| Some(OsStr::new(*name)))),
253+ Some(ref files) => Box::new(files.into_iter().map(|name| Some(*name))),
258 None => Box::new(std::iter::repeat(None)),
254 None => Box::new(std::iter::repeat(None)),
259 };
255 };
260
256
261 let mut file_input = Vec::new();
257 let mut file_input = Vec::new();
262 for (input, name) in files_or_none.zip(filenames_or_none) {
258 for (input, name) in files_or_none.zip(filenames_or_none) {
263 if let Some(input) = input {
259 if let Some(input) = input {
264− if input.to_str().unwrap() == "-" {
260+ if input.to_str().unwrap_or_default() == "-" {
265 file_input.push(InputFile::StdIn(name));
261 file_input.push(InputFile::StdIn(name));
266 } else {
262 } else {
267 let mut ofile = OrdinaryFile::from_path(input);
263 let mut ofile = OrdinaryFile::from_path(input);
tests/examples/test-invalid-utf8-�(.rsnew file+4
@@ -0,0 +1,4 @@
1+fn print_square(num: f64) {
2+ let result = f64::powf(num, 2.0);
3+ println!("The square of {:.2} is {:.2}.", num, result);
4+}
tests/integration_tests.rs+12
@@ -628,6 +628,18 @@ fn filename_multiple_err() {
628 .failure();
628 .failure();
629}
629}
630
630
631+#[cfg(target_os = "linux")]
632+#[test]
633+fn file_with_invalid_utf8_filename() {
634+ use std::ffi::OsStr;
635+ use std::os::unix::ffi::OsStrExt;
636+
637+ bat()
638+ .arg(OsStr::from_bytes(b"test-invalid-utf8-\xC3(.rs"))
639+ .assert()
640+ .success();
641+}
642+
631#[test]
643#[test]
632fn do_not_panic_regression_tests() {
644fn do_not_panic_regression_tests() {
633 for filename in &[
645 for filename in &[