]> git.kaiwu.me - haproxy.git/commitdiff
CLEANUP: pattern: don't even run string-based matches on empty strings
authorWilly Tarreau <w@1wt.eu>
Sat, 21 Nov 2026 08:04:40 +0000 (09:04 +0100)
committerWilly Tarreau <w@1wt.eu>
Sat, 21 Nov 2026 08:04:40 +0000 (09:04 +0100)
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.

src/pattern.c

index 1c61b32c8144c75b117970c85f5a98fa5235a0e9..3cbe7366cfaa23b50021978934efedfde0d7c483 100644 (file)
@@ -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;