dircolors: don't panic on an invalid TERM/COLORTERM glob (#12728)

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.
This commit is contained in:
Wei Li
2026-06-10 06:16:55 +10:00
committed by GitHub
parent 47f549a8fa
commit 3db0433c41
2 changed files with 11 additions and 1 deletions
+2 -1
View File
@@ -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))
}
}
+9
View File
@@ -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");
}