Skip to content

Commit

Permalink
Merge pull request #4861 from sylvestre/ls-invalid-utf8
Browse files Browse the repository at this point in the history
ls: when facing an invalid utf-8, don't panic
  • Loading branch information
sylvestre authored May 15, 2023
2 parents 49c16a2 + 369a2a6 commit caaf25a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/uu/ls/src/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1997,7 +1997,12 @@ fn should_display(entry: &DirEntry, config: &Config) -> bool {
require_literal_separator: false,
case_sensitive: true,
};
let file_name = entry.file_name().into_string().unwrap();
let file_name = entry.file_name();
// If the decoding fails, still show an incorrect rendering
let file_name = match file_name.to_str() {
Some(s) => s.to_string(),
None => file_name.to_string_lossy().into_owned(),
};
!config
.ignore_patterns
.iter()
Expand Down
14 changes: 14 additions & 0 deletions tests/by-util/test_ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use crate::common::util::TestScenario;
use nix::unistd::{close, dup};
use regex::Regex;
use std::collections::HashMap;
#[cfg(target_os = "linux")]
use std::ffi::OsStr;
#[cfg(target_os = "linux")]
use std::os::unix::ffi::OsStrExt;
#[cfg(all(unix, feature = "chmod"))]
use std::os::unix::io::IntoRawFd;
use std::path::Path;
Expand Down Expand Up @@ -3434,3 +3438,13 @@ fn test_device_number() {
.succeeds()
.stdout_contains(major_minor_str);
}

#[test]
#[cfg(target_os = "linux")]
fn test_invalid_utf8() {
let (at, mut ucmd) = at_and_ucmd!();

let filename = OsStr::from_bytes(b"-\xE0-foo");
at.touch(filename);
ucmd.succeeds();
}

0 comments on commit caaf25a

Please sign in to comment.