MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cross_join_with_rels

Function cross_join_with_rels

src/cypher/cypher.c:5654–5698  ·  view source on GitHub ↗

Cross-join pattern-with-rels into existing bindings */

Source from the content-addressed store, hash-verified

5652
5653/* Cross-join pattern-with-rels into existing bindings */
5654static void cross_join_with_rels(cbm_store_t *store, cbm_pattern_t *patn, binding_t **bindings,
5655 int *bind_count, cbm_node_t *extra_nodes, int extra_count,
5656 const char *nvar, bool opt) {
5657 /* size_t arithmetic: bind_count * extra_count can exceed INT_MAX on large
5658 * graphs (e.g. an unbound `c` scanned against ~29 K `f` bindings), wrapping
5659 * the int product negative and yielding a tiny/garbage malloc → heap OOB
5660 * write → SIGSEGV/SIGABRT (#627). */
5661 int new_cap = *bind_count > 0 && extra_count > 0 ? *bind_count : SKIP_ONE;
5662 binding_t *new_bindings = malloc((size_t)new_cap * sizeof(binding_t));
5663 if (!new_bindings) {
5664 return; /* OOM: leave existing bindings untouched rather than corrupt */
5665 }
5666 int new_count = 0;
5667 for (int bi = 0; bi < *bind_count; bi++) {
5668 for (int ni = 0; ni < extra_count; ni++) {
5669 binding_t nb = {0};
5670 binding_copy(&nb, &(*bindings)[bi]);
5671 binding_set(&nb, nvar, &extra_nodes[ni]);
5672 binding_t *tmp = malloc(PAIR_LEN * sizeof(binding_t));
5673 tmp[0] = nb;
5674 int tc = SKIP_ONE;
5675 int tcap = SKIP_ONE;
5676 const char *tv = nvar;
5677 expand_pattern_rels(store, patn, &tmp, &tc, &tcap, &tv, opt);
5678 for (int ti = 0; ti < tc; ti++) {
5679 (void)binding_out_append(&new_bindings, &new_count, &new_cap, &tmp[ti]);
5680 }
5681 free(tmp);
5682 }
5683 if (opt && extra_count == 0) {
5684 /* OPTIONAL with no start node: keep the row, pattern vars unbound.
5685 * The buffer was sized for a single row when extra_count == 0, so
5686 * this write must grow it like every other writer here. */
5687 binding_t nb = {0};
5688 binding_copy(&nb, &(*bindings)[bi]);
5689 (void)binding_out_append(&new_bindings, &new_count, &new_cap, &nb);
5690 }
5691 }
5692 for (int bi = 0; bi < *bind_count; bi++) {
5693 binding_free(&(*bindings)[bi]);
5694 }
5695 free(*bindings);
5696 *bindings = new_bindings;
5697 *bind_count = new_count;
5698}
5699
5700/* Drive a single-relationship additional pattern from its ALREADY-BOUND
5701 * terminal node, binding the unbound START var to the edge's other endpoint.

Callers 1

Calls 5

binding_copyFunction · 0.85
binding_setFunction · 0.85
expand_pattern_relsFunction · 0.85
binding_out_appendFunction · 0.85
binding_freeFunction · 0.85

Tested by

no test coverage detected