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

Function expand_pattern_rels

src/cypher/cypher.c:3283–3354  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3281}
3282
3283static void expand_pattern_rels(cbm_store_t *store, cbm_pattern_t *pat, binding_t **bindings,
3284 int *bind_count, const int *bind_cap, const char **var_name,
3285 bool is_optional) {
3286 for (int ri = 0; ri < pat->rel_count; ri++) {
3287 /* #601: stop expanding further hops once the wall-clock budget is spent
3288 * (an unbounded expansion is exactly what blows up here). */
3289 if (cypher_deadline_exceeded()) {
3290 return;
3291 }
3292 cbm_rel_pattern_t *rel = &pat->rels[ri];
3293 cbm_node_pattern_t *target_node = &pat->nodes[ri + SKIP_ONE];
3294 const char *to_var = target_node->variable ? target_node->variable : "_n_t";
3295
3296 bool is_variable_length = (rel->min_hops != SKIP_ONE || rel->max_hops != SKIP_ONE);
3297
3298 /* Size this hop's output for BOTH writers without dropping any row: the
3299 * expansion helpers emit at most max_new = bind_cap*10 rows (they stop at
3300 * max_new), and the OPTIONAL fallback emits at most one row per source
3301 * (<= *bind_count). A source either matches (feeds the expansion) or takes
3302 * the fallback, never both, so the two counts are additive and bounded by
3303 * max_new + *bind_count. Computed in size_t so the product cannot overflow.
3304 * The previous "+ 1" sizing fit only a SINGLE fallback row after a
3305 * saturated expansion; a second one ran off the end (heap OOB, CWE-787). */
3306 size_t alloc_n = (size_t)*bind_cap * (size_t)CYP_GROWTH_10 + (size_t)*bind_count;
3307 binding_t *new_bindings = malloc(alloc_n * sizeof(binding_t));
3308 if (!new_bindings) {
3309 return; /* OOM: leave existing bindings untouched rather than corrupt */
3310 }
3311 int new_count = 0;
3312
3313 for (int bi = 0; bi < *bind_count; bi++) {
3314 if ((bi & CYPHER_DEADLINE_CHECK_MASK) == 0 && cypher_deadline_exceeded()) {
3315 break;
3316 }
3317 binding_t *b = &(*bindings)[bi];
3318 cbm_node_t *src = binding_get(b, *var_name);
3319 if (!src) {
3320 continue;
3321 }
3322
3323 int match_count = 0;
3324
3325 int max_new = *bind_cap * CYP_GROWTH_10;
3326 if (is_variable_length) {
3327 expand_var_length(store, rel, target_node, b, src, to_var, new_bindings, &new_count,
3328 max_new, &match_count);
3329 } else {
3330 expand_fixed_length(store, rel, target_node, b, src, to_var, new_bindings,
3331 &new_count, max_new, &match_count);
3332 }
3333
3334 /* OPTIONAL MATCH: no expansion for this source, so keep the binding
3335 * with the target unbound (projection renders it ""). The buffer is
3336 * sized max_new + *bind_count precisely so every such fallback row has
3337 * a slot — no guard needed, and no OPTIONAL no-match row is dropped. */
3338 if (is_optional && match_count == 0) {
3339 binding_t nb = {0};
3340 binding_copy(&nb, b);

Callers 3

cross_join_with_relsFunction · 0.85
execute_singleFunction · 0.85

Calls 6

cypher_deadline_exceededFunction · 0.85
binding_getFunction · 0.85
expand_var_lengthFunction · 0.85
expand_fixed_lengthFunction · 0.85
binding_copyFunction · 0.85
binding_freeFunction · 0.85

Tested by

no test coverage detected