Skip to content

Commit

Permalink
parser: if closing square bracket not found, stop looking for it again
Browse files Browse the repository at this point in the history
This solves uutils#5584, where the fuzzing would take hours without this.
  • Loading branch information
cj-zoltan-kiss committed Mar 11, 2024
1 parent 09048a3 commit be24742
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/uucore/src/lib/parser/parse_glob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<String>();
assert_eq!(fix_negation(chars.as_str()), chars);
}

#[test]
Expand Down

0 comments on commit be24742

Please # to comment.