Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix(regex): size a pattern before compiling and refuse oversized ones
Martin Vogel9db7de7bae49
diff --git a/src/cypher/cypher.c b/src/cypher/cypher.cindex 3cc31f9d7..fde4798d1 100644--- a/src/cypher/cypher.c+++ b/src/cypher/cypher.c@@ -2594,6 +2594,12 @@ static const char *resolve_condition_value(const cbm_condition_t *c, binding_t *return n->name ? n->name : "";}+/* Set when an `=~` or inline-property pattern was refused by the regex+ * wrapper's compile-size guard during the CURRENT execution; cbm_cypher_execute+ * turns it into result->warning so an empty result can be told from "no such+ * name". Reset at the start of every execution. */+static _Thread_local bool g_cypher_regex_refused = false;+/* Evaluate a comparison operator between actual and expected strings. */static bool eval_comparison_op(const char *op, const char *actual, const char *expected) {if (strcmp(op, "=") == 0) {@@ -2604,7 +2610,11 @@ static bool eval_comparison_op(const char *op, const char *actual, const char *e}if (strcmp(op, "=~") == 0) {cbm_regex_t re;- if (cbm_regcomp(&re, expected, CBM_REG_EXTENDED | CBM_REG_NOSUB) != 0) {+ int comp_rc = cbm_regcomp(&re, expected, CBM_REG_EXTENDED | CBM_REG_NOSUB);+ if (comp_rc != 0) {+ if (comp_rc == CBM_REG_ETOOBIG) {+ g_cypher_regex_refused = true;+ }return false;}int rc = cbm_regexec(&re, actual, 0, NULL, 0);@@ -2784,13 +2794,17 @@ static bool check_inline_props(const cbm_node_t *n, const cbm_prop_filter_t *proconst char *actual = node_prop(n, props[i].key, store);if (looks_like_regex(props[i].value)) {cbm_regex_t re;- if (cbm_regcomp(&re, props[i].value, CBM_REG_EXTENDED | CBM_REG_NOSUB) == 0) {+ int comp_rc = cbm_regcomp(&re, props[i].value, CBM_REG_EXTENDED | CBM_REG_NOSUB);+ if (comp_rc == 0) {bool matched = cbm_regexec(&re, actual, 0, NULL, 0) == 0;cbm_regfree(&re);if (!matched) {return false;}} else if (strcmp(actual, props[i].value) != 0) {+ if (comp_rc == CBM_REG_ETOOBIG) {+ g_cypher_regex_refused = true;+ }return false;}} else if (strcmp(actual, props[i].value) != 0) {@@ -5447,6 +5461,7 @@ int cbm_cypher_execute(cbm_store_t *store, const char *query, const char *projecg_cypher_depth_clamped = 0;g_cypher_trail_truncated = 0;g_cypher_truncated = false;+ g_cypher_regex_refused = false;cypher_deadline_arm(); /* #601: start the wall-clock budget for this query */if (max_rows <= 0) {max_rows = CYPHER_RESULT_CEILING;@@ -5534,8 +5549,8 @@ int cbm_cypher_execute(cbm_store_t *store, const char *query, const char *projec/* Any internal ceiling that prevented exhaustive evaluation: a candidate or* traversal budget, or a variable-length range clamped to the engine cap. */out->truncated = g_cypher_truncated || g_cypher_trail_truncated != 0;+ char wbuf[CBM_SZ_512] = "";if (g_cypher_depth_clamped > 0 || g_cypher_trail_truncated) {- char wbuf[CBM_SZ_256];if (g_cypher_depth_clamped > 0 && g_cypher_trail_truncated) {snprintf(wbuf, sizeof(wbuf),"variable-length hop range clamped to the engine ceiling (%d) and "@@ -5550,6 +5565,16 @@ int cbm_cypher_execute(cbm_store_t *store, const char *query, const char *projecsnprintf(wbuf, sizeof(wbuf),"variable-length traversal budget was exhausted — results may be partial");}+ }+ /* A refused `=~` or property pattern matched nothing: say so, once per+ * query, next to any traversal warning. */+ if (g_cypher_regex_refused) {+ size_t used = strlen(wbuf);+ snprintf(wbuf + used, sizeof(wbuf) - used,+ "%sa =~ or property " CBM_REG_ETOOBIG_REASON " — the comparison matched nothing",+ used ? "; " : "");+ }+ if (wbuf[0]) {out->warning = heap_strdup(wbuf);}diff --git a/src/foundation/compat_regex.c b/src/foundation/compat_regex.cindex 14bd815fb..f79bd01d2 100644--- a/src/foundation/compat_regex.c+++ b/src/foundation/compat_regex.c@@ -3,12 +3,254 @@** POSIX: direct wrappers around <regex.h>.* Windows: vendored TRE regex library (BSD-licensed).+ *+ * Both backends expand bounded repetition at compile time, so cbm_regcomp+ * sizes a pattern first (the compile-size guard below) and refuses one whose+ * compiled form would be too large, before the platform compiler allocates+ * anything.*/#include "foundation/constants.h"#include "foundation/compat_regex.h"+#include <stdbool.h>+#include <stdint.h>#include <string.h>+/* ── Compile-size guard (shared by both backends) ─────────────────+ *+ * The platform compilers expand `X{m,n}` into n copies of X, so nested+ * intervals multiply: `((((a){20}){20}){20}){20}` is 160,000 expanded atoms+ * from 25 bytes of pattern. A K-way alternation additionally costs about K^2+ * position-set entries on the TRE-derived backends (macOS libc and the vendored+ * TRE), because every union node copies its branches' sets.+ *+ * Measured on macOS libc with REG_EXTENDED|REG_NOSUB: the nested shape costs+ * about 0.6 KB of heap per expanded atom, 1.1 KB with submatch tracking on,+ * 1.7 KB with REG_ICASE and 2.3 KB with both; a K-way alternation adds about+ * 70 B x K^2 on top of its branches (twice that under REG_ICASE), and an+ * alternation under an interval pays that for every copy, about 1 KB per unit+ * with REG_NOSUB and 2.8 KB with REG_ICASE plus submatches. glibc costs about+ * 0.45 KB per atom and expands intervals the same way.+ *+ * cbm_regcomp_estimate_units() counts expanded atoms in one linear pass with+ * saturating arithmetic and charges a K-way alternation K^2/8 units, the+ * measured ratio between the two costs. With CBM_REGEX_COMPILE_BUDGET_UNITS =+ * 32768 the heaviest accepted compile is a repeated alternation at about 30 MB+ * with REG_NOSUB and about 90 MB under the ICASE-plus-submatch flag set no+ * call site uses; the nested shape tops out at 13^4 = 28,561 units, about+ * 16 MB. `(\w{1,100}){1,20}` (2,000 units) and an alternation of 500 short+ * names (31,250 units plus its letters) remain accepted. */++enum { REGEX_GROUP_DEPTH_MAX = 64 };++typedef struct {+ uint64_t total; /* units of this group so far; includes `last` */+ uint64_t last; /* units of the element a following quantifier applies to */+ uint64_t branches; /* alternatives in this group: 1 + the number of '|' */+} regex_frame_t;++static uint64_t regex_sat_add(uint64_t a, uint64_t b) {+ return a > UINT64_MAX - b ? UINT64_MAX : a + b;+}++static uint64_t regex_sat_mul(uint64_t a, uint64_t b) {+ if (a == 0 || b == 0) {+ return 0;+ }+ return a > UINT64_MAX / b ? UINT64_MAX : a * b;+}++static void regex_frame_atom(regex_frame_t *f, uint64_t units) {+ f->total = regex_sat_add(f->total, units);+ f->last = units;+}++/* An interval applies to the last element: n copies of it replace the one. */+static void regex_frame_repeat_last(regex_frame_t *f, uint64_t copies) {+ uint64_t grown = regex_sat_mul(f->last, copies);+ f->total = regex_sat_add(f->total - f->last, grown);+ f->last = grown;+}++/* Units a closed group contributes to its parent: its content plus the K^2/8+ * alternation share, never less than one atom. */+static uint64_t regex_frame_units(const regex_frame_t *f) {+ uint64_t alternation = regex_sat_mul(f->branches, f->branches) / 8;+ uint64_t units = regex_sat_add(f->total, alternation);+ return units ? units : 1;+}++/* Skip the bracket expression opening at p[i] == '['. Returns the index just+ * past its closing ']', or the end of the string when it is unterminated.+ * Nothing inside is an operator: `[]a]`, `[^]a]` and `[[:alpha:]]` are each+ * one atom, and a brace inside brackets never opens an interval. */+static size_t regex_skip_bracket(const char *p, size_t i) {+ size_t j = i + 1;+ if (p[j] == '^') {+ j++;+ }+ if (p[j] == ']') {+ j++;+ }+ while (p[j] && p[j] != ']') {+ if (p[j] == '[' && (p[j + 1] == ':' || p[j + 1] == '=' || p[j + 1] == '.')) {+ char close = p[j + 1];+ size_t k = j + 2;+ while (p[k] && !(p[k] == close && p[k + 1] == ']')) {+ k++;+ }+ j = p[k] ? k + 2 : k;+ } else {+ j++;+ }+ }+ return p[j] ? j + 1 : j;+}++static bool regex_parse_digits(const char *p, size_t *i, uint64_t *value) {+ bool any = false;+ *value = 0;+ while (p[*i] >= '0' && p[*i] <= '9') {+ *value = regex_sat_add(regex_sat_mul(*value, 10), (uint64_t)(p[*i] - '0'));+ (*i)++;+ any = true;+ }+ return any;+}++/* Parse the interval body that starts at p[i], just past the opening brace+ * (`{` in extended syntax, `\{` in basic). On success stores the number of+ * copies the compiler will make (the upper bound, or the lower bound for+ * `{m,}`, at least 1) and the index just past the closing brace. Anything that+ * is not an interval leaves the brace to be counted as a literal, which is what+ * the backends that accept it do; the others reject the pattern. */+static bool regex_parse_interval(const char *p, size_t i, bool extended, uint64_t *copies,+ size_t *end) {+ uint64_t lower = 0;+ uint64_t upper = 0;+ bool has_lower = regex_parse_digits(p, &i, &lower);+ bool has_upper = false;+ if (p[i] == ',') {+ i++;+ has_upper = regex_parse_digits(p, &i, &upper);+ } else {+ if (!has_lower) {+ return false;+ }+ upper = lower;+ has_upper = true;+ }+ if (extended) {+ if (p[i] != '}') {+ return false;+ }+ i++;+ } else {+ if (p[i] != '\\' || p[i + 1] != '}') {+ return false;+ }+ i += 2;+ }+ uint64_t n = has_upper ? upper : lower;+ if (has_upper && lower > upper) {+ n = lower; /* `{5,3}` is rejected by the backends; count the larger bound */+ }+ *copies = n ? n : 1;+ *end = i;+ return true;+}++uint64_t cbm_regcomp_estimate_units(const char *pattern, int flags) {+ const bool extended = (flags & CBM_REG_EXTENDED) != 0;+ regex_frame_t frames[REGEX_GROUP_DEPTH_MAX];+ int depth = 0;+ frames[0] = (regex_frame_t){0, 0, 1};++ size_t i = 0;+ while (pattern[i]) {+ char c = pattern[i];+ if (c == '[') {+ i = regex_skip_bracket(pattern, i);+ regex_frame_atom(&frames[depth], 1);+ continue;+ }+ /* In extended syntax the bare characters operate and `\x` is one+ * literal; in basic syntax only `\(`, `\)`, `\{`, `\|`, `\+`, `\?` and+ * a bare `*` operate. */+ char op = 0;+ size_t len = 1;+ if (c == '\\') {+ len = pattern[i + 1] ? 2 : 1;+ if (!extended && len == 2 && strchr("(){|+?", pattern[i + 1])) {+ op = pattern[i + 1];+ }+ } else if (extended ? strchr("(){|*+?", c) != NULL : c == '*') {+ op = c;+ }+ regex_frame_t *f = &frames[depth];+ switch (op) {+ case '(':+ if (depth + 1 >= REGEX_GROUP_DEPTH_MAX) {+ return UINT64_MAX;+ }+ frames[++depth] = (regex_frame_t){0, 0, 1};+ break;+ case ')':+ if (depth == 0) {+ regex_frame_atom(f, 1); /* unmatched: a literal where accepted */+ break;+ }+ depth--;+ regex_frame_atom(&frames[depth], regex_frame_units(f));+ break;+ case '|':+ f->branches = regex_sat_add(f->branches, 1);+ f->last = 0;+ break;+ case '{': {+ uint64_t copies = 0;+ size_t end = 0;+ if (regex_parse_interval(pattern, i + len, extended, &copies, &end)) {+ regex_frame_repeat_last(f, copies);+ i = end;+ continue;+ }+ regex_frame_atom(f, 1);+ break;+ }+ case '*':+ case '+':+ case '?':+ break; /* unbounded or optional repetition is not expanded */+ default:+ regex_frame_atom(f, 1);+ break;+ }+ i += len;+ }+ while (depth > 0) {+ regex_frame_t *f = &frames[depth];+ depth--;+ regex_frame_atom(&frames[depth], regex_frame_units(f));+ }+ return regex_frame_units(&frames[0]);+}++/* Refuse a pattern before the platform compiler sees it. */+static int regex_compile_guard(const char *pattern, int flags) {+ size_t len = 0;+ while (len <= (size_t)CBM_REGEX_PATTERN_MAX_BYTES && pattern[len]) {+ len++;+ }+ if (len > (size_t)CBM_REGEX_PATTERN_MAX_BYTES) {+ return CBM_REG_ETOOBIG;+ }+ if (cbm_regcomp_estimate_units(pattern, flags) > (uint64_t)CBM_REGEX_COMPILE_BUDGET_UNITS) {+ return CBM_REG_ETOOBIG;+ }+ return CBM_REG_OK;+}+#ifdef _WIN32/* ── Windows: use vendored TRE regex ─────────────────────────── */@@ -31,6 +273,10 @@ static int translate_flags_tre(int flags) {}int cbm_regcomp(cbm_regex_t *r, const char *pattern, int flags) {+ int refused = regex_compile_guard(pattern, flags);+ if (refused != CBM_REG_OK) {+ return refused;+ }regex_t *re = (regex_t *)r->opaque;int rc = tre_regcomp(re, pattern, translate_flags_tre(flags));return rc == 0 ? CBM_REG_OK : rc;@@ -88,6 +334,10 @@ static int translate_flags(int flags) {}int cbm_regcomp(cbm_regex_t *r, const char *pattern, int flags) {+ int refused = regex_compile_guard(pattern, flags);+ if (refused != CBM_REG_OK) {+ return refused;+ }regex_t *re = (regex_t *)r->opaque;int rc = regcomp(re, pattern, translate_flags(flags));return rc == 0 ? CBM_REG_OK : rc;diff --git a/src/foundation/compat_regex.h b/src/foundation/compat_regex.hindex c57245967..7545b2571 100644--- a/src/foundation/compat_regex.h+++ b/src/foundation/compat_regex.h@@ -11,6 +11,7 @@#include "foundation/constants.h"#include <stddef.h>+#include <stdint.h>/* ── Flags ────────────────────────────────────────────────────── */@@ -23,6 +24,29 @@#define CBM_REG_OK 0#define CBM_REG_NOMATCH (-1)+/* cbm_regcomp refused the pattern before compiling it: longer than+ * CBM_REGEX_PATTERN_MAX_BYTES, or its compiled form would exceed+ * CBM_REGEX_COMPILE_BUDGET_UNITS (see cbm_regcomp_estimate_units). Well above+ * every platform compile error code, which are small positive integers. */+#define CBM_REG_ETOOBIG 100+/* Reason text for a CBM_REG_ETOOBIG refusal, for user-facing error messages. */+#define CBM_REG_ETOOBIG_REASON \+ "regex pattern too large to compile: reduce nested {m,n} repetition or alternation"++/* ── Compile-size guard ───────────────────────────────────────── */++enum {+ /* Longest pattern cbm_regcomp hands to the platform compiler. Bounds only+ * the sizing pass: a pattern without repetition costs one unit per atom,+ * so the unit budget below already bounds its compiled size. Equal to the+ * budget, so a realistic pattern is only ever refused for its cost. */+ CBM_REGEX_PATTERN_MAX_BYTES = CBM_SZ_32K,+ /* Largest estimated compiled size, in units of one expanded atom, that+ * cbm_regcomp accepts. Bounds what bounded repetition and alternation can+ * expand a pattern into. compat_regex.c documents the measured bytes per+ * unit and what the budget admits. */+ CBM_REGEX_COMPILE_BUDGET_UNITS = CBM_SZ_32K+};/* ── Types ────────────────────────────────────────────────────── */@@ -40,9 +64,21 @@ typedef struct {/* ── Functions ────────────────────────────────────────────────── */-/* Compile a regular expression. Returns CBM_REG_OK on success, non-zero on error. */+/* Compile a regular expression. Returns CBM_REG_OK on success, non-zero on error:+ * CBM_REG_ETOOBIG when the pattern is refused before compilation (nothing to+ * free), otherwise the platform compiler's error code. */int cbm_regcomp(cbm_regex_t *r, const char *pattern, int flags);+/* Estimated size of the compiled form of pattern, in units of one expanded+ * atom: an atom costs 1, concatenation and alternation add, a group costs its+ * content, and an interval `{m}`, `{m,}`, `{m,n}` multiplies the element it+ * applies to by the number of copies the compiler makes (the upper bound; m for+ * `{m,}`; at least 1), so nested intervals multiply. `*`, `+` and `?` do not+ * multiply. A K-way alternation adds K*K/8 for its position sets. Arithmetic+ * saturates at UINT64_MAX; a group nesting deeper than the pass tracks returns+ * UINT64_MAX. Honors CBM_REG_EXTENDED (basic syntax otherwise). */+uint64_t cbm_regcomp_estimate_units(const char *pattern, int flags);+/* Execute compiled regex against str. nmatch/matches may be 0/NULL.* eflags: 0 or combination of platform-specific exec flags.* Returns CBM_REG_OK on match, CBM_REG_NOMATCH on no match. */diff --git a/src/mcp/mcp.c b/src/mcp/mcp.cindex c877d5110..f6306077c 100644--- a/src/mcp/mcp.c+++ b/src/mcp/mcp.c@@ -5336,8 +5336,11 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {max_degree != CBM_NOT_FOUND;bool semantic_only = sq_present && !has_filters;cbm_search_output_t out = {0};- if (!semantic_only) {- (void)cbm_store_search(store, ¶ms, &out);+ char search_error[CBM_SZ_512] = "";+ if (!semantic_only && cbm_store_search(store, ¶ms, &out) != CBM_STORE_OK) {+ /* A refused or invalid regex aborts the row scan inside SQLite; the+ * store's reason is the caller's only hint about the pattern. */+ snprintf(search_error, sizeof(search_error), "search_graph: %s", cbm_store_error(store));}const char *diagnostic_hint = NULL;@@ -5420,7 +5423,9 @@ static char *handle_search_graph(cbm_mcp_server_t *srv, const char *args) {free(file_pattern);free(relationship);- char *result = cbm_mcp_text_result(payload ? payload : "out of memory", payload == NULL);+ char *result = search_error[0]+ ? cbm_mcp_text_result(search_error, true)+ : cbm_mcp_text_result(payload ? payload : "out of memory", payload == NULL);free(payload);return result;}@@ -14821,12 +14826,16 @@ static bool search_scratch_open(search_scratch_t *scratch, const char *pattern)return true;}-/* Compile a path filter regex. Returns true if compiled successfully. */-static bool compile_path_filter(const char *filter, cbm_regex_t *re) {+/* Compile a path filter regex. Returns true when *re holds the compiled filter.+ * An absent or empty filter compiles nothing and reports CBM_REG_OK in *rc; a+ * filter the regex wrapper refuses or cannot compile reports its error there. */+static bool compile_path_filter(const char *filter, cbm_regex_t *re, int *rc) {+ *rc = CBM_REG_OK;if (!filter || !filter[0]) {return false;}- return cbm_regcomp(re, filter, CBM_REG_EXTENDED | CBM_REG_NOSUB) == CBM_REG_OK;+ *rc = cbm_regcomp(re, filter, CBM_REG_EXTENDED | CBM_REG_NOSUB);+ return *rc == CBM_REG_OK;}static mcp_scan_cause_t mcp_run_shell_command_cancellable_bounded(@@ -14979,7 +14988,8 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {size_t byte_budget = (size_t)max_output_tokens * (size_t)MCP_OUTPUT_BYTES_PER_TOKEN_ESTIMATE;cbm_regex_t path_regex;- bool has_path_filter = compile_path_filter(path_filter, &path_regex);+ int path_filter_rc = CBM_REG_OK; /* reported with the regex probe below */+ bool has_path_filter = compile_path_filter(path_filter, &path_regex, &path_filter_rc);free(path_filter);path_filter = NULL;@@ -15025,21 +15035,35 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {* unclosed group) makes the underlying grep fail, which the handler would* otherwise report as an empty result set — indistinguishable from a* legitimate no-match. Validate the user's regex up front and return an- * explicit error so callers can tell "broken pattern" from "no matches". */- if (use_regex) {+ * explicit error so callers can tell "broken pattern" from "no matches".+ * A path_filter the regex wrapper refused or could not compile is reported+ * the same way instead of silently searching unfiltered. */+ const char *regex_error = NULL;+ if (path_filter_rc != CBM_REG_OK) {+ regex_error = path_filter_rc == CBM_REG_ETOOBIG+ ? "path_filter: " CBM_REG_ETOOBIG_REASON+ : "invalid path_filter regex: check for unbalanced (), [], or {}";+ } else if (use_regex) {cbm_regex_t probe;- if (cbm_regcomp(&probe, pattern, CBM_REG_EXTENDED | CBM_REG_NOSUB) != CBM_REG_OK) {- if (has_path_filter) {- cbm_regfree(&path_regex);- }- free(root_path);- free(pattern);- free(project);- free(file_pattern);- return cbm_mcp_text_result(- "invalid regex pattern (regex=true): check for unbalanced (), [], or {}", true);+ int probe_rc = cbm_regcomp(&probe, pattern, CBM_REG_EXTENDED | CBM_REG_NOSUB);+ if (probe_rc != CBM_REG_OK) {+ regex_error =+ probe_rc == CBM_REG_ETOOBIG+ ? CBM_REG_ETOOBIG_REASON " (regex=true)"+ : "invalid regex pattern (regex=true): check for unbalanced (), [], or {}";+ } else {+ cbm_regfree(&probe);+ }+ }+ if (regex_error) {+ if (has_path_filter) {+ cbm_regfree(&path_regex);}- cbm_regfree(&probe);+ free(root_path);+ free(pattern);+ free(project);+ free(file_pattern);+ return cbm_mcp_text_result(regex_error, true);}/* ── Phase 0.5: Multi-word → regex conversion ───────────── */diff --git a/src/store/store.c b/src/store/store.cindex d66ef0ffb..449dad5cf 100644--- a/src/store/store.c+++ b/src/store/store.c@@ -719,9 +719,12 @@ static void sqlite_regexp(sqlite3_context *ctx, int argc, sqlite3_value **argv)sqlite3_result_error_nomem(ctx);return;}- if (cbm_regcomp(re, pattern, CBM_REG_EXTENDED | CBM_REG_NOSUB) != 0) {+ int rc = cbm_regcomp(re, pattern, CBM_REG_EXTENDED | CBM_REG_NOSUB);+ if (rc != 0) {free(re);- sqlite3_result_error(ctx, "invalid regex", CBM_NOT_FOUND);+ sqlite3_result_error(ctx,+ rc == CBM_REG_ETOOBIG ? CBM_REG_ETOOBIG_REASON : "invalid regex",+ CBM_NOT_FOUND);return;}sqlite3_set_auxdata(ctx, 0, re, regex_free_cb);@@ -747,9 +750,12 @@ static void sqlite_iregexp(sqlite3_context *ctx, int argc, sqlite3_value **argv)sqlite3_result_error_nomem(ctx);return;}- if (cbm_regcomp(re, pattern, CBM_REG_EXTENDED | CBM_REG_NOSUB | CBM_REG_ICASE) != 0) {+ int rc = cbm_regcomp(re, pattern, CBM_REG_EXTENDED | CBM_REG_NOSUB | CBM_REG_ICASE);+ if (rc != 0) {free(re);- sqlite3_result_error(ctx, "invalid regex", CBM_NOT_FOUND);+ sqlite3_result_error(ctx,+ rc == CBM_REG_ETOOBIG ? CBM_REG_ETOOBIG_REASON : "invalid regex",+ CBM_NOT_FOUND);return;}sqlite3_set_auxdata(ctx, 0, re, regex_free_cb);diff --git a/tests/test_cypher.c b/tests/test_cypher.cindex abd334683..9bce9043a 100644--- a/tests/test_cypher.c+++ b/tests/test_cypher.c@@ -1442,6 +1442,43 @@ TEST(cypher_exec_where_regex) {PASS();}+/* An `=~` pattern over the regex wrapper's compile-size budget is refused+ * before the platform compiler expands it; the comparison then matches nothing,+ * as for any pattern that cannot be compiled, the result carries a warning+ * naming the reason, and the engine keeps answering. The pattern is a 509-way+ * alternation whose first branch is `.`: once compiled it matches every name,+ * and it costs 509 + 509*509/8 = 32,894 units, just over the budget. */+TEST(cypher_exec_where_regex_oversized_pattern_matches_nothing) {+ cbm_store_t *s = setup_cypher_store();+ cbm_cypher_result_t r = {0};++ char query[1200];+ char *w = query;+ w += snprintf(w, sizeof(query), "MATCH (f:Function) WHERE f.name =~ \"(.");+ for (int i = 0; i < 508; i++) {+ *w++ = '|';+ *w++ = (char)('a' + i % 26);+ }+ snprintf(w, (size_t)(query + sizeof(query) - w), ")\"");++ int rc = cbm_cypher_execute(s, query, "test", 0, &r);+ ASSERT_EQ(rc, 0);+ ASSERT_EQ(r.row_count, 0);+ ASSERT_NOT_NULL(r.warning);+ ASSERT_NOT_NULL(strstr(r.warning, "too large to compile"));+ cbm_cypher_result_free(&r);++ cbm_cypher_result_t r2 = {0};+ rc = cbm_cypher_execute(s, "MATCH (f:Function) WHERE f.name =~ \".*Order.*\"", "test", 0, &r2);+ ASSERT_EQ(rc, 0);+ ASSERT_EQ(r2.row_count, 3);+ ASSERT_NULL(r2.warning);+ cbm_cypher_result_free(&r2);++ cbm_store_close(s);+ PASS();+}+TEST(cypher_exec_where_contains) {cbm_store_t *s = setup_cypher_store();cbm_cypher_result_t r = {0};@@ -4928,6 +4965,7 @@ SUITE(cypher) {RUN_TEST(cypher_exec_varlength_path_semantics_issue797);RUN_TEST(cypher_exec_where_coalesce_issue874);RUN_TEST(cypher_exec_where_regex);+ RUN_TEST(cypher_exec_where_regex_oversized_pattern_matches_nothing);RUN_TEST(cypher_exec_where_contains);RUN_TEST(cypher_exec_where_starts_with);RUN_TEST(cypher_exec_return_properties);diff --git a/tests/test_mcp.c b/tests/test_mcp.cindex 22af972af..dcc2d4d16 100644--- a/tests/test_mcp.c+++ b/tests/test_mcp.c@@ -10992,6 +10992,87 @@ TEST(search_code_path_filter_matches_nothing) {PASS();}+/* An over-budget regex is refused by the wrapper before the platform compiler+ * expands it. search_code reports that as a tool error naming the argument and+ * the reason, for path_filter and for a regex=true pattern alike, and keeps+ * answering afterwards. The pattern is 14^4 = 38,416 expanded atoms: over the+ * 32,768-unit budget, and tens of MB to hold even when unguarded. */+TEST(search_code_oversized_path_filter_is_tool_error) {+ char tmp[512], src_path[768], vendor_path[768];+ cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),+ vendor_path, sizeof(vendor_path));+ ASSERT_NOT_NULL(srv);++ char *resp = cbm_mcp_server_handle(+ srv, "{\"jsonrpc\":\"2.0\",\"id\":97,\"method\":\"tools/call\","+ "\"params\":{\"name\":\"search_code\","+ "\"arguments\":{\"pattern\":\"HandleRequest\",\"project\":\"prefilter-search\","+ "\"path_filter\":\"((((a){14}){14}){14}){14}\"}}}");+ ASSERT_NOT_NULL(resp);+ ASSERT_NOT_NULL(strstr(resp, "\"isError\":true"));+ ASSERT_NOT_NULL(strstr(resp, "path_filter"));+ ASSERT_NOT_NULL(strstr(resp, "too large to compile"));+ free(resp);++ resp = cbm_mcp_server_handle(+ srv, "{\"jsonrpc\":\"2.0\",\"id\":98,\"method\":\"tools/call\","+ "\"params\":{\"name\":\"search_code\","+ "\"arguments\":{\"pattern\":\"((((a){14}){14}){14}){14}\",\"regex\":true,"+ "\"project\":\"prefilter-search\"}}}");+ ASSERT_NOT_NULL(resp);+ ASSERT_NOT_NULL(strstr(resp, "\"isError\":true"));+ ASSERT_NOT_NULL(strstr(resp, "too large to compile"));+ ASSERT_NOT_NULL(strstr(resp, "regex=true"));+ free(resp);++ /* The server still answers an ordinary filtered call. */+ resp = cbm_mcp_server_handle(+ srv, "{\"jsonrpc\":\"2.0\",\"id\":99,\"method\":\"tools/call\","+ "\"params\":{\"name\":\"search_code\","+ "\"arguments\":{\"pattern\":\"HandleRequest\",\"project\":\"prefilter-search\","+ "\"path_filter\":\"^src/\"}}}");+ ASSERT_NOT_NULL(resp);+ ASSERT_TRUE(strstr(resp, "\"isError\":true") == NULL);+ char *inner = extract_text_content(resp);+ ASSERT_NOT_NULL(inner);+ ASSERT_NOT_NULL(strstr(inner, "src/handler.go"));+ free(inner);+ free(resp);+ cbm_mcp_server_free(srv);+ cleanup_prefilter_dir(tmp, src_path, vendor_path);+ PASS();+}++/* search_graph compiles name_pattern inside SQLite's REGEXP function. An+ * over-budget pattern is refused there, the row scan aborts, and the tool+ * reports the reason as a tool error instead of an empty result. The next call+ * answers normally. */+TEST(search_graph_oversized_name_pattern_is_tool_error) {+ char tmp[512], src_path[768], vendor_path[768];+ cbm_mcp_server_t *srv = setup_prefilter_server(tmp, sizeof(tmp), src_path, sizeof(src_path),+ vendor_path, sizeof(vendor_path));+ ASSERT_NOT_NULL(srv);++ char *resp = cbm_mcp_handle_tool(+ srv, "search_graph",+ "{\"project\":\"prefilter-search\",\"name_pattern\":\"((((a){14}){14}){14}){14}\"}");+ ASSERT_NOT_NULL(resp);+ ASSERT_NOT_NULL(strstr(resp, "\"isError\":true"));+ ASSERT_NOT_NULL(strstr(resp, "too large to compile"));+ free(resp);++ resp = cbm_mcp_handle_tool(+ srv, "search_graph",+ "{\"project\":\"prefilter-search\",\"name_pattern\":\"^HandleRequest$\"}");+ ASSERT_NOT_NULL(resp);+ ASSERT_TRUE(strstr(resp, "\"isError\":true") == NULL);+ ASSERT_NOT_NULL(strstr(resp, "HandleRequest"));+ free(resp);+ cbm_mcp_server_free(srv);+ cleanup_prefilter_dir(tmp, src_path, vendor_path);+ PASS();+}+TEST(search_code_file_pattern_prefilter_boundaries) {ASSERT_TRUE(cbm_search_code_file_pattern_can_prefilter("*.pas"));ASSERT_TRUE(cbm_search_code_file_pattern_can_prefilter("*.PAS"));@@ -21230,6 +21311,8 @@ SUITE(mcp) {RUN_TEST(search_code_path_filter_prefilter_keeps_matches);RUN_TEST(search_code_long_line_does_not_invent_matches);RUN_TEST(search_code_path_filter_matches_nothing);+ RUN_TEST(search_code_oversized_path_filter_is_tool_error);+ RUN_TEST(search_graph_oversized_name_pattern_is_tool_error);RUN_TEST(search_code_file_pattern_prefilter_boundaries);RUN_TEST(search_code_windows_scope_prefilter_removes_pipeline_filter);RUN_TEST(search_code_cancel_cleans_supervised_scan);diff --git a/tests/test_platform.c b/tests/test_platform.cindex 16cc3b5d3..7545ea6c5 100644--- a/tests/test_platform.c+++ b/tests/test_platform.c@@ -4,6 +4,7 @@#include "test_framework.h"#include "../src/foundation/compat.h" /* cbm_setenv / cbm_unsetenv (Windows-portable) */#include "../src/foundation/compat_fs.h"+#include "../src/foundation/compat_regex.h"#include "../src/foundation/constants.h"#include "../src/foundation/compat_thread.h"#include "../src/foundation/platform.h"@@ -1158,6 +1159,234 @@ TEST(cgroup_no_mem_files) {#endif /* __linux__ */+/* ── compat_regex: compile-size guard ──────────────────────────── */++static uint64_t regex_units_ere(const char *pattern) {+ return cbm_regcomp_estimate_units(pattern, CBM_REG_EXTENDED);+}++static uint64_t regex_units_bre(const char *pattern) {+ return cbm_regcomp_estimate_units(pattern, 0);+}++/* Writes "(a|b|c|...)" with `branches` one-letter alternatives into buf, which+ * must hold 2 * branches + 2 bytes. */+static void regex_one_letter_alternation(char *buf, int branches) {+ char *w = buf;+ *w++ = '(';+ for (int i = 0; i < branches; i++) {+ if (i) {+ *w++ = '|';+ }+ *w++ = (char)('a' + i % 26);+ }+ *w++ = ')';+ *w = '\0';+}++/* Both regex backends expand `X{m,n}` into n copies of X at compile time, so+ * nested intervals multiply and a 25-byte pattern can ask for millions of+ * atoms. The wrapper sizes the pattern first and refuses one over the budget+ * with CBM_REG_ETOOBIG before the platform compiler allocates anything, so+ * there is nothing to free on refusal. The over-budget shapes here are small+ * enough that an unguarded compile would still only cost tens of MB. */+TEST(regex_compile_refuses_oversized_expansion) {+ cbm_regex_t re;+ /* 14^4 = 38,416 expanded atoms from 25 bytes: over the 32,768-unit budget. */+ const char *nested = "((((a){14}){14}){14}){14}";+ ASSERT_EQ(cbm_regcomp(&re, nested, CBM_REG_EXTENDED | CBM_REG_NOSUB), CBM_REG_ETOOBIG);+ /* Every flag set the call sites use is refused the same way. */+ ASSERT_EQ(cbm_regcomp(&re, nested, CBM_REG_EXTENDED), CBM_REG_ETOOBIG);+ ASSERT_EQ(cbm_regcomp(&re, nested, CBM_REG_EXTENDED | CBM_REG_NOSUB | CBM_REG_ICASE),+ CBM_REG_ETOOBIG);+ /* A repeated alternation multiplies too: (16 + 16*16/8) x 255 x 3 = 36,720. */+ ASSERT_EQ(cbm_regcomp(&re, "((a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p){255}){3}",+ CBM_REG_EXTENDED | CBM_REG_NOSUB),+ CBM_REG_ETOOBIG);+ /* A plain alternation just over the budget: 509 one-letter branches cost+ * 509 + 509*509/8 = 32,894 units; 508 cost 32,766 and stay accepted. */+ char alts[2 * 509 + 2];+ regex_one_letter_alternation(alts, 509);+ ASSERT_EQ(regex_units_ere(alts), 32894);+ ASSERT_EQ(cbm_regcomp(&re, alts, CBM_REG_EXTENDED | CBM_REG_NOSUB), CBM_REG_ETOOBIG);+ regex_one_letter_alternation(alts, 508);+ ASSERT_EQ(regex_units_ere(alts), 32766);+ ASSERT_LTE(regex_units_ere(alts), (uint64_t)CBM_REGEX_COMPILE_BUDGET_UNITS);+ /* Bounds too large to represent saturate instead of wrapping around. */+ ASSERT_EQ(cbm_regcomp(&re, "(a{99999999999999999999}){99999999999999999999}",+ CBM_REG_EXTENDED | CBM_REG_NOSUB),+ CBM_REG_ETOOBIG);+ /* Nesting deeper than the sizing pass tracks is refused, not recursed into. */+ char deep[2 * 70 + 2];+ memset(deep, '(', 70);+ deep[70] = 'a';+ memset(deep + 71, ')', 70);+ deep[141] = '\0';+ ASSERT_EQ(cbm_regcomp(&re, deep, CBM_REG_EXTENDED | CBM_REG_NOSUB), CBM_REG_ETOOBIG);+ PASS();+}++/* The same nested shape one step under the budget (13^4 = 28,561 units)+ * compiles and matches, so the refusal starts only past the budget. */+TEST(regex_compile_accepts_expansion_under_budget) {+ cbm_regex_t re;+ ASSERT_EQ(regex_units_ere("((((a){13}){13}){13}){13}"), 28561);+ ASSERT_EQ(cbm_regcomp(&re, "((((a){13}){13}){13}){13}", CBM_REG_EXTENDED | CBM_REG_NOSUB),+ CBM_REG_OK);+ char *subject = malloc(28561 + 1);+ ASSERT_NOT_NULL(subject);+ memset(subject, 'a', 28561);+ subject[28561] = '\0';+ ASSERT_EQ(cbm_regexec(&re, subject, 0, NULL, 0), CBM_REG_OK);+ subject[28560] = '\0'; /* 28,560 a's: one short of 13^4 */+ ASSERT_EQ(cbm_regexec(&re, subject, 0, NULL, 0), CBM_REG_NOMATCH);+ free(subject);+ cbm_regfree(&re);+ PASS();+}++/* A pattern longer than CBM_REGEX_PATTERN_MAX_BYTES is refused outright; one+ * exactly at the cap still compiles (a literal at the cap is exactly the unit+ * budget, which is accepted). */+TEST(regex_compile_refuses_overlong_pattern) {+ cbm_regex_t re;+ char *pattern = malloc(CBM_REGEX_PATTERN_MAX_BYTES + 2);+ ASSERT_NOT_NULL(pattern);+ memset(pattern, 'a', CBM_REGEX_PATTERN_MAX_BYTES + 1);+ pattern[CBM_REGEX_PATTERN_MAX_BYTES + 1] = '\0';+ ASSERT_EQ(cbm_regcomp(&re, pattern, CBM_REG_EXTENDED | CBM_REG_NOSUB), CBM_REG_ETOOBIG);+ pattern[CBM_REGEX_PATTERN_MAX_BYTES] = '\0';+ ASSERT_EQ(cbm_regcomp(&re, pattern, CBM_REG_EXTENDED | CBM_REG_NOSUB), CBM_REG_OK);+ cbm_regfree(&re);+ free(pattern);+ PASS();+}++/* Ordinary intervals, the pipeline's fixed patterns and patterns from the+ * tool-level tests stay well inside the budget. */+TEST(regex_compile_accepts_ordinary_intervals) {+ static const char *const patterns[] = {+ "[a-z]{1,64}",+ ".{0,255}",+ "(\\w{1,100}){1,20}",+ "(foo|bar|baz){2,5}",+ "^[A-Za-z_][A-Za-z0-9_]{0,255}$",+ "[0-9]{4}-[0-9]{2}-[0-9]{2}",+ /* pass_calls.c and pass_envscan.c */+ "Depends\\(([A-Za-z_][A-Za-z0-9_.]*)",+ "^(ENV|ARG)[[:space:]]+([A-Za-z0-9_]+)[= ](.*)",+ "(default|value)[[:space:]]*=[[:space:]]*\"(https?://[^\"]+)\"",+ "(export[[:space:]]+)?([A-Za-z0-9_]+)=[\"']?(https?://[^ \t\"']+)",+ /* test_mcp.c and test_cypher.c */+ ".*Order.*",+ "^src/",+ "^no_such_dir/",+ "/api/.*",+ "^design/",+ };+ cbm_regex_t re;+ for (size_t i = 0; i < sizeof(patterns) / sizeof(patterns[0]); i++) {+ int rc = cbm_regcomp(&re, patterns[i], CBM_REG_EXTENDED | CBM_REG_NOSUB);+ if (rc != CBM_REG_OK) {+ printf(" pattern %zu refused with rc=%d: %s\n", i, rc, patterns[i]);+ }+ ASSERT_EQ(rc, CBM_REG_OK);+ cbm_regfree(&re);+ }++ /* A long alternation of identifiers, as a search for "any of these names"+ * sends it: 400 names of up to nine characters is 3,761 bytes and about+ * 23,400 units (20,000 of them the alternation share). It stays under both+ * caps and compiles under the search_graph flag set. */+ static const char *const prefixes[] = {"parse", "emit", "visit", "bind", "scan"};+ char names[CBM_SZ_4K];+ char *w = names;+ *w++ = '(';+ for (int i = 0; i < 400; i++) {+ w += snprintf(w, (size_t)(names + sizeof(names) - w), "%s%s_%03d", i ? "|" : "",+ prefixes[i % 5], i);+ }+ *w++ = ')';+ *w = '\0';+ ASSERT_LTE(strlen(names), (size_t)CBM_REGEX_PATTERN_MAX_BYTES);+ ASSERT_GTE(regex_units_ere(names), (uint64_t)20000);+ ASSERT_LTE(regex_units_ere(names), (uint64_t)CBM_REGEX_COMPILE_BUDGET_UNITS);+ ASSERT_EQ(cbm_regcomp(&re, names, CBM_REG_EXTENDED | CBM_REG_NOSUB | CBM_REG_ICASE),+ CBM_REG_OK);+ ASSERT_EQ(cbm_regexec(&re, "Bind_123", 0, NULL, 0), CBM_REG_OK);+ ASSERT_EQ(cbm_regexec(&re, "bind_400", 0, NULL, 0), CBM_REG_NOMATCH);+ cbm_regfree(&re);++ /* The same at realistic identifier length: 400 names of 20 characters is+ * 8,401 bytes and 28,000 units, inside the budget — the length cap never+ * decides a name list of this size, the unit budget does. */+ static const char *const long_prefixes[] = {"compute_checksum", "validate_request",+ "serialize_record", "normalize_header",+ "dispatch_message"};+ const size_t long_cap = 400 * 21 + 3;+ char *long_names = malloc(long_cap);+ ASSERT_NOT_NULL(long_names);+ w = long_names;+ *w++ = '(';+ for (int i = 0; i < 400; i++) {+ w += snprintf(w, long_cap - (size_t)(w - long_names), "%s%s_%03d", i ? "|" : "",+ long_prefixes[i % 5], i);+ }+ *w++ = ')';+ *w = '\0';+ ASSERT_EQ(strlen(long_names), 8401);+ ASSERT_EQ(regex_units_ere(long_names), 28000);+ ASSERT_EQ(cbm_regcomp(&re, long_names, CBM_REG_EXTENDED | CBM_REG_NOSUB), CBM_REG_OK);+ ASSERT_EQ(cbm_regexec(&re, "normalize_header_123", 0, NULL, 0), CBM_REG_OK);+ ASSERT_EQ(cbm_regexec(&re, "normalize_header_400", 0, NULL, 0), CBM_REG_NOMATCH);+ cbm_regfree(&re);+ free(long_names);++ /* `.{0,500}` is 500 units, inside the budget. Whether it compiles is the+ * platform's RE_DUP_MAX (255 on the TRE-derived libcs, 32767 on glibc),+ * which the guard does not change. */+ ASSERT_LTE(regex_units_ere(".{0,500}"), (uint64_t)CBM_REGEX_COMPILE_BUDGET_UNITS);+#ifdef __linux__+ ASSERT_EQ(cbm_regcomp(&re, ".{0,500}", CBM_REG_EXTENDED | CBM_REG_NOSUB), CBM_REG_OK);+ cbm_regfree(&re);+#endif+ PASS();+}++/* Sizing must read the syntax the way the compilers do: nothing inside a+ * bracket expression operates, a backslash escapes one character, braces that+ * do not form an interval are literals, and only an interval multiplies. */+TEST(regex_estimate_counts_brackets_and_escapes_literally) {+ ASSERT_EQ(regex_units_ere("[{]{2}"), 2);+ ASSERT_EQ(regex_units_ere("\\{"), 1);+ ASSERT_EQ(regex_units_ere("a\\{2\\}"), 4);+ ASSERT_EQ(regex_units_ere("[]a]{2}"), 2);+ ASSERT_EQ(regex_units_ere("[^]a]{3}"), 3);+ ASSERT_EQ(regex_units_ere("[[:alpha:]]{4}"), 4);+ ASSERT_EQ(regex_units_ere("[a-z{3}]"), 1);+ ASSERT_EQ(regex_units_ere("a{"), 2);+ ASSERT_EQ(regex_units_ere("a{2"), 3);+ ASSERT_EQ(regex_units_ere("a{x}"), 4);+ ASSERT_EQ(regex_units_ere("a{2}"), 2);+ ASSERT_EQ(regex_units_ere("a{2,}"), 2);+ ASSERT_EQ(regex_units_ere("a{,3}"), 3);+ ASSERT_EQ(regex_units_ere("a{2,5}"), 5);+ ASSERT_EQ(regex_units_ere("a*b+c?"), 3);+ ASSERT_EQ(regex_units_ere("a{3}{3}"), 9);+ ASSERT_EQ(regex_units_ere("(ab){3}"), 6);+ ASSERT_EQ(regex_units_ere("((ab){3}){4}"), 24);+ /* A K-way alternation is charged K*K/8 on top of its branches. */+ ASSERT_EQ(regex_units_ere("a|b|c|d"), 4 + 2);+ ASSERT_EQ(regex_units_ere("(a|b|c|d){10}"), 60);+ /* Basic syntax: bare braces and parens are literals; `\{ \}` and `\( \)`+ * operate. No call site uses it today, but the guard is shared. */+ ASSERT_EQ(regex_units_bre("a{2}"), 4);+ ASSERT_EQ(regex_units_bre("a\\{2\\}"), 2);+ ASSERT_EQ(regex_units_bre("\\(ab\\)\\{3\\}"), 6);+ ASSERT_EQ(regex_units_bre("(ab){3}"), 7);+ PASS();+}+SUITE(platform) {RUN_TEST(platform_file_apis_survive_max_path_overflow);RUN_TEST(platform_mkdir_p_follows_own_symlink_only_when_opted_in);@@ -1192,6 +1421,11 @@ SUITE(platform) {RUN_TEST(platform_env_long_reads_a_clean_number);RUN_TEST(platform_env_long_refuses_what_it_cannot_read);RUN_TEST(platform_system_info);+ RUN_TEST(regex_compile_refuses_oversized_expansion);+ RUN_TEST(regex_compile_accepts_expansion_under_budget);+ RUN_TEST(regex_compile_refuses_overlong_pattern);+ RUN_TEST(regex_compile_accepts_ordinary_intervals);+ RUN_TEST(regex_estimate_counts_brackets_and_escapes_literally);#ifdef __linux__RUN_TEST(cgroup_v2_cpu_quota);RUN_TEST(cgroup_v2_cpu_quota_rounds_up);