Skip to content

Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.

fix(cypher): refuse a WITH wider than a binding can carry

Joshua Richter426e415f5427
diff --git a/src/cypher/cypher.c b/src/cypher/cypher.c
index 82f365b62..0654cea70 100644
--- a/src/cypher/cypher.c
+++ b/src/cypher/cypher.c
@@ -1802,8 +1802,16 @@ static int parse_return_or_with(parser_t *p, cbm_return_clause_t **out, bool is_
/* Projection is materialized per row into fixed-width stack arrays sized at
* CBM_SZ_32 columns (execute_return_simple and its siblings). Bound the
* parsed item count to that width so an over-wide RETURN is rejected here
- * instead of writing past those arrays downstream. */
- if (r->count > CBM_SZ_32) {
+ * instead of writing past those arrays downstream.
+ *
+ * WITH is bounded tighter, by CYP_MAX_VARS. Every item a WITH projects
+ * becomes one variable of the binding that carries the rest of the query,
+ * and binding_t holds exactly CYP_MAX_VARS variables. A wider WITH used to
+ * parse, then lose every alias past the 16th in with_add_vbinding_var and
+ * answer with silently blank columns. Refuse it here, the same way an
+ * over-wide RETURN is refused, so the caller sees an error instead of a
+ * short or empty result. */
+ if (r->count > (is_with ? CYP_MAX_VARS : CBM_SZ_32)) {
free_return_clause(r);
return CBM_NOT_FOUND;
}
@@ -4286,6 +4294,8 @@ static void execute_return_star_after_with(cbm_query_t *q, binding_t *bindings,
cbm_return_clause_t *wc = q->with_clause;
char name_bufs[CYP_MAX_VARS][CBM_SZ_128];
const char *cols[CYP_MAX_VARS];
+ /* parse_return_or_with refuses a WITH wider than CYP_MAX_VARS, so this
+ * clamp cannot fire. It stays as the bound this function relies on. */
int col_n = wc->count < CYP_MAX_VARS ? wc->count : CYP_MAX_VARS;
for (int i = 0; i < col_n; i++) {
cols[i] = resolve_item_alias(&wc->items[i], name_bufs[i], sizeof(name_bufs[i]));
diff --git a/tests/test_cypher.c b/tests/test_cypher.c
index 32f9568fe..bf4770c02 100644
--- a/tests/test_cypher.c
+++ b/tests/test_cypher.c
@@ -3026,6 +3026,45 @@ TEST(cypher_return_star_after_with_names_aliases) {
PASS();
}
+TEST(cypher_wide_with_refused_not_truncated) {
+ /* Every item a WITH projects becomes one variable of the binding that
+ * carries the rest of the query, and a binding holds CYP_MAX_VARS (16) of
+ * them. A 20-alias WITH used to parse, drop aliases 17 to 20 inside
+ * with_add_vbinding_var, and answer RETURN * with 16 columns and no error —
+ * a short result the caller could not tell from a complete one. It has to
+ * be refused at parse time instead. */
+ char query[1024];
+ int off = snprintf(query, sizeof(query), "MATCH (f:Function) WITH ");
+ for (int i = 0; i < 20; i++) { /* 20 > CYP_MAX_VARS (16) */
+ off += snprintf(query + off, sizeof(query) - (size_t)off, "%sf.name AS c%d", i ? ", " : "",
+ i);
+ }
+ snprintf(query + off, sizeof(query) - (size_t)off, " RETURN *");
+
+ cbm_store_t *s = setup_cypher_store();
+ cbm_cypher_result_t r = {0};
+ int rc = cbm_cypher_execute(s, query, "test", 0, &r);
+ ASSERT_TRUE(rc != 0); /* refused, not silently narrowed to 16 columns */
+ cbm_cypher_result_free(&r);
+
+ /* The width just under the bound still works, so the guard rejects only
+ * what the binding genuinely cannot carry. */
+ char ok_query[1024];
+ off = snprintf(ok_query, sizeof(ok_query), "MATCH (f:Function) WITH ");
+ for (int i = 0; i < 16; i++) {
+ off += snprintf(ok_query + off, sizeof(ok_query) - (size_t)off, "%sf.name AS c%d",
+ i ? ", " : "", i);
+ }
+ snprintf(ok_query + off, sizeof(ok_query) - (size_t)off, " RETURN *");
+ cbm_cypher_result_t r16 = {0};
+ ASSERT_EQ(cbm_cypher_execute(s, ok_query, "test", 0, &r16), 0);
+ ASSERT_EQ(r16.col_count, 16);
+ cbm_cypher_result_free(&r16);
+
+ cbm_store_close(s);
+ PASS();
+}
+
TEST(cypher_parse_neq) {
cbm_query_t *q = NULL;
char *err = NULL;
@@ -4336,6 +4375,7 @@ SUITE(cypher) {
RUN_TEST(cypher_exec_return_star);
RUN_TEST(cypher_return_star_dedups_repeated_pattern_var);
RUN_TEST(cypher_return_star_after_with_names_aliases);
+ RUN_TEST(cypher_wide_with_refused_not_truncated);
RUN_TEST(cypher_parse_neq);
RUN_TEST(cypher_parse_in);
RUN_TEST(cypher_parse_is_null);