| 3672 | } |
| 3673 | |
| 3674 | static void expand_pattern_rels(cbm_store_t *store, cbm_pattern_t *pat, binding_t **bindings, |
| 3675 | int *bind_count, int *bind_cap, const char **var_name, |
| 3676 | bool is_optional) { |
| 3677 | for (int ri = 0; ri < pat->rel_count; ri++) { |
| 3678 | /* #601: stop expanding further hops once the wall-clock budget is spent |
| 3679 | * (an unbounded expansion is exactly what blows up here). */ |
| 3680 | if (cypher_deadline_exceeded()) { |
| 3681 | return; |
| 3682 | } |
| 3683 | cbm_rel_pattern_t *rel = &pat->rels[ri]; |
| 3684 | cbm_node_pattern_t *target_node = &pat->nodes[ri + SKIP_ONE]; |
| 3685 | const char *to_var = target_node->variable ? target_node->variable : "_n_t"; |
| 3686 | |
| 3687 | bool is_variable_length = (rel->min_hops != SKIP_ONE || rel->max_hops != SKIP_ONE); |
| 3688 | |
| 3689 | /* #1196: the hop's output buffer GROWS to hold every matched row — |
| 3690 | * the old bind_cap*10 ceiling silently dropped edges before WHERE and |
| 3691 | * aggregation, falsifying counts. binding_out_append handles growth |
| 3692 | * and the OPTIONAL fallback shares it, so no writer can run off the |
| 3693 | * end and no row class is dropped (the old fixed sizing had exactly |
| 3694 | * those two failure modes, CWE-787 and the OPTIONAL data loss). */ |
| 3695 | int new_cap = *bind_count > 0 ? *bind_count : CYP_GROWTH_10; |
| 3696 | binding_t *new_bindings = malloc((size_t)new_cap * sizeof(binding_t)); |
| 3697 | if (!new_bindings) { |
| 3698 | return; /* OOM: leave existing bindings untouched rather than corrupt */ |
| 3699 | } |
| 3700 | int new_count = 0; |
| 3701 | |
| 3702 | for (int bi = 0; bi < *bind_count; bi++) { |
| 3703 | if ((bi & CYPHER_DEADLINE_CHECK_MASK) == 0 && cypher_deadline_exceeded()) { |
| 3704 | break; |
| 3705 | } |
| 3706 | binding_t *b = &(*bindings)[bi]; |
| 3707 | cbm_node_t *src = binding_get(b, *var_name); |
| 3708 | if (!src) { |
| 3709 | continue; |
| 3710 | } |
| 3711 | |
| 3712 | int match_count = 0; |
| 3713 | |
| 3714 | if (is_variable_length) { |
| 3715 | expand_var_length(store, rel, target_node, b, src, to_var, &new_bindings, |
| 3716 | &new_count, &new_cap, &match_count); |
| 3717 | } else { |
| 3718 | expand_fixed_length(store, rel, target_node, b, src, to_var, &new_bindings, |
| 3719 | &new_count, &new_cap, &match_count); |
| 3720 | } |
| 3721 | |
| 3722 | /* OPTIONAL MATCH: no expansion for this source, so keep the binding |
| 3723 | * with the target unbound (projection renders it ""). The shared |
| 3724 | * growable append gives every fallback row a slot. */ |
| 3725 | if (is_optional && match_count == 0) { |
| 3726 | binding_t nb = {0}; |
| 3727 | binding_copy(&nb, b); |
| 3728 | /* Don't set to_var — it remains unbound; projection returns "" */ |
| 3729 | (void)binding_out_append(&new_bindings, &new_count, &new_cap, &nb); |
| 3730 | } |
| 3731 | } |
no test coverage detected