summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsharkdp <davidpeter@web.de>2020-04-21 14:09:05 +0200
committersharkdp <davidpeter@web.de>2020-04-21 14:09:18 +0200
commit82e20bfe14b05c3b3224cb1feb529c655a1e74e3 (patch)
treeffb46cd3752ec020db408d5a0d7383e61b313f03
parent47abb192bc7476379986b13834d7c02406ed6a6b (diff)
Fix bug for file with invalid-utf8 filenames
-rw-r--r--src/bin/bat/app.rs10
-rw-r--r--tests/examples/test-invalid-utf8-Ã(.rs4
-rw-r--r--tests/integration_tests.rs12
3 files changed, 19 insertions, 7 deletions
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<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);
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 &[