Commit d5a51910 by Russell Belfer

Import DOS fix for fnmatch

Because fnmatch uses recursion, there were some input sequences
that cause seriously degenerate behavior.  This imports a fix
that imposes a max recursion limiter to avoid the worst of it.
parent 824d5e4d
...@@ -24,13 +24,16 @@ ...@@ -24,13 +24,16 @@
static int rangematch(const char *, char, int, char **); static int rangematch(const char *, char, int, char **);
int static int
p_fnmatch(const char *pattern, const char *string, int flags) p_fnmatchx(const char *pattern, const char *string, int flags, size_t recurs)
{ {
const char *stringstart; const char *stringstart;
char *newp; char *newp;
char c, test; char c, test;
if (recurs-- == 0)
return FNM_NORES;
for (stringstart = string;;) for (stringstart = string;;)
switch (c = *pattern++) { switch (c = *pattern++) {
case EOS: case EOS:
...@@ -75,8 +78,11 @@ p_fnmatch(const char *pattern, const char *string, int flags) ...@@ -75,8 +78,11 @@ p_fnmatch(const char *pattern, const char *string, int flags)
/* General case, use recursion. */ /* General case, use recursion. */
while ((test = *string) != EOS) { while ((test = *string) != EOS) {
if (!p_fnmatch(pattern, string, flags & ~FNM_PERIOD)) int e;
return (0);
e = p_fnmatchx(pattern, string, flags & ~FNM_PERIOD, recurs);
if (e != FNM_NOMATCH)
return e;
if (test == '/' && (flags & FNM_PATHNAME)) if (test == '/' && (flags & FNM_PATHNAME))
break; break;
++string; ++string;
...@@ -178,3 +184,9 @@ rangematch(const char *pattern, char test, int flags, char **newp) ...@@ -178,3 +184,9 @@ rangematch(const char *pattern, char test, int flags, char **newp)
return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH); return (ok == negate ? RANGE_NOMATCH : RANGE_MATCH);
} }
int
p_fnmatch(const char *pattern, const char *string, int flags)
{
return p_fnmatchx(pattern, string, flags, 64);
}
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#define FNM_NOMATCH 1 /* Match failed. */ #define FNM_NOMATCH 1 /* Match failed. */
#define FNM_NOSYS 2 /* Function not supported (unused). */ #define FNM_NOSYS 2 /* Function not supported (unused). */
#define FNM_NORES 3 /* Out of resources */
#define FNM_NOESCAPE 0x01 /* Disable backslash escaping. */ #define FNM_NOESCAPE 0x01 /* Disable backslash escaping. */
#define FNM_PATHNAME 0x02 /* Slash must be matched by slash. */ #define FNM_PATHNAME 0x02 /* Slash must be matched by slash. */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment