From 60904f86938435af8d8d129dd1f02606ac074b4e Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Wed, 25 Nov 2026 11:20:55 +0100 Subject: [PATCH] BUG/MINOR: pattern: do not index too large strings using the tree 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 | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/pattern.c b/src/pattern.c index b4b4d1314..6ae657cb7 100644 --- a/src/pattern.c +++ b/src/pattern.c @@ -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); -- 2.47.3