Skip to content
This repository was archived by the owner on Jan 26, 2026. It is now read-only.

Commit 13fa009

Browse files
Jakujecryptomilk
authored andcommitted
match: Avoid recursion with many asterisks in pattern
Partially fixes T186 Signed-off-by: Jakub Jelen <jjelen@redhat.com> Reviewed-by: Andreas Schneider <asn@cryptomilk.org> (cherry picked from commit cf0beff)
1 parent 8600015 commit 13fa009

1 file changed

Lines changed: 9 additions & 3 deletions

File tree

src/match.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "config.h"
3939

4040
#include <ctype.h>
41+
#include <stdbool.h>
4142
#include <sys/types.h>
4243

4344
#include "libssh/priv.h"
@@ -46,7 +47,9 @@
4647
* Returns true if the given string matches the pattern (which may contain ?
4748
* and * as wildcards), and zero if it does not match.
4849
*/
49-
static int match_pattern(const char *s, const char *pattern) {
50+
static int match_pattern(const char *s, const char *pattern)
51+
{
52+
bool had_asterisk = false;
5053
if (s == NULL || pattern == NULL) {
5154
return 0;
5255
}
@@ -57,16 +60,19 @@ static int match_pattern(const char *s, const char *pattern) {
5760
return (*s == '\0');
5861
}
5962

60-
if (*pattern == '*') {
63+
while (*pattern == '*') {
6164
/* Skip the asterisk. */
65+
had_asterisk = true;
6266
pattern++;
67+
}
6368

69+
if (had_asterisk) {
6470
/* If at end of pattern, accept immediately. */
6571
if (!*pattern)
6672
return 1;
6773

6874
/* If next character in pattern is known, optimize. */
69-
if (*pattern != '?' && *pattern != '*') {
75+
if (*pattern != '?') {
7076
/*
7177
* Look instances of the next character in
7278
* pattern, and try to match starting from

0 commit comments

Comments
 (0)