From cf0beff987cc01e4ab0dd66cb1931c49511956c9 Mon Sep 17 00:00:00 2001 From: Jakub Jelen Date: Tue, 29 Oct 2019 14:02:23 +0100 Subject: [PATCH] match: Avoid recursion with many asterisks in pattern Partially fixes T186 Signed-off-by: Jakub Jelen Reviewed-by: Andreas Schneider --- src/match.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/match.c b/src/match.c index 06587b92..5c85c7b0 100644 --- a/src/match.c +++ b/src/match.c @@ -38,6 +38,7 @@ #include "config.h" #include +#include #include #include "libssh/priv.h" @@ -46,7 +47,9 @@ * Returns true if the given string matches the pattern (which may contain ? * and * as wildcards), and zero if it does not match. */ -static int match_pattern(const char *s, const char *pattern) { +static int match_pattern(const char *s, const char *pattern) +{ + bool had_asterisk = false; if (s == NULL || pattern == NULL) { return 0; } @@ -57,16 +60,19 @@ static int match_pattern(const char *s, const char *pattern) { return (*s == '\0'); } - if (*pattern == '*') { + while (*pattern == '*') { /* Skip the asterisk. */ + had_asterisk = true; pattern++; + } + if (had_asterisk) { /* If at end of pattern, accept immediately. */ if (!*pattern) return 1; /* If next character in pattern is known, optimize. */ - if (*pattern != '?' && *pattern != '*') { + if (*pattern != '?') { /* * Look instances of the next character in * pattern, and try to match starting from