@@ -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+
549678TEST (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