Skip to content

Commit 7814e1b

Browse files
authored
Merge pull request DeusData#1177 from SEPURI-SAI-KRISHNA/fix/cypher-expand-optional-overflow
fix(cypher): bound OPTIONAL fallback in expand_pattern_rels to a single ceiling
2 parents 9cad2be + c2a19b8 commit 7814e1b

2 files changed

Lines changed: 144 additions & 2 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3233,7 +3233,15 @@ static void expand_pattern_rels(cbm_store_t *store, cbm_pattern_t *pat, binding_
32333233

32343234
bool is_variable_length = (rel->min_hops != SKIP_ONE || rel->max_hops != SKIP_ONE);
32353235

3236-
size_t alloc_n = (size_t)*bind_cap * (size_t)CYP_GROWTH_10 + SKIP_ONE;
3236+
/* Size this hop's output for BOTH writers without dropping any row: the
3237+
* expansion helpers emit at most max_new = bind_cap*10 rows (they stop at
3238+
* max_new), and the OPTIONAL fallback emits at most one row per source
3239+
* (<= *bind_count). A source either matches (feeds the expansion) or takes
3240+
* the fallback, never both, so the two counts are additive and bounded by
3241+
* max_new + *bind_count. Computed in size_t so the product cannot overflow.
3242+
* The previous "+ 1" sizing fit only a SINGLE fallback row after a
3243+
* saturated expansion; a second one ran off the end (heap OOB, CWE-787). */
3244+
size_t alloc_n = (size_t)*bind_cap * (size_t)CYP_GROWTH_10 + (size_t)*bind_count;
32373245
binding_t *new_bindings = malloc(alloc_n * sizeof(binding_t));
32383246
if (!new_bindings) {
32393247
return; /* OOM: leave existing bindings untouched rather than corrupt */
@@ -3261,7 +3269,10 @@ static void expand_pattern_rels(cbm_store_t *store, cbm_pattern_t *pat, binding_
32613269
&new_count, max_new, &match_count);
32623270
}
32633271

3264-
/* OPTIONAL MATCH: keep binding with empty target if no matches */
3272+
/* OPTIONAL MATCH: no expansion for this source, so keep the binding
3273+
* with the target unbound (projection renders it ""). The buffer is
3274+
* sized max_new + *bind_count precisely so every such fallback row has
3275+
* a slot — no guard needed, and no OPTIONAL no-match row is dropped. */
32653276
if (is_optional && match_count == 0) {
32663277
binding_t nb = {0};
32673278
binding_copy(&nb, b);

‎tests/test_cypher.c‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,69 @@ TEST(cypher_exec_optional_empty_label_no_overflow) {
517517
PASS();
518518
}
519519

520+
/* Regression: expand_pattern_rels sized its OPTIONAL-expansion output buffer as
521+
* bind_cap*10 + 1 — room for the bounded expansion (max_new = bind_cap*10) plus
522+
* a SINGLE OPTIONAL fallback row. When one source saturated the expansion to
523+
* max_new and two or more later sources took the OPTIONAL (no-match) path, the
524+
* second fallback write ran past the allocation (ASan: heap-buffer-overflow).
525+
*
526+
* The fix sizes the buffer for both writers losslessly (max_new + *bind_count),
527+
* so every OPTIONAL no-match row keeps its slot — no overflow AND no dropped
528+
* rows. Here the hub saturates the expansion to max_new while all 20 leaves take
529+
* the fallback; the buffer holds them all, and the max_rows LIMIT bounds only the
530+
* OUTPUT. Query text is agent-controlled via the MCP query tool. */
531+
TEST(cypher_exec_optional_rel_saturated_no_overflow) {
532+
cbm_store_t *s = cbm_store_open_memory();
533+
cbm_store_upsert_project(s, "test", "/tmp/test");
534+
535+
/* 1 hub + 20 leaf Function nodes → bind_cap = 21, max_new = 210, so the hop
536+
* buffer holds max_new + *bind_count = 231 rows. The hub is inserted first so
537+
* it is expanded before the leaves; it saturates the expansion to max_new,
538+
* then each of the 20 leaves adds one OPTIONAL fallback row — under the old
539+
* alloc (211 slots) the 2nd such write overflowed; now all 230 fit. */
540+
cbm_node_t hub = {
541+
.project = "test", .label = "Function", .name = "hub", .qualified_name = "test.hub"};
542+
int64_t hub_id = cbm_store_upsert_node(s, &hub);
543+
for (int i = 0; i < 20; i++) {
544+
char nm[32];
545+
char qn[48];
546+
snprintf(nm, sizeof(nm), "leaf%02d", i);
547+
snprintf(qn, sizeof(qn), "test.leaf%02d", i);
548+
cbm_node_t leaf = {
549+
.project = "test", .label = "Function", .name = nm, .qualified_name = qn};
550+
cbm_store_upsert_node(s, &leaf);
551+
}
552+
553+
/* Give the hub 300 CALLS edges (> max_new = 210) so its expansion saturates
554+
* max_new; targets are non-Function so they don't inflate bind_cap. */
555+
for (int i = 0; i < 300; i++) {
556+
char nm[32];
557+
char qn[48];
558+
snprintf(nm, sizeof(nm), "callee%d", i);
559+
snprintf(qn, sizeof(qn), "test.callee%d", i);
560+
cbm_node_t callee = {.project = "test", .label = "Var", .name = nm, .qualified_name = qn};
561+
int64_t cid = cbm_store_upsert_node(s, &callee);
562+
cbm_edge_t e = {.project = "test", .source_id = hub_id, .target_id = cid, .type = "CALLS"};
563+
cbm_store_insert_edge(s, &e);
564+
}
565+
566+
/* max_rows below the Function count (21) so bind_cap tracks scan_count (21)
567+
* rather than the 100000 result ceiling — the same regime a large repo
568+
* (> ceiling functions) or an agent-supplied small limit hits. */
569+
cbm_cypher_result_t r = {0};
570+
int rc = cbm_cypher_execute(
571+
s, "MATCH (a:Function) OPTIONAL MATCH (a)-[:CALLS]->(b) RETURN a.name", "test", 5, &r);
572+
/* Bounded success, no overflow (ASan proves the buffer holds every row); the
573+
* LIMIT caps the output rows rather than the query crashing. */
574+
ASSERT_EQ(rc, 0);
575+
ASSERT_GT(r.row_count, 0);
576+
ASSERT_TRUE(r.row_count <= 5);
577+
578+
cbm_cypher_result_free(&r);
579+
cbm_store_close(s);
580+
PASS();
581+
}
582+
520583
/* Arithmetic-boundary companion to the zero-label overflow above: the node
521584
* cross-join sizes its buffer from bind_count * extra_count. As a plain int that
522585
* product wraps past INT_MAX to a negative/garbage malloc size (the large-graph
@@ -546,6 +609,72 @@ TEST(cypher_cross_join_alloc_rejects_overflow) {
546609
PASS();
547610
}
548611

612+
/* Companion to the truncation regression: when the expansion does NOT saturate
613+
* the ceiling, every leaf's OPTIONAL fallback row must survive with its target
614+
* unbound. A `row_count > 0` check is too weak — it can pass on hub rows alone —
615+
* so this asserts a specific leaf appears with an empty b.name, and that a real
616+
* expanded hub row is present too. */
617+
TEST(cypher_exec_optional_rel_leaf_fallback_survives) {
618+
cbm_store_t *s = cbm_store_open_memory();
619+
cbm_store_upsert_project(s, "test", "/tmp/test");
620+
621+
/* 1 hub (2 CALLS edges) + 3 leaves (no edges). max_rows 0 is defaulted to
622+
* CYPHER_RESULT_CEILING (100000) in cbm_cypher_execute before bind_cap is
623+
* computed, so bind_cap = max(scan_count, 100000) = 100000 and the buffer is
624+
* far larger than needed here — this exercises the fallback rows, not the
625+
* saturation edge. */
626+
cbm_node_t hub = {
627+
.project = "test", .label = "Function", .name = "hub", .qualified_name = "test.hub"};
628+
int64_t hub_id = cbm_store_upsert_node(s, &hub);
629+
for (int i = 0; i < 3; i++) {
630+
char nm[32];
631+
char qn[48];
632+
snprintf(nm, sizeof(nm), "leaf%d", i);
633+
snprintf(qn, sizeof(qn), "test.leaf%d", i);
634+
cbm_node_t leaf = {
635+
.project = "test", .label = "Function", .name = nm, .qualified_name = qn};
636+
cbm_store_upsert_node(s, &leaf);
637+
}
638+
for (int i = 0; i < 2; i++) {
639+
char nm[32];
640+
char qn[48];
641+
snprintf(nm, sizeof(nm), "callee%d", i);
642+
snprintf(qn, sizeof(qn), "test.callee%d", i);
643+
cbm_node_t callee = {.project = "test", .label = "Var", .name = nm, .qualified_name = qn};
644+
int64_t cid = cbm_store_upsert_node(s, &callee);
645+
cbm_edge_t e = {.project = "test", .source_id = hub_id, .target_id = cid, .type = "CALLS"};
646+
cbm_store_insert_edge(s, &e);
647+
}
648+
649+
cbm_cypher_result_t r = {0};
650+
int rc = cbm_cypher_execute(
651+
s, "MATCH (a:Function) OPTIONAL MATCH (a)-[:CALLS]->(b) RETURN a.name, b.name", "test", 0,
652+
&r);
653+
ASSERT_EQ(rc, 0);
654+
ASSERT_EQ(r.col_count, 2);
655+
656+
/* Scan for a leaf fallback row (a.name = "leaf0", b.name unbound = "") and a
657+
* real expanded hub row (a.name = "hub", b.name non-empty). */
658+
bool leaf_fallback = false;
659+
bool hub_expanded = false;
660+
for (int i = 0; i < r.row_count; i++) {
661+
const char *a = r.rows[i][0];
662+
const char *b = r.rows[i][1];
663+
if (strcmp(a, "leaf0") == 0 && b[0] == '\0') {
664+
leaf_fallback = true;
665+
}
666+
if (strcmp(a, "hub") == 0 && b[0] != '\0') {
667+
hub_expanded = true;
668+
}
669+
}
670+
ASSERT_TRUE(leaf_fallback); /* the OPTIONAL no-match row survived */
671+
ASSERT_TRUE(hub_expanded); /* the expansion still produced bound rows */
672+
673+
cbm_cypher_result_free(&r);
674+
cbm_store_close(s);
675+
PASS();
676+
}
677+
549678
TEST(cypher_exec_where_eq) {
550679
cbm_store_t *s = setup_cypher_store();
551680
cbm_cypher_result_t r = {0};
@@ -3294,6 +3423,8 @@ SUITE(cypher) {
32943423
RUN_TEST(cypher_exec_match_all_functions);
32953424
RUN_TEST(cypher_exec_optional_empty_label_no_overflow);
32963425
RUN_TEST(cypher_cross_join_alloc_rejects_overflow);
3426+
RUN_TEST(cypher_exec_optional_rel_saturated_no_overflow);
3427+
RUN_TEST(cypher_exec_optional_rel_leaf_fallback_survives);
32973428
RUN_TEST(cypher_issue240_labels_function);
32983429
RUN_TEST(cypher_issue237_distinct_order_limit);
32993430
RUN_TEST(cypher_issue873_distinct_order_limit_dedupes_before_limit);

0 commit comments

Comments
 (0)