From be2474228267fc6622e35fa51e1f15901280c006 Mon Sep 17 00:00:00 2001 From: Zoltan Kiss <121870572+cj-zoltan-kiss@users.noreply.github.com> Date: Fri, 1 Mar 2024 15:46:29 +0100 Subject: [PATCH] parser: if closing square bracket not found, stop looking for it again This solves #5584, where the fuzzing would take hours without this. --- src/uucore/src/lib/parser/parse_glob.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/uucore/src/lib/parser/parse_glob.rs b/src/uucore/src/lib/parser/parse_glob.rs index 9215dd7bf..a4e12f467 100644 --- a/src/uucore/src/lib/parser/parse_glob.rs +++ b/src/uucore/src/lib/parser/parse_glob.rs @@ -18,7 +18,11 @@ fn fix_negation(glob: &str) -> String { while i + 3 < chars.len() { if chars[i] == '[' && chars[i + 1] == '^' { match chars[i + 3..].iter().position(|x| *x == ']') { - None => (), + None => { + // if closing square bracket not found, stop looking for it + // again + break; + } Some(j) => { chars[i + 1] = '!'; i += j + 4; @@ -90,6 +94,11 @@ mod tests { assert_eq!(fix_negation("[[]] [^a]"), "[[]] [!a]"); assert_eq!(fix_negation("[[] [^a]"), "[[] [!a]"); assert_eq!(fix_negation("[]] [^a]"), "[]] [!a]"); + + // test that we don't look for closing square brackets unnecessarily + // Verifies issue #5584 + let chars = std::iter::repeat("^[").take(174571).collect::(); + assert_eq!(fix_negation(chars.as_str()), chars); } #[test]