fix(cypher): bound OPTIONAL fallback in expand_pattern_rels to prevent heap overflow (#627)#1177
Open
SEPURI-SAI-KRISHNA wants to merge 1 commit into
Conversation
…t heap overflow (DeusData#627) Signed-off-by: SEPURI-SAI-KRISHNA <saik20533@gmail.com>
SEPURI-SAI-KRISHNA
force-pushed
the
fix/cypher-expand-optional-overflow
branch
from
July 19, 2026 12:22
7891465 to
a3adfbc
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
Fixes a heap buffer overflow (CWE-787) in
expand_pattern_rels(
src/cypher/cypher.c) when expanding anOPTIONAL MATCHrelationship pattern.Another root cause behind the still-open #627 ("Crash when calling
query_graph"). The query text is agent-controlled via the MCP
querytool.The per-hop output buffer is sized
bind_cap * 10 + 1:The expansion helpers (
expand_fixed_length/expand_var_length) correctlystop at
max_new = bind_cap * 10. But the OPTIONAL fallback that keeps a rowwith the target left unbound is not bounded:
The
+ 1leaves room for a single OPTIONAL row after a saturatedexpansion. When one source binding saturates the expansion to
max_newandtwo or more later sources take the OPTIONAL (no-match) path, the second
fallback write runs past the allocation.
Reachable with e.g.:
whenever
bind_captracks the scanned node count — i.e. the number of matchedstart nodes exceeds the result limit. That happens on a large repo (more
functions than the 100000 result ceiling — e.g. the Linux kernel, a flagship
target) even at the default limit, and on any repo when an agent supplies a
smaller
max_rows. A dense hub (out-degree ≥bind_cap * 10, e.g. a generateddispatch table or god object) saturates the buffer; the leaf functions that
follow then overflow it.
The fix sizes the buffer for the worst case — up to
max_newexpanded rowsplus one OPTIONAL fallback row per source binding — and bounds the fallback
write, matching the
max_newchecks the expansion helpers already do.Reproduced first (ASan, before the fix):
After the fix the full
cyphersuite passes with no sanitizer errors.Adds regression test
cypher_exec_optional_rel_saturated_no_overflow(a hubthat saturates the expansion followed by leaf functions on the OPTIONAL path).
Checklist
git commit -s) — required, CI rejectsunsigned commits (DCO, see CONTRIBUTING.md)
make -f Makefile.cbm test)make -f Makefile.cbm lint-ci)