on master help sign in
Fix bug for file with invalid-utf8 filenames
commit82e20bfe14b05c3b3224cb1feb529c655a1e74e3 authorsharkdp <davidpeter@web.de> dateTue 21 Apr 2020, 14:09 (7 years ago) parent47abb19
3 files changed+19−7
src/bin/bat/app.rs+3−7 tests/examples/test-invalid-utf8-�(.rs+4 tests/integration_tests.rs+12
src/bin/bat/app.rs+3−7
@@ -244,24 +244,20 @@ impl App {
244244 }
245245 None => Box::new(std::iter::repeat(None)),
246246 };
247− let files: Option<Vec<&str>> = self
248− .matches
249− .values_of_os("FILE")
250− .map(|values| values.map(|fname| fname.to_str()).collect())
251− .unwrap_or(None);
247+ let files: Option<Vec<&OsStr>> = self.matches.values_of_os("FILE").map(|vs| vs.collect());
252248
253249 if files.is_none() {
254250 return Ok(vec![InputFile::StdIn(filenames_or_none.nth(0).unwrap())]);
255251 }
256252 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))),
258254 None => Box::new(std::iter::repeat(None)),
259255 };
260256
261257 let mut file_input = Vec::new();
262258 for (input, name) in files_or_none.zip(filenames_or_none) {
263259 if let Some(input) = input {
264− if input.to_str().unwrap() == "-" {
260+ if input.to_str().unwrap_or_default() == "-" {
265261 file_input.push(InputFile::StdIn(name));
266262 } else {
267263 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() {
628628 .failure();
629629}
630630
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+
631643#[test]
632644fn do_not_panic_regression_tests() {
633645 for filename in &[