Skip to content

Commit 2fabaa2

Browse files
committed
fix(cypher): grow the OPTIONAL fallback buffer in the rel cross-join
An OPTIONAL MATCH with a relationship, whose start and terminal variables are not bound by an earlier MATCH and whose start node pattern matches no node, is joined in cross_join_with_rels() with an empty candidate list. That path sized its output for a single binding, but the OPTIONAL fallback then kept one row per existing binding with a plain indexed store, so every row after the first was written past the end of the allocation. Route the fallback through binding_out_append(), the growing writer that the expansion branch of the same function and the other OPTIONAL fallbacks already use. The node-only join (cross_join_nodes) was fixed earlier by sizing its buffer for the fallback rows; the relationship variant kept the unchecked store. Add a cypher regression test that runs such a query over four bound functions and asserts one row per function with the optional variables unbound. Before this change the test runner reported a heap-buffer-overflow in cross_join_with_rels under ASan. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 80eb92a commit 2fabaa2

2 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎src/cypher/cypher.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4811,9 +4811,12 @@ static void cross_join_with_rels(cbm_store_t *store, cbm_pattern_t *patn, bindin
48114811
free(tmp);
48124812
}
48134813
if (opt && extra_count == 0) {
4814+
/* OPTIONAL with no start node: keep the row, pattern vars unbound.
4815+
* The buffer was sized for a single row when extra_count == 0, so
4816+
* this write must grow it like every other writer here. */
48144817
binding_t nb = {0};
48154818
binding_copy(&nb, &(*bindings)[bi]);
4816-
new_bindings[new_count++] = nb;
4819+
(void)binding_out_append(&new_bindings, &new_count, &new_cap, &nb);
48174820
}
48184821
}
48194822
for (int bi = 0; bi < *bind_count; bi++) {

‎tests/test_cypher.c‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,45 @@ TEST(cypher_exec_optional_empty_label_no_overflow) {
715715
PASS();
716716
}
717717

718+
/* Regression: the relationship variant of the test above. An OPTIONAL MATCH
719+
* with a relationship whose start AND terminal are both unbound, and whose
720+
* start matches zero nodes, drove cross_join_with_rels with extra_count == 0.
721+
* That sized its output for a single binding, and the OPTIONAL fallback then
722+
* wrote one row per existing binding without growing the buffer: a heap
723+
* buffer overflow once the first MATCH bound more than one node (ASan:
724+
* heap-buffer-overflow). The query text reaches this from the MCP query tool. */
725+
TEST(cypher_exec_optional_empty_label_rel_no_overflow) {
726+
cbm_store_t *s = setup_cypher_store(); /* 4 Function nodes */
727+
cbm_cypher_result_t r = {0};
728+
729+
int rc = cbm_cypher_execute(s,
730+
"MATCH (a:Function) OPTIONAL MATCH (x:NoSuchLabel)-[:CALLS]->(y) "
731+
"RETURN a.name, x.name, y.name",
732+
"test", 0, &r);
733+
ASSERT_EQ(rc, 0);
734+
ASSERT_EQ(r.col_count, 3);
735+
/* One row per Function, each with x and y left unbound (""). */
736+
ASSERT_EQ(r.row_count, 4);
737+
const char *want[] = {"HandleOrder", "ValidateOrder", "SubmitOrder", "LogError"};
738+
for (int w = 0; w < 4; w++) {
739+
int seen = 0;
740+
for (int i = 0; i < r.row_count; i++) {
741+
if (strcmp(r.rows[i][0], want[w]) == 0) {
742+
seen++;
743+
}
744+
}
745+
ASSERT_EQ(seen, 1);
746+
}
747+
for (int i = 0; i < r.row_count; i++) {
748+
ASSERT_STR_EQ(r.rows[i][1], "");
749+
ASSERT_STR_EQ(r.rows[i][2], "");
750+
}
751+
752+
cbm_cypher_result_free(&r);
753+
cbm_store_close(s);
754+
PASS();
755+
}
756+
718757
/* Regression: expand_pattern_rels sized its OPTIONAL-expansion output buffer as
719758
* bind_cap*10 + 1 — room for the bounded expansion (max_new = bind_cap*10) plus
720759
* a SINGLE OPTIONAL fallback row. When one source saturated the expansion to
@@ -4673,6 +4712,7 @@ SUITE(cypher) {
46734712
RUN_TEST(cypher_deep_nesting_rejected_not_crash);
46744713
RUN_TEST(cypher_exec_match_all_functions);
46754714
RUN_TEST(cypher_exec_optional_empty_label_no_overflow);
4715+
RUN_TEST(cypher_exec_optional_empty_label_rel_no_overflow);
46764716
RUN_TEST(cypher_cross_join_alloc_rejects_overflow);
46774717
RUN_TEST(cypher_exec_optional_rel_saturated_no_overflow);
46784718
RUN_TEST(cypher_exec_optional_saturated_does_not_fabricate_no_match);

0 commit comments

Comments
 (0)