Skip to content

fix(cypher): report an =~ pattern that does not compile - #3069

Open
Zhuoxi2000 wants to merge 2 commits into
DeusData:mainfrom
Zhuoxi2000:fix-cypher-invalid-regex-silent
Open

Zhuoxi2000 wants to merge 2 commits into
DeusData:mainfrom
Zhuoxi2000:fix-cypher-invalid-regex-silent

Conversation

@Zhuoxi2000

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #3022

In query_graph, an =~ pattern that the regex compiler rejects is now reported in the result's warning field. A pattern written in the query is named; a pattern read from a row is reported without its value. Before, nothing in the result said the pattern had never run:

Cause. eval_comparison_op (src/cypher/cypher.c) returns false whenever cbm_regcomp fails. Only the compile-size refusal (CBM_REG_ETOOBIG, added in #2475) sets the flag that cbm_cypher_execute turns into result->warning. Every other compile error became "row does not match" on every row.

Change (src/cypher/cypher.c, +68/-5 lines, no other source file):

  • The first literal pattern the platform compiler rejects during an execution is recorded in a thread-local, next to g_cypher_regex_refused and reset with it.
  • cbm_cypher_execute appends a warning after the existing traversal and too-large warnings:
    =~ pattern '<pattern>' is not a valid POSIX extended regular expression — the comparison evaluated to false on every row
  • The echoed pattern is capped at 127 bytes; a longer one is cut on a code point boundary and ends in ....
  • WHERE a.name =~ b.name (query_graph: a WHERE comparison between two properties (a.file_path = b.file_path) fails with "expected value at pos N" #2591) takes its pattern from the row. That value is graph data, not query text, and only the rows that hold a value that does not compile compare false. This case sets a separate flag and gets its own warning, which repeats no value:
    =~ pattern read from a property or alias is not a valid POSIX extended regular expression on at least one row — the comparison evaluated to false on those rows
    eval_comparison_op takes a bool that says which of its two callers supplied the pattern.
  • The warning buffer in cbm_cypher_execute grows from 512 to 1024 bytes. The four notices together are about 670 bytes at worst.
  • src/mcp/mcp.c is not touched: query_graph already writes result->warning in both output formats.

Why a warning and not an error. #3022 suggests an error, and search_code returns one for an invalid pattern since #283 ("conflating "broken pattern" with "no matches" wastes debugging time"). I kept the shape #2475 chose for this same code path: the comparison still evaluates to false, the query still returns rc 0, and the warning carries the reason. The comment on cypher_exec_where_regex_oversized_pattern_matches_nothing already describes it: the comparison "matches nothing, as for any pattern that cannot be compiled", and "the engine keeps answering". Until now only the oversized case reached the warning. Keeping that shape means no query that answers today starts failing, for example WHERE f.name =~ 'bad(' OR f.name = 'x', which still returns the x rows. The cost is that the NOT form still returns every row, now with the warning attached. If you would rather have a query error here, it is a small change on top of this one (the pattern is already recorded) and I am happy to switch.

Result, end to end with the built binary on a four-function Python file (claim_order, claim_refund, Claim_Legacy, log_error):

$ codebase-memory-mcp cli --quiet query_graph '{"project":"...","query":"MATCH (f:Function) WHERE f.name =~ '\''(?i)claim.*'\'' RETURN f.name LIMIT 5"}'
rows: 0  (cols: f.name)
returned: 0
total: 0
total_relation: eq
has_more: false
truncated: false
warning: "=~ pattern '(?i)claim.*' is not a valid POSIX extended regular expression — the comparison evaluated to false on every row"
hint: "Query returned no results. Use get_graph_schema() to see available labels and edge types."

$ ... "MATCH (f:Function) WHERE NOT f.name =~ 'claim(' RETURN f.name LIMIT 5"
rows: 5  (cols: f.name)
  ...
warning: "=~ pattern 'claim(' is not a valid POSIX extended regular expression — the comparison evaluated to false on every row"

=~ 'claim.*' on the same index returns claim_order and claim_refund with no warning.

A pattern read from a row, on a second index of three files: orders.py (the four functions above), claim.py (claim_all) and a file named claim(.py (claim_bad). The file name claim(.py does not compile as a pattern. The functions of the other two files are returned and the warning does not repeat the name:

$ ... "MATCH (m:File)-[:DEFINES]->(f:Function) WHERE f.file_path =~ m.name RETURN m.name, f.name"
rows: 5  (cols: m.name f.name)
  orders.py log_error
  orders.py claim_order
  orders.py claim_refund
  orders.py Claim_Legacy
  claim.py claim_all
returned: 5
total: 5
total_relation: eq
has_more: false
truncated: false
warning: "=~ pattern read from a property or alias is not a valid POSIX extended regular expression on at least one row — the comparison evaluated to false on those rows"

Limits of this change, and what it leaves alone:

  • The warning is raised when the comparison is evaluated, like the too-large one. A query in which no row reaches the =~ (no candidate nodes, or an AND that short-circuits before it) carries no warning.
  • Only the first rejected literal pattern of a query is named. A pattern read from a row is never named, and its warning does not say which rows or how many were affected.
  • A query that has both a rejected literal and a rejected row value carries both warnings, joined with ; .
  • Inline property patterns ({name: "..."}) are unchanged. A value that does not compile there falls back to a literal comparison, which looks deliberate.
  • No (?i) support (suggestion 2 in query_graph: =~ with an unsupported pattern such as (?i) silently returns 0 rows instead of an error #3022). Stripping a leading (?i) and compiling with CBM_REG_ICASE changes what existing queries match, so it should be its own change. I can send it as a follow-up if you want it.
  • No README change. A line saying =~ is POSIX ERE would fit next to the WHERE operator list; I left it out to keep this PR to one claim.

Platforms. Only macOS was run locally. An unbalanced group is rejected by every backend, so the assertions that use one (the literal pattern, the NOT form, the length cap and the pattern read from a row) do not depend on the platform. The (?i) assertion does: libc regcomp on macOS rejects it, while the vendored TRE used on Windows has a (?...) branch in vendored/tre/tre-parse.c and may accept it. The test therefore accepts either the warning or the three case-insensitive matches for that one pattern. Linux, FreeBSD and Windows are left to CI.

Checklist

  • Every commit is signed off (git commit -s): scripts/check-dco.sh origin/main..HEAD reports OK: 2 commit(s) in origin/main..HEAD carry a valid Signed-off-by.
  • Tests pass locally, with one caveat: the ASan+UBSan runner from make -f Makefile.cbm test hangs on this machine during ASan shadow-memory init, before main() (macOS 26.5, Apple clang 17.0.0), so an unsanitized runner was used: make -f Makefile.cbm -j4 SANITIZE= BUILD_DIR=build/nosan build/nosan/test-runner. The sanitized run is left to CI. Suites run with the fix:
    • cypher: 246 passed
    • mcp: 438 passed, 4 skipped
    • integration: 27 passed
    • store_search: 78 passed
  • Lint passes:
    • make -f Makefile.cbm lint-format with clang-format 20.1.8: clean over LINT_SRCS and LINT_HDRS. tests/test_cypher.c has two violations that are already on main (the ASSERT_TRUE(ref_query( call at line 6168 on main; test files are not in LINT_SRCS) and none in the lines this PR adds.
    • make -f Makefile.cbm lint-cppcheck with cppcheck 2.20.0 (built from the tag CI uses) passes.
    • lint-no-suppress, lint-memory-core, lint-vendored-scanners and scripts/check-no-test-skips.sh pass.
    • clang-tidy (local only, not part of lint-ci) was not run.
  • New behavior is covered by a test (reproduce-first): cypher_exec_where_regex_invalid_pattern_is_reported_issue3022 in tests/test_cypher.c.

Test evidence

Machine: Apple M4, macOS 26.5, Apple clang 17.0.0. Base: main at 1111d9a.

Before (test commit only, src/cypher/cypher.c as on main):

$ make -f Makefile.cbm -j4 SANITIZE= BUILD_DIR=build/nosan build/nosan/test-runner
$ ./build/nosan/test-runner cypher
  cypher_exec_where_regex_invalid_pattern_is_reported_issue3022  FAIL tests/test_cypher.c:1619: r.warning is NULL
  245 passed, 1 failed

After (both commits):

$ ./build/nosan/test-runner cypher
  cypher_exec_where_regex                                PASS
  cypher_exec_where_regex_oversized_pattern_matches_nothingPASS
  cypher_exec_where_regex_invalid_pattern_is_reported_issue3022PASS
  246 passed

The test pins, for the unbalanced pattern .*Order(: rc 0, 0 rows, and a warning that contains the pattern and the reason. For the NOT form it pins rc 0 and the warning. It checks that a 201-byte pattern is cut on a code point boundary and marked. For a.name =~ b.name it builds a three-node store in which Caller calls one function named Call.* and one named Unbalanced(Name, and pins: rc 0, the one row whose pattern compiles is returned, a warning is present, and the warning contains neither Unbalanced nor "every row". Last, a valid pattern run afterwards returns its 3 rows with no warning.

Size: 2 commits (test, then fix), 2 files, +182/-5 lines (src/cypher/cypher.c +68/-5, tests/test_cypher.c +114).

AI assistance: this change was drafted with an AI coding assistant (Claude) and verified locally with the tests above.

…ning

Regression test for DeusData#3022. On main the compile failure of an =~ pattern
is swallowed per row: the query answers rc 0 with no rows, no error and
no warning, and under NOT it answers every row. Only the compile-size
refusal (CBM_REG_ETOOBIG) is surfaced today.

The test pins the engine contract for a pattern no dialect accepts (an
unbalanced group): rc 0, the comparison false on every row, and a
warning that names the pattern. For the reporter's `(?i)` pattern it
accepts either that warning or, on a regex backend that implements the
flag (the TRE build used on Windows parses `(?...)`), the three
case-insensitive matches.

When the pattern is read from the row (`a.name =~ b.name`) the test
pins a warning that repeats no property value and does not say "every
row": the row whose value does compile is still returned.

Signed-off-by: Edson <zhuoxi2000@gmail.com>
eval_comparison_op returned false whenever cbm_regcomp failed and only
flagged the compile-size refusal (CBM_REG_ETOOBIG). Any other compile
error, such as an unbalanced group or the `(?i)` flag POSIX ERE does not
have, turned into "row does not match" on every row with nothing in the
result to say so: `WHERE f.name =~ "(?i)claim.*"` answered rc 0 and no
rows, and `WHERE NOT f.name =~ ".*Order("` answered every row.

The first literal pattern the platform compiler rejects during an
execution is now recorded and cbm_cypher_execute names it in
result->warning, next to the existing traversal and too-large warnings.
query_graph already emits that field. The comparison still evaluates to
false and the query still returns rc 0, as it does for an oversized
pattern, so no query that answered before starts failing. The echoed
pattern is capped at 127 bytes, cut on a code point boundary and marked
with "...".

A pattern read from the row (`a.name =~ b.name`) gets a separate
warning that repeats no value: it is graph data, not query text, and
only the rows holding such a value compared false. The warning buffer
grows from 512 to 1024 bytes so that all four notices fit together.

Inline property patterns ({name: "..."}) are unchanged: a value that
does not compile there falls back to a literal comparison.

Fixes DeusData#3022

Signed-off-by: Edson <zhuoxi2000@gmail.com>
@Zhuoxi2000
Zhuoxi2000 requested a review from DeusData as a code owner November 21, 2026 02:04
@github-actions

Copy link
Copy Markdown

Thanks for opening this — it has been seen, and it is queued.

This note is automated, but it is not a brush-off: it exists so you know where your PR stands instead of having to guess from silence.

Current review status: working through a backlog. 0.9.1-rc.1 is out, so the release freeze that held reviews is over — but it left a large queue of open pull requests behind it, and we are reading through them oldest-first. The background is in discussion #1144.

What that means for this PR, concretely:

  • It will not be closed for inactivity. No stale bot touches pull requests here.
  • It may still sit a while before a human reads it. That is on us, not on you.
  • Older PRs are read first, so a recent one is not being skipped — it is behind a queue.

Things that will genuinely speed it up whenever review does happen:

  • Keep it rebased on main — the tree is moving quickly right now, and a conflicting branch cannot be reviewed as the diff you intended.
  • Get CI green, or say which failures you believe are pre-existing.
  • Keep the change to one claim. Bundled features and refactors get split before they get merged, which costs you a round trip.
  • Every commit needs a sign-off (git commit -s) — CI enforces DCO.

If this fixes a bug, a reproduction we can run is worth more than a description of the symptom.

Thanks for contributing, and sorry in advance for the wait.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

query_graph: =~ with an unsupported pattern such as (?i) silently returns 0 rows instead of an error

1 participant