From c86ab87e80b35b98139c7387262eac997ce22121 Mon Sep 17 00:00:00 2001 From: weili <541602953@qq.com> Date: Thu, 11 Jun 2026 06:57:10 +0000 Subject: [PATCH] du: do not panic on a non-UTF-8 name in the --exclude check MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/uu/du/src/du.rs | 2 +- tests/by-util/test_du.rs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/uu/du/src/du.rs b/src/uu/du/src/du.rs index fa91da7ac24..b838ca6fa10 100644 --- a/src/uu/du/src/du.rs +++ b/src/uu/du/src/du.rs @@ -653,7 +653,7 @@ fn du_regular( // if we have 'du foo' but search to exclude 'foo/bar' // we need the full path if pattern.matches(&this_stat.path.to_string_lossy()) - || pattern.matches(&entry.file_name().into_string().unwrap()) + || pattern.matches(&entry.file_name().to_string_lossy()) { // if the directory is ignored, leave early if options.verbose { diff --git a/tests/by-util/test_du.rs b/tests/by-util/test_du.rs index 7dfda01fd4a..aed4be42775 100644 --- a/tests/by-util/test_du.rs +++ b/tests/by-util/test_du.rs @@ -1364,6 +1364,23 @@ fn test_du_exclude() { .stdout_contains("'subdir' ignored"); } +#[test] +#[cfg(unix)] +fn test_du_exclude_non_utf8_name() { + use std::ffi::OsStr; + use std::os::unix::ffi::OsStrExt; + + let ts = TestScenario::new(util_name!()); + let at = &ts.fixtures; + + at.mkdir("d"); + let mut name = std::ffi::OsString::from("d/"); + name.push(OsStr::from_bytes(&[0xff, 0xfe])); + at.mkdir(name); + + ts.ucmd().args(&["-L", "--exclude=zzz", "d"]).succeeds(); +} + #[test] // Disable on Windows because we are looking for / // And the tests would be more complex if we have to support \ too