Fix issue #10: Add null check for pGraph in cypher_execute()#12
Open
dirtyfilthy wants to merge 1 commit intoagentflare-ai:mainfrom
Open
Fix issue #10: Add null check for pGraph in cypher_execute()#12dirtyfilthy wants to merge 1 commit intoagentflare-ai:mainfrom
dirtyfilthy wants to merge 1 commit intoagentflare-ai:mainfrom
Conversation
…te() - Add null check before using pGraph in cypherExecuteSqlFunc() - Return clear error message when graph virtual table not initialized - Add test case to verify fix prevents segfaults - Test verifies error message and normal operation after table creation - Update Makefile to include new test and necessary LDFLAGS
gabewillen
approved these changes
Nov 17, 2025
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.
Description
This PR fixes a critical NULL pointer dereference bug in
cypher_execute()that caused segmentation faults when the function was called before creating a graph virtual table.Problem: The
cypher_execute()SQL function accessed the globalpGraphpointer without null validation. When called beforeCREATE VIRTUAL TABLE graph USING graph(),pGraphis NULL, causing segfaults when passed to functions that dereference it.Solution: Added a null check for
pGraphincypherExecuteSqlFunc()that returns a clear error message instead of crashing.Related Issues
Closes #10
Type of Change
Changes Made
pGraphinsrc/cypher/cypher-executor-sql.cbefore using it incypherPlannerCreate()andcypherExecutorCreate()tests/test_cypher_execute_before_table.cto verify:tests/Makefileto include new test inBASIC_TESTSTesting
make test)Test Evidence
Before fix:
$ python3 -c "import sqlite3; conn = sqlite3.connect(':memory:'); conn.enable_load_extension(True); conn.load_extension('./build/libgraph'); conn.execute('SELECT cypher_execute(\"MATCH (n) RETURN n\")')" Segmentation fault (SIGSEGV)After fix:
Documentation
Checklist
Breaking Changes
N/A - This is a bug fix that prevents crashes. The behavior change is:
cypher_execute()called before table creationcypher_execute()called before table creationPerformance Impact
The null check adds a single pointer comparison, which has negligible performance impact.
Code Changes
Modified:
src/cypher/cypher-executor-sql.cAdded null check after AST parsing and before using
pGraph:Added:
tests/test_cypher_execute_before_table.cComprehensive test case covering:
Additional Context
Root Cause: The global
pGraphvariable (defined insrc/graph.c:27) is initialized toNULLand only set whenCREATE VIRTUAL TABLE graph USING graph()is executed. Thecypher_execute()function was usingpGraphwithout validation.Impact:
cypher_execute()is called before graph table creationRelated Code:
src/graph.c:27- GlobalpGraphdeclarationsrc/graph.c:34-36-setGlobalGraph()functionsrc/graph-vtab.c:171-setGlobalGraph()called ingraphCreate()src/graph-vtab.c:287-setGlobalGraph()called ingraphConnect()src/cypher/cypher-executor-sql.c:15- External reference topGraphsrc/cypher/cypher-executor-sql.c:75,109- Usage ofpGraph(now protected by null check)Reviewer Notes
Please verify:
pGraph)cypher_execute()is called before table creationNOTE: includes linker flags from the fix test infrastructure pr because i needed them to run the tests
For Maintainers: