From d171a6e4448d23e8a9c2b5a0898dfe9df226ba02 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Wed, 9 Feb 2022 22:17:47 +0100 Subject: [PATCH] match: Optimize pattern matching even more The adjacent question marks and asterisks can be simplified to single wildcard so there is no need to excersise all the recursive pattern matching. These inputs were generated by oss-fuzz and probably caused also the previously reported timeouts. Signed-off-by: Jakub Jelen Reviewed-by: Andreas Schneider --- src/match.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/match.c b/src/match.c index f19f73de..555a5dd1 100644 --- a/src/match.c +++ b/src/match.c @@ -63,9 +63,11 @@ static int match_pattern(const char *s, const char *pattern, size_t limit) return (*s == '\0'); } - while (*pattern == '*') { - /* Skip all the asterisks. */ - had_asterisk = true; + /* Skip all the asterisks and adjacent question marks */ + while (*pattern == '*' || (had_asterisk && *pattern == '?')) { + if (*pattern == '*') { + had_asterisk = true; + } pattern++; }