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): exclude entity-introspection funcs from WITH's bare-node id carry

Amir Fathi2f522b40cce3
diff --git a/src/cypher/cypher.c b/src/cypher/cypher.c
index bf8d3c38c..a1d7fa34b 100644
--- a/src/cypher/cypher.c
+++ b/src/cypher/cypher.c
@@ -3780,8 +3780,12 @@ static int with_agg_find_or_create(with_agg_t **aggs, int *agg_cnt, int *agg_cap
(*aggs)[found].group_vals[ci] = heap_strdup(v);
/* If this group item is a bare node variable, remember its id so the
* carried virtual var can re-fetch any property (group_vals holds only
- * the name). */
- if (!wc->items[ci].property && wc->items[ci].variable) {
+ * the name). Excludes entity-introspection funcs (labels/id/keys/
+ * properties): those project a scalar off the node (via project_item
+ * above), not the node itself, so the carried id must not be set or a
+ * later alias.property re-fetches the source node's real properties
+ * instead of returning empty for the non-node alias. */
+ if (!wc->items[ci].func && !wc->items[ci].property && wc->items[ci].variable) {
cbm_node_t *gn = binding_get(b, wc->items[ci].variable);
if (gn) {
(*aggs)[found].group_node_ids[ci] = gn->id;
diff --git a/tests/test_cypher.c b/tests/test_cypher.c
index 3d2aa0c4d..add3bdc3e 100644
--- a/tests/test_cypher.c
+++ b/tests/test_cypher.c
@@ -2749,6 +2749,31 @@ TEST(cypher_issue1111_with_type_count_group) {
PASS();
}
+/* #1111 follow-up (review from DeusData on #1221): with_agg_find_or_create's
+ * bare-node-carry check only tested `!property && variable`, so an entity-
+ * introspection alias like `labels(f) AS l` (variable set, property NULL, func
+ * set) was ALSO tagged with the source node's id. A later `l.file_path` then
+ * hit node_prop's stub re-fetch heuristic (id set, file_path/label both NULL on
+ * the virtual stub) and silently returned HandleOrder's real file_path instead
+ * of "" for the non-node alias `l`. */
+TEST(cypher_issue1111_with_scalar_func_alias_no_node_leak) {
+ cbm_store_t *s = setup_cypher_store();
+ cbm_cypher_result_t r = {0};
+ int rc = cbm_cypher_execute(s,
+ "MATCH (f:Function) WHERE f.name = \"HandleOrder\" "
+ "WITH labels(f) AS l, COUNT(*) AS c "
+ "RETURN l, l.file_path, c",
+ "test", 0, &r);
+ ASSERT_EQ(rc, 0);
+ ASSERT_EQ(r.row_count, 1);
+ ASSERT_STR_EQ(r.rows[0][0], "[\"Function\"]");
+ ASSERT_STR_EQ(r.rows[0][1], ""); /* was "handler.go" before the fix */
+ ASSERT_STR_EQ(r.rows[0][2], "1");
+ cbm_cypher_result_free(&r);
+ cbm_store_close(s);
+ PASS();
+}
+
TEST(cypher_exec_with_where) {
cbm_store_t *s = setup_cypher_store();
cbm_cypher_result_t r = {0};
@@ -3459,6 +3484,7 @@ SUITE(cypher) {
RUN_TEST(cypher_exec_with_rename);
RUN_TEST(cypher_exec_with_count);
RUN_TEST(cypher_issue1111_with_type_count_group);
+ RUN_TEST(cypher_issue1111_with_scalar_func_alias_no_node_leak);
RUN_TEST(cypher_exec_with_node_groupvar_prop);
RUN_TEST(cypher_exec_with_where);
RUN_TEST(cypher_exec_with_orderby_limit);