From: Willy Tarreau Date: Sat, 21 Nov 2026 08:04:40 +0000 (+0100) Subject: CLEANUP: pattern: don't even run string-based matches on empty strings X-Git-Url: https://git.kaiwu.me/http/ngx_http_auth_basic_module.html?a=commitdiff_plain;h=ad89b10742fd3df23ca50981aa5d7e47d06f2835;p=haproxy.git CLEANUP: pattern: don't even run string-based matches on empty strings None of our matching patterns supports matching an empty string, so let's just not even try to call them for empty strings. This may even speed up suboptimal configs that unconditionally search for certain things in empty fields. --- diff --git a/src/pattern.c b/src/pattern.c index 1c61b32c8..3cbe7366c 100644 --- a/src/pattern.c +++ b/src/pattern.c @@ -498,6 +498,9 @@ struct pattern *pat_match_str(struct sample *smp, struct pattern_expr *expr, int struct pattern *ret = NULL; struct lru64 *lru = NULL; + if (!smp->data.u.str.data) + return ret; + /* Lookup a string in the expression's pattern tree. */ if (!eb_is_empty(&expr->pattern_tree)) { if (!pat_match_ensure_str(smp)) @@ -566,6 +569,9 @@ struct pattern *pat_match_bin(struct sample *smp, struct pattern_expr *expr, int struct pattern *ret = NULL; struct lru64 *lru = NULL; + if (!smp->data.u.str.data) + return ret; + if (pat_lru_tree && !LIST_ISEMPTY(&expr->patterns) && expr->ref->entry_cnt >= 20) { unsigned long long seed = pat_lru_seed ^ (long)expr; @@ -680,6 +686,9 @@ struct pattern *pat_match_beg(struct sample *smp, struct pattern_expr *expr, int struct pattern *ret = NULL; struct lru64 *lru = NULL; + if (!smp->data.u.str.data) + return ret; + /* Lookup a string in the expression's pattern tree. */ if (!eb_is_empty(&expr->pattern_tree)) { if (!pat_match_ensure_str(smp)) @@ -750,6 +759,9 @@ struct pattern *pat_match_end(struct sample *smp, struct pattern_expr *expr, int struct pattern *ret = NULL; struct lru64 *lru = NULL; + if (!smp->data.u.str.data) + return ret; + if (pat_lru_tree && !LIST_ISEMPTY(&expr->patterns) && expr->ref->entry_cnt >= 20) { unsigned long long seed = pat_lru_seed ^ (long)expr; @@ -798,6 +810,9 @@ struct pattern *pat_match_sub(struct sample *smp, struct pattern_expr *expr, int struct pattern *ret = NULL; struct lru64 *lru = NULL; + if (!smp->data.u.str.data) + return ret; + if (pat_lru_tree && !LIST_ISEMPTY(&expr->patterns) && expr->ref->entry_cnt >= 20) { unsigned long long seed = pat_lru_seed ^ (long)expr; @@ -911,6 +926,9 @@ struct pattern *pat_match_dir(struct sample *smp, struct pattern_expr *expr, int struct pattern_list *lst; struct pattern *pattern; + if (!smp->data.u.str.data) + return NULL; + list_for_each_entry(lst, &expr->patterns, list) { pattern = &lst->pat; @@ -932,6 +950,9 @@ struct pattern *pat_match_dom(struct sample *smp, struct pattern_expr *expr, int struct pattern_list *lst; struct pattern *pattern; + if (!smp->data.u.str.data) + return NULL; + list_for_each_entry(lst, &expr->patterns, list) { pattern = &lst->pat;