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.

cypher: bound RETURN/WITH projection width to the result column limit

Martin Vogele69e0844dbc1
diff --git a/src/cypher/cypher.c b/src/cypher/cypher.c
index 4f554ea4b..8f321f588 100644
--- a/src/cypher/cypher.c
+++ b/src/cypher/cypher.c
@@ -1652,6 +1652,16 @@ static int parse_return_or_with(parser_t *p, cbm_return_clause_t **out, bool is_
} while (check(p, TOK_COMMA));
+ /* 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) {
+ free(r->items);
+ free(r);
+ return CBM_NOT_FOUND;
+ }
+
tail:
/* Optional ORDER BY */
if (match(p, TOK_ORDER)) {
diff --git a/tests/test_cypher.c b/tests/test_cypher.c
index 5fe3cd592..9ab7e618e 100644
--- a/tests/test_cypher.c
+++ b/tests/test_cypher.c
@@ -10,6 +10,10 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
+#ifndef _WIN32
+#include <sys/wait.h> /* fork/waitpid crash-isolation for the projection-width guard */
+#include <unistd.h>
+#endif
/* ══════════════════════════════════════════════════════════════════
* LEXER TESTS
@@ -2578,10 +2582,10 @@ TEST(cypher_multi_prop_projection_no_alias) {
ASSERT_EQ(rc, 0);
ASSERT_EQ(r.row_count, 1);
ASSERT_EQ(r.col_count, 6);
- ASSERT_STR_EQ(r.rows[0][0], "1"); /* loop_depth — NOT the suffix transitive_loop_depth */
- ASSERT_STR_EQ(r.rows[0][1], "5"); /* transitive_loop_depth */
- ASSERT_STR_EQ(r.rows[0][2], "7"); /* cognitive */
- ASSERT_STR_EQ(r.rows[0][3], "3"); /* complexity */
+ ASSERT_STR_EQ(r.rows[0][0], "1"); /* loop_depth — NOT the suffix transitive_loop_depth */
+ ASSERT_STR_EQ(r.rows[0][1], "5"); /* transitive_loop_depth */
+ ASSERT_STR_EQ(r.rows[0][2], "7"); /* cognitive */
+ ASSERT_STR_EQ(r.rows[0][3], "3"); /* complexity */
ASSERT_STR_EQ(r.rows[0][4], "10"); /* start_line (computed) */
ASSERT_STR_EQ(r.rows[0][5], "42"); /* end_line (computed) — distinct from start_line */
cbm_cypher_result_free(&r);
@@ -2589,6 +2593,47 @@ TEST(cypher_multi_prop_projection_no_alias) {
PASS();
}
+/* Result projection writes into fixed-width per-row stack arrays
+ * (vals[CBM_SZ_32] / func_bufs[CBM_SZ_32][…] in execute_return_simple and its
+ * siblings), indexed by the parsed RETURN item count. The parser must bound
+ * that count to the array width; an over-wide RETURN has to be rejected, not
+ * allowed to run and write past the arrays. Drive a >32-column RETURN in a
+ * forked child so a stack overwrite (ASan abort, or a raw segfault) shows up
+ * as a killing signal instead of taking down the whole runner; the bounded
+ * path returns an ordinary error and the child exits cleanly. */
+TEST(cypher_wide_return_projection_bounded) {
+#ifdef _WIN32
+ SKIP_PLATFORM("fork crash-isolation is POSIX-only; the parse-time bound is platform-agnostic");
+#else
+ char query[4096];
+ int off = snprintf(query, sizeof(query), "MATCH (f:Function) RETURN ");
+ for (int i = 0; i < 48; i++) { /* 48 > CBM_SZ_32 (32) fixed columns */
+ off += snprintf(query + off, sizeof(query) - (size_t)off, "%sf.p%d", i ? ", " : "", i);
+ }
+ fflush(NULL);
+ pid_t pid = fork();
+ if (pid == 0) {
+ cbm_store_t *s = setup_cypher_store();
+ cbm_cypher_result_t r = {0};
+ (void)cbm_cypher_execute(s, query, "test", 0, &r);
+ cbm_cypher_result_free(&r);
+ cbm_store_close(s);
+ _exit(0); /* reached only when execution did NOT overflow */
+ }
+ ASSERT_TRUE(pid > 0);
+ int status = 0;
+ (void)waitpid(pid, &status, 0);
+ if (WIFSIGNALED(status)) {
+ char msg[96];
+ snprintf(msg, sizeof(msg), "wide RETURN killed by signal %d — projection stack overflow",
+ WTERMSIG(status));
+ FAIL(msg);
+ }
+ ASSERT_TRUE(WIFEXITED(status));
+ PASS();
+#endif
+}
+
/* ══════════════════════════════════════════════════════════════════ */
SUITE(cypher) {
@@ -2750,4 +2795,5 @@ SUITE(cypher) {
/* Phase 9: UNWIND */
RUN_TEST(cypher_parse_unwind);
RUN_TEST(cypher_parse_unwind_var);
+ RUN_TEST(cypher_wide_return_projection_bounded);
}