From 2e5a82bd8af77e44afb01b1c366641511fdd8099 Mon Sep 17 00:00:00 2001 From: Sébastien Marie Date: Mon, 22 Jun 2015 16:52:49 +0200 Subject: don't do tail-call-recursive every time - switch '*', '?' and '-' to normal call, in order to pass into "too complex" check - remove assert() - on error, quit early --- httpd/patterns.c | 11 ++++++++--- httpd/patterns.h | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/httpd/patterns.c b/httpd/patterns.c index 1dbea15..177a5af 100644 --- a/httpd/patterns.c +++ b/httpd/patterns.c @@ -427,9 +427,12 @@ match(struct match_state *ms, const char *s, const char *p) if (!singlematch(ms, s, p, ep)) { /* accept empty? */ if (*ep == '*' || *ep == '?' || *ep == '-') { - p = ep + 1; /* return match(ms, s, ep + 1); */ - goto init; + return match(ms, s, ep + 1); + /* + * p = ep + 1; + * goto init; + */ } else { /* '+' or no suffix */ s = NULL; /* fail */ @@ -621,11 +624,13 @@ str_find_aux(struct match_state *ms, const char *pattern, const char *string, do { const char *res; ms->level = 0; - assert(ms->matchdepth == MAXCCALLS); if ((res = match(ms, s1, p)) != NULL) { sm->sm_so = 0; sm->sm_eo = ls; return push_captures(ms, s1, res, sm + 1, nsm - 1) + 1; + + } else if (ms->error != NULL) { + return 0; } } while (s1++ < ms->src_end && !anchor); diff --git a/httpd/patterns.h b/httpd/patterns.h index ddda0dd..28b0f95 100644 --- a/httpd/patterns.h +++ b/httpd/patterns.h @@ -23,7 +23,7 @@ #define PATTERNS_H #define MAXCAPTURES 32 /* Max no. of allowed captures in pattern */ -#define MAXCCALLS 200 /* Max recusion depth in pattern matching */ +#define MAXCCALLS 5000 /* Max recusion depth in pattern matching */ struct str_find { off_t sm_so; /* start offset of match */ -- cgit v1.2.3-54-g00ecf From cb9aff69125a32fd4327cce7079eb18273356d92 Mon Sep 17 00:00:00 2001 From: Sébastien Marie Date: Tue, 23 Jun 2015 10:50:15 +0200 Subject: add '+' to limited matchdepth - revert "normal" call to optimized-tail-call-recursion for '*', '?' and '-' - add a "pattern too complex" test to !singlematch() branch, so that it is limited on call numbers. --- httpd/patterns.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/httpd/patterns.c b/httpd/patterns.c index 177a5af..e6e1b1f 100644 --- a/httpd/patterns.c +++ b/httpd/patterns.c @@ -425,14 +425,16 @@ match(struct match_state *ms, const char *s, const char *p) /* does not match at least once? */ if (!singlematch(ms, s, p, ep)) { + if (ms->matchdepth-- == 0) { + match_error(ms, "pattern too complex"); + s = NULL; /* failed */ + } + /* accept empty? */ if (*ep == '*' || *ep == '?' || *ep == '-') { + p = ep + 1; /* return match(ms, s, ep + 1); */ - return match(ms, s, ep + 1); - /* - * p = ep + 1; - * goto init; - */ + goto init; } else { /* '+' or no suffix */ s = NULL; /* fail */ -- cgit v1.2.3-54-g00ecf