From: Willy Tarreau Date: Wed, 25 Nov 2026 10:20:55 +0000 (+0100) Subject: BUG/MINOR: pattern: do not index too large strings using the tree X-Git-Tag: v3.5-dev14~27 X-Git-Url: https://git.kaiwu.me/http/ngx_http_ssi_module.html?a=commitdiff_plain;h=60904f86938435af8d8d129dd1f02606ac074b4e;p=haproxy.git 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. --- 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);