mirror of
https://git.libssh.org/projects/libssh.git
synced 2026-02-09 18:04:25 +09:00
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 cf0beff987)
This commit is contained in:
committed by
Andreas Schneider
parent
8600015b3e
commit
13fa009a2e
12
src/match.c
12
src/match.c
@@ -38,6 +38,7 @@
|
|||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include "libssh/priv.h"
|
#include "libssh/priv.h"
|
||||||
@@ -46,7 +47,9 @@
|
|||||||
* Returns true if the given string matches the pattern (which may contain ?
|
* Returns true if the given string matches the pattern (which may contain ?
|
||||||
* and * as wildcards), and zero if it does not match.
|
* 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) {
|
if (s == NULL || pattern == NULL) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -57,16 +60,19 @@ static int match_pattern(const char *s, const char *pattern) {
|
|||||||
return (*s == '\0');
|
return (*s == '\0');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (*pattern == '*') {
|
while (*pattern == '*') {
|
||||||
/* Skip the asterisk. */
|
/* Skip the asterisk. */
|
||||||
|
had_asterisk = true;
|
||||||
pattern++;
|
pattern++;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (had_asterisk) {
|
||||||
/* If at end of pattern, accept immediately. */
|
/* If at end of pattern, accept immediately. */
|
||||||
if (!*pattern)
|
if (!*pattern)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
/* If next character in pattern is known, optimize. */
|
/* If next character in pattern is known, optimize. */
|
||||||
if (*pattern != '?' && *pattern != '*') {
|
if (*pattern != '?') {
|
||||||
/*
|
/*
|
||||||
* Look instances of the next character in
|
* Look instances of the next character in
|
||||||
* pattern, and try to match starting from
|
* pattern, and try to match starting from
|
||||||
|
|||||||
Reference in New Issue
Block a user