From 670b3a395ff6365bc92889d5aafd72e362dc97c2 Mon Sep 17 00:00:00 2001 From: Glenn Strauss Date: Mon, 22 Nov 2021 00:49:48 -0500 Subject: [PATCH] [core] allocate one fewer cond_match_t, if needed allocate one fewer cond_match_t in r->cond_match_data, if any are needed --- src/configfile-glue.c | 6 ++++-- src/configfile.c | 3 --- src/h2.c | 2 +- src/mod_redirect.c | 2 +- src/mod_rewrite.c | 2 +- 5 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/configfile-glue.c b/src/configfile-glue.c index f4fbea41..7602e419 100644 --- a/src/configfile-glue.c +++ b/src/configfile-glue.c @@ -651,8 +651,9 @@ static int config_pcre_match(request_st * const r, const data_config * const dc, return pcre2_match(dc->code, (PCRE2_SPTR)BUF_PTR_LEN(b), 0, 0, dc->match_data, NULL); + const int capture_offset = dc->capture_idx - 1; cond_match_t * const cond_match = - r->cond_match[dc->capture_idx] = r->cond_match_data + dc->capture_idx; + r->cond_match[capture_offset] = r->cond_match_data + capture_offset; pcre2_match_data *match_data = cond_match->match_data; if (__builtin_expect( (NULL == match_data), 0)) { /*(allocate on demand)*/ @@ -689,8 +690,9 @@ static int config_pcre_match(request_st * const r, const data_config * const dc, #ifndef elementsof #define elementsof(x) (sizeof(x) / sizeof(x[0])) #endif + const int capture_offset = dc->capture_idx - 1; cond_match_t * const cond_match = - r->cond_match[dc->capture_idx] = r->cond_match_data + dc->capture_idx; + r->cond_match[capture_offset] = r->cond_match_data + capture_offset; cond_match->comp_value = b; /*holds pointer to b (!) for pattern subst*/ cond_match->captures = pcre_exec(dc->regex, dc->regex_study, BUF_PTR_LEN(b), 0, 0, diff --git a/src/configfile.c b/src/configfile.c index fbc19fb2..5760bb43 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -1233,9 +1233,6 @@ int config_finalize(server *srv, const buffer *default_server_tag) { return 0; } - /* adjust cond_match_data list size if regex config conditions present */ - if (srv->config_captures) ++srv->config_captures; - #ifdef HAVE_PCRE2_H for (uint32_t i = 1; i < srv->config_context->used; ++i) { data_config * const dc = diff --git a/src/h2.c b/src/h2.c index 07b94071..0f8cca7c 100644 --- a/src/h2.c +++ b/src/h2.c @@ -2571,7 +2571,7 @@ h2_init_stream (request_st * const h2r, connection * const con) r->conditional_is_valid = h2r->conditional_is_valid; memcpy(r->cond_cache, h2r->cond_cache, used * sizeof(cond_cache_t)); #ifdef HAVE_PCRE - if (srv->config_captures > 1) + if (srv->config_captures) memcpy(r->cond_match, h2r->cond_match, srv->config_captures * sizeof(cond_match_t)); #endif diff --git a/src/mod_redirect.c b/src/mod_redirect.c index b64a997f..1bdfac5e 100644 --- a/src/mod_redirect.c +++ b/src/mod_redirect.c @@ -169,7 +169,7 @@ URIHANDLER_FUNC(mod_redirect_uri_handler) { ctx.cache = NULL; if (p->conf.redirect->x0) { /*(p->conf.redirect->x0 is capture_idx)*/ - ctx.cache = r->cond_match[p->conf.redirect->x0]; + ctx.cache = r->cond_match[p->conf.redirect->x0 - 1]; } ctx.burl = &burl; burl.scheme = &r->uri.scheme; diff --git a/src/mod_rewrite.c b/src/mod_rewrite.c index 352d8860..b4b50c51 100644 --- a/src/mod_rewrite.c +++ b/src/mod_rewrite.c @@ -291,7 +291,7 @@ static handler_t process_rewrite_rules(request_st * const r, plugin_data *p, con ctx.cache = NULL; if (kvb->x0) { /*(kvb->x0 is capture_idx)*/ - ctx.cache = r->cond_match[kvb->x0]; + ctx.cache = r->cond_match[kvb->x0 - 1]; } ctx.burl = &burl; burl.scheme = &r->uri.scheme;