Skip to content

Commit db2105c

Browse files
committed
du: do not panic on a non-UTF-8 name in the --exclude check
The regular (non-safe) traversal — used with `-L`/`--dereference`, and always on non-Linux — matched `--exclude` patterns against `entry.file_name().into_string().unwrap()`, which aborts on any non-UTF-8 entry name. Use `to_string_lossy()` instead, matching the sibling path check on the same line, so a non-UTF-8 name is handled gracefully like GNU.
1 parent 542e7af commit db2105c

2 files changed

Lines changed: 16 additions & 1 deletion

File tree

src/uu/du/src/du.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ fn du_regular(
653653
// if we have 'du foo' but search to exclude 'foo/bar'
654654
// we need the full path
655655
if pattern.matches(&this_stat.path.to_string_lossy())
656-
|| pattern.matches(&entry.file_name().into_string().unwrap())
656+
|| pattern.matches(&entry.file_name().to_string_lossy())
657657
{
658658
// if the directory is ignored, leave early
659659
if options.verbose {

tests/by-util/test_du.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,21 @@ fn test_du_exclude() {
13641364
.stdout_contains("'subdir' ignored");
13651365
}
13661366

1367+
#[test]
1368+
#[cfg(unix)]
1369+
fn test_du_exclude_non_utf8_name() {
1370+
use std::ffi::OsStr;
1371+
use std::os::unix::ffi::OsStrExt;
1372+
1373+
let ts = TestScenario::new(util_name!());
1374+
let at = &ts.fixtures;
1375+
1376+
at.mkdir("d");
1377+
at.mkdir(OsStr::from_bytes(b"d/\xff\xfesub"));
1378+
1379+
ts.ucmd().args(&["-L", "--exclude=zzz", "d"]).succeeds();
1380+
}
1381+
13671382
#[test]
13681383
// Disable on Windows because we are looking for /
13691384
// And the tests would be more complex if we have to support \ too

0 commit comments

Comments
 (0)