From 3db0433c41aefec5d5514d0273a9bedea713f7fd Mon Sep 17 00:00:00 2001 From: Wei Li <541602953@qq.com> Date: Wed, 10 Jun 2026 06:16:55 +1000 Subject: [PATCH] dircolors: don't panic on an invalid TERM/COLORTERM glob (#12728) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A database file with a TERM/COLORTERM line whose value is an invalid glob (e.g. an unclosed `[`) aborted dircolors with a Result::unwrap panic: `fnmatch` did `parse_glob::from_str(pat).unwrap().matches(self)`, and from_str returns Err for an unparseable pattern. Use `is_ok_and`, so an invalid glob is treated as a non-match (and the unmatched TERM section is skipped) instead of crashing — matching GNU, which emits an empty LS_COLORS and exits 0. --- src/uu/dircolors/src/dircolors.rs | 3 ++- tests/by-util/test_dircolors.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/uu/dircolors/src/dircolors.rs b/src/uu/dircolors/src/dircolors.rs index 311aa3af9..05a30a8eb 100644 --- a/src/uu/dircolors/src/dircolors.rs +++ b/src/uu/dircolors/src/dircolors.rs @@ -310,7 +310,8 @@ impl StrUtils for str { } fn fnmatch(&self, pat: &str) -> bool { - parse_glob::from_str(pat).unwrap().matches(self) + // An invalid glob never matches (GNU ignores it); don't unwrap the Err. + parse_glob::from_str(pat).is_ok_and(|glob| glob.matches(self)) } } diff --git a/tests/by-util/test_dircolors.rs b/tests/by-util/test_dircolors.rs index 2be2671af..871151fe3 100644 --- a/tests/by-util/test_dircolors.rs +++ b/tests/by-util/test_dircolors.rs @@ -254,3 +254,12 @@ fn test_colorterm_empty_with_wildcard() { .succeeds() .stdout_only("LS_COLORS='';\nexport LS_COLORS\n"); } + +#[test] +fn test_invalid_term_glob() { + new_ucmd!() + .pipe_in("TERM [\nDIR 01;34\n") + .args(&["-b", "-"]) + .succeeds() + .stdout_only("LS_COLORS='';\nexport LS_COLORS\n"); +}