]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: pattern: do not index too large strings using the tree
authorWilly Tarreau <w@1wt.eu>
Wed, 25 Nov 2026 10:20:55 +0000 (11:20 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 25 Nov 2026 10:33:05 +0000 (11:33 +0100)
In pat_idx_tree_str(), we have to fall back to the list also when
trying to index strings that are larger than the encoded bit depth,
i.e. 4095 bytes including the trailing zero. Anyway at such lengths
there cannot be that many strings (due to memory usage) and lists
will probably not be much more expensive than trees.

This needs to be backported to all versions.

src/pattern.c

index b4b4d13145b78825ad0dc175a005609b10e0db58..6ae657cb746bc3576d771355e19d6966a9febefe 100644 (file)
@@ -1419,15 +1419,17 @@ int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
                return 0;
        }
 
-       /* If the flag PAT_F_IGNORE_CASE is set, we cannot use trees */
-       if (expr->mflags & PAT_MF_IGNORE_CASE)
-               return pat_idx_list_str(expr, pat, err);
+       /* the len was already filled by pat_parse_str() */
+       len = pat->len;
 
-       /* Process the key len */
-       len = strlen(pat->ptr.str) + 1;
+       /* If the flag PAT_F_IGNORE_CASE is set, or if the string is longer
+        * than what ebtrees may index (32767 bits) we cannot use trees.
+        */
+       if (expr->mflags & PAT_MF_IGNORE_CASE || len >= 4095)
+               return pat_idx_list_str(expr, pat, err);
 
        /* node memory allocation */
-       node = calloc(1, sizeof(*node) + len);
+       node = calloc(1, sizeof(*node) + len + 1);
        if (!node) {
                memprintf(err, "out of memory while loading pattern");
                return 0;
@@ -1438,7 +1440,7 @@ int pat_idx_tree_str(struct pattern_expr *expr, struct pattern *pat, char **err)
        node->ref = pat->ref;
 
        /* copy the string */
-       memcpy(node->node.key, pat->ptr.str, len);
+       memcpy(node->node.key, pat->ptr.str, len + 1);
 
        /* index the new node */
        ebst_insert(&expr->pattern_tree, &node->node);