1
0
mirror of https://github.com/moparisthebest/curl synced 2024-12-23 08:38:49 -05:00

fnmatch: pattern syntax can no longer fail

Whenever an expected pattern syntax rule cannot be matched, the
character starting the rule loses its special meaning and the parsing
is resumed:
- backslash at the end of pattern string matches itself.
- Error in [:keyword:] results in set containing :\[dekorwy.

Unit test 1307 updated for this new situation.

Closes #2273
This commit is contained in:
Patrick Monnerat 2018-01-30 20:22:17 +01:00
parent fcaa1826bd
commit da5f4b1d84
2 changed files with 84 additions and 116 deletions

View File

@ -46,11 +46,6 @@
#define CURLFNM_SPACE (CURLFNM_CHARSET_LEN + 9) #define CURLFNM_SPACE (CURLFNM_CHARSET_LEN + 9)
#define CURLFNM_UPPER (CURLFNM_CHARSET_LEN + 10) #define CURLFNM_UPPER (CURLFNM_CHARSET_LEN + 10)
typedef enum {
CURLFNM_LOOP_DEFAULT = 0,
CURLFNM_LOOP_BACKSLASH
} loop_state;
typedef enum { typedef enum {
CURLFNM_SCHS_DEFAULT = 0, CURLFNM_SCHS_DEFAULT = 0,
CURLFNM_SCHS_RIGHTBR, CURLFNM_SCHS_RIGHTBR,
@ -86,12 +81,12 @@ static int parsekeyword(unsigned char **pattern, unsigned char *charset)
return SETCHARSET_FAIL; return SETCHARSET_FAIL;
switch(state) { switch(state) {
case CURLFNM_PKW_INIT: case CURLFNM_PKW_INIT:
if(ISALPHA(c) && ISLOWER(c)) if(ISLOWER(c))
keyword[i] = c; keyword[i] = c;
else if(c == ':') else if(c == ':')
state = CURLFNM_PKW_DDOT; state = CURLFNM_PKW_DDOT;
else else
return 0; return SETCHARSET_FAIL;
break; break;
case CURLFNM_PKW_DDOT: case CURLFNM_PKW_DDOT:
if(c == ']') if(c == ']')
@ -186,15 +181,10 @@ static int setcharset(unsigned char **p, unsigned char *charset)
(*p)++; (*p)++;
} }
else if(c == '[') { else if(c == '[') {
char c2 = *((*p) + 1); unsigned char *pp = *p + 1;
if(c2 == ':') { /* there has to be a keyword */
(*p) += 2; if(*pp++ == ':' && parsekeyword(&pp, charset))
if(parsekeyword(p, charset)) { *p = pp;
state = CURLFNM_SCHS_DEFAULT;
}
else
return SETCHARSET_FAIL;
}
else { else {
charset[c] = 1; charset[c] = 1;
(*p)++; (*p)++;
@ -248,14 +238,11 @@ static int setcharset(unsigned char **p, unsigned char *charset)
goto fail; goto fail;
break; break;
case CURLFNM_SCHS_RIGHTBRLEFTBR: case CURLFNM_SCHS_RIGHTBRLEFTBR:
if(c == ']') { if(c == ']')
return SETCHARSET_OK; return SETCHARSET_OK;
}
else {
state = CURLFNM_SCHS_DEFAULT; state = CURLFNM_SCHS_DEFAULT;
charset[c] = 1; charset[c] = 1;
(*p)++; (*p)++;
}
break; break;
} }
} }
@ -266,52 +253,46 @@ fail:
static int loop(const unsigned char *pattern, const unsigned char *string, static int loop(const unsigned char *pattern, const unsigned char *string,
int maxstars) int maxstars)
{ {
loop_state state = CURLFNM_LOOP_DEFAULT;
unsigned char *p = (unsigned char *)pattern; unsigned char *p = (unsigned char *)pattern;
unsigned char *s = (unsigned char *)string; unsigned char *s = (unsigned char *)string;
unsigned char charset[CURLFNM_CHSET_SIZE] = { 0 }; unsigned char charset[CURLFNM_CHSET_SIZE] = { 0 };
int rc = 0; int rc = 0;
for(;;) { for(;;) {
switch(state) { unsigned char *pp;
case CURLFNM_LOOP_DEFAULT:
if(*p == '*') { switch(*p) {
case '*':
if(!maxstars) if(!maxstars)
return CURL_FNMATCH_NOMATCH; return CURL_FNMATCH_NOMATCH;
while(*(p + 1) == '*') /* eliminate multiple stars */ while(p[1] == '*') /* eliminate multiple stars */
p++; p++;
if(*s == '\0' && *(p + 1) == '\0') if(*s == '\0' && p[1] == '\0')
return CURL_FNMATCH_MATCH; return CURL_FNMATCH_MATCH;
rc = loop(p + 1, s, maxstars - 1); /* *.txt matches .txt <=> rc = loop(p + 1, s, maxstars - 1); /* *.txt matches .txt <=>
.txt matches .txt */ .txt matches .txt */
if(rc == CURL_FNMATCH_MATCH) if(rc == CURL_FNMATCH_MATCH)
return CURL_FNMATCH_MATCH; return CURL_FNMATCH_MATCH;
if(*s) /* let the star eat up one character */ if(!*s)
s++; return CURL_FNMATCH_NOMATCH;
else s++; /* let the star eat up one character */
break;
case '?':
if(!*s)
return CURL_FNMATCH_NOMATCH; return CURL_FNMATCH_NOMATCH;
}
else if(*p == '?') {
if(ISPRINT(*s)) {
s++; s++;
p++; p++;
} break;
else if(*s == '\0') case '\0':
return CURL_FNMATCH_NOMATCH; return *s? CURL_FNMATCH_NOMATCH: CURL_FNMATCH_MATCH;
else case '\\':
return CURL_FNMATCH_FAIL; /* cannot deal with other character */ if(p[1])
}
else if(*p == '\0') {
if(*s == '\0')
return CURL_FNMATCH_MATCH;
return CURL_FNMATCH_NOMATCH;
}
else if(*p == '\\') {
state = CURLFNM_LOOP_BACKSLASH;
p++; p++;
} if(*s++ != *p++)
else if(*p == '[') { return CURL_FNMATCH_NOMATCH;
unsigned char *pp = p + 1; /* cannot handle with pointer to register */ break;
case '[':
pp = p + 1; /* Copy in case of syntax error in set. */
if(setcharset(&pp, charset)) { if(setcharset(&pp, charset)) {
int found = FALSE; int found = FALSE;
if(!*s) if(!*s)
@ -342,32 +323,18 @@ static int loop(const unsigned char *pattern, const unsigned char *string,
if(charset[CURLFNM_NEGATE]) if(charset[CURLFNM_NEGATE])
found = !found; found = !found;
if(found) { if(!found)
return CURL_FNMATCH_NOMATCH;
p = pp + 1; p = pp + 1;
s++; s++;
}
else
return CURL_FNMATCH_NOMATCH;
}
else {
if(*p++ != *s++)
return CURL_FNMATCH_NOMATCH;
}
}
else {
if(*p++ != *s++)
return CURL_FNMATCH_NOMATCH;
}
break; break;
case CURLFNM_LOOP_BACKSLASH:
if(ISPRINT(*p)) {
if(*p++ == *s++)
state = CURLFNM_LOOP_DEFAULT;
else
return CURL_FNMATCH_NOMATCH;
} }
else
return CURL_FNMATCH_FAIL; /* Syntax error in set: this must be taken as a regular character. */
/* FALLTHROUGH */
default:
if(*p++ != *s++)
return CURL_FNMATCH_NOMATCH;
break; break;
} }
} }

View File

@ -25,7 +25,6 @@
#define MATCH CURL_FNMATCH_MATCH #define MATCH CURL_FNMATCH_MATCH
#define NOMATCH CURL_FNMATCH_NOMATCH #define NOMATCH CURL_FNMATCH_NOMATCH
#define RE_ERR CURL_FNMATCH_FAIL
struct testcase { struct testcase {
const char *pattern; const char *pattern;
@ -135,6 +134,8 @@ static const struct testcase tests[] = {
{ "[^[:blank:]]", "\t", NOMATCH }, { "[^[:blank:]]", "\t", NOMATCH },
{ "[^[:print:]]", "\10", MATCH }, { "[^[:print:]]", "\10", MATCH },
{ "[[:lower:]][[:lower:]]", "ll", MATCH }, { "[[:lower:]][[:lower:]]", "ll", MATCH },
{ "[[:foo:]]", "bar", NOMATCH },
{ "[[:foo:]]", "f]", MATCH },
{ "Curl[[:blank:]];-)", "Curl ;-)", MATCH }, { "Curl[[:blank:]];-)", "Curl ;-)", MATCH },
{ "*[[:blank:]]*", " ", MATCH }, { "*[[:blank:]]*", " ", MATCH },
@ -172,7 +173,7 @@ static const struct testcase tests[] = {
{ "x", "", NOMATCH }, { "x", "", NOMATCH },
/* backslash */ /* backslash */
{ "\\", "\\", RE_ERR }, { "\\", "\\", MATCH },
{ "\\\\", "\\", MATCH }, { "\\\\", "\\", MATCH },
{ "\\\\", "\\\\", NOMATCH }, { "\\\\", "\\\\", NOMATCH },
{ "\\?", "?", MATCH }, { "\\?", "?", MATCH },