@@ -3202,6 +3202,124 @@ TEST(cypher_wide_with_refused_not_truncated) {
32023202 ASSERT_EQ (cbm_cypher_execute (s , ok_query , "test" , 0 , & r16 ), 0 );
32033203 ASSERT_EQ (r16 .col_count , 16 );
32043204 cbm_cypher_result_free (& r16 );
3205+ cbm_store_close (s );
3206+ PASS ();
3207+ }
3208+
3209+ /* Build "MATCH (a0:NoSuchLabelXYZ)-[:CALLS]->(a1)-…->(aN-1)" into buf. The label
3210+ * matches nothing, so any query built on it is instant and needs no fixture. */
3211+ static void build_node_chain (char * buf , size_t buf_sz , int nodes ) {
3212+ int off = snprintf (buf , buf_sz , "MATCH (a0:NoSuchLabelXYZ)" );
3213+ for (int i = 1 ; i < nodes ; i ++ ) {
3214+ off += snprintf (buf + off , buf_sz - (size_t )off , "-[:CALLS]->(a%d)" , i );
3215+ }
3216+ }
3217+
3218+ TEST (cypher_wide_pattern_refused ) {
3219+ /* A binding holds CYP_MAX_VARS (16) node variables, and binding_set drops
3220+ * the 17th without a word. The query then answers a column of empty strings
3221+ * for every name it could not bind, which reads as "the graph holds no such
3222+ * data". Refuse the query instead of answering it wrong. */
3223+ cbm_store_t * s = setup_cypher_store ();
3224+ char query [2048 ];
3225+
3226+ build_node_chain (query , sizeof (query ), 20 ); /* 20 > CYP_MAX_VARS */
3227+ strncat (query , " RETURN a0.name" , sizeof (query ) - strlen (query ) - 1 );
3228+ cbm_cypher_result_t wide = {0 };
3229+ ASSERT_TRUE (cbm_cypher_execute (s , query , "test" , 0 , & wide ) != 0 );
3230+ ASSERT_NOT_NULL (wide .error );
3231+ ASSERT_TRUE (strstr (wide .error , "node" ) != NULL ); /* says which limit was passed */
3232+ cbm_cypher_result_free (& wide );
3233+
3234+ /* The width right at the bound still runs, so the guard refuses only what a
3235+ * binding genuinely cannot hold. */
3236+ build_node_chain (query , sizeof (query ), 16 );
3237+ strncat (query , " RETURN a0.name" , sizeof (query ) - strlen (query ) - 1 );
3238+ cbm_cypher_result_t ok = {0 };
3239+ ASSERT_EQ (cbm_cypher_execute (s , query , "test" , 0 , & ok ), 0 );
3240+ cbm_cypher_result_free (& ok );
3241+
3242+ cbm_store_close (s );
3243+ PASS ();
3244+ }
3245+
3246+ TEST (cypher_wide_edge_pattern_refused ) {
3247+ /* Same shape on the edge table, where binding_set_edge stops at
3248+ * CYP_MAX_EDGE_VARS (8). Only NAMED relationships take a slot. */
3249+ cbm_store_t * s = setup_cypher_store ();
3250+ char query [2048 ];
3251+ int off = snprintf (query , sizeof (query ), "MATCH (a0:NoSuchLabelXYZ)" );
3252+ for (int i = 1 ; i <= 9 ; i ++ ) { /* 9 > CYP_MAX_EDGE_VARS */
3253+ off += snprintf (query + off , sizeof (query ) - (size_t )off , "-[r%d:CALLS]->(a%d)" , i , i );
3254+ }
3255+ snprintf (query + off , sizeof (query ) - (size_t )off , " RETURN a0.name" );
3256+ cbm_cypher_result_t wide = {0 };
3257+ ASSERT_TRUE (cbm_cypher_execute (s , query , "test" , 0 , & wide ) != 0 );
3258+ ASSERT_NOT_NULL (wide .error );
3259+ ASSERT_TRUE (strstr (wide .error , "edge" ) != NULL );
3260+ cbm_cypher_result_free (& wide );
3261+ cbm_store_close (s );
3262+ PASS ();
3263+ }
3264+
3265+ TEST (cypher_unnamed_head_takes_a_slot ) {
3266+ /* The head of the first pattern is bound whether the query names it or not:
3267+ * execute_single falls back to the synthetic name "_n0". So an unnamed head
3268+ * plus CYP_MAX_VARS (16) named nodes needs 17 slots and only 16 exist. Before
3269+ * the fix, the capacity check counted names alone, let this query through,
3270+ * and binding_set dropped the 16th name without a word — a0..a14 answered and
3271+ * a15 came back empty. Refuse it instead. */
3272+ cbm_store_t * s = setup_cypher_store ();
3273+ char query [2048 ];
3274+ int off = snprintf (query , sizeof (query ), "MATCH (:NoSuchLabelXYZ)" );
3275+ for (int i = 0 ; i < 16 ; i ++ ) { /* 16 named + the unnamed head = 17 */
3276+ off += snprintf (query + off , sizeof (query ) - (size_t )off , "-[:CALLS]->(a%d)" , i );
3277+ }
3278+ snprintf (query + off , sizeof (query ) - (size_t )off , " RETURN a0.name" );
3279+ cbm_cypher_result_t wide = {0 };
3280+ ASSERT_TRUE (cbm_cypher_execute (s , query , "test" , 0 , & wide ) != 0 );
3281+ ASSERT_NOT_NULL (wide .error );
3282+ ASSERT_TRUE (strstr (wide .error , "node" ) != NULL );
3283+ cbm_cypher_result_free (& wide );
3284+
3285+ /* One name fewer fits exactly, so the guard still refuses only what a
3286+ * binding genuinely cannot hold. */
3287+ off = snprintf (query , sizeof (query ), "MATCH (:NoSuchLabelXYZ)" );
3288+ for (int i = 0 ; i < 15 ; i ++ ) {
3289+ off += snprintf (query + off , sizeof (query ) - (size_t )off , "-[:CALLS]->(a%d)" , i );
3290+ }
3291+ snprintf (query + off , sizeof (query ) - (size_t )off , " RETURN a0.name" );
3292+ cbm_cypher_result_t ok = {0 };
3293+ ASSERT_EQ (cbm_cypher_execute (s , query , "test" , 0 , & ok ), 0 );
3294+ cbm_cypher_result_free (& ok );
3295+
3296+ cbm_store_close (s );
3297+ PASS ();
3298+ }
3299+
3300+ TEST (cypher_scope_check_survives_wide_pattern ) {
3301+ /* Regression test for #1995. check_projection_scope models declared names in
3302+ * a fixed array and used to skip the check entirely when a query declared
3303+ * more than it held. So the same out-of-scope name was refused on a narrow
3304+ * query and quietly accepted on a wide one. Both must now be refused. */
3305+ cbm_store_t * s = setup_cypher_store ();
3306+ char query [4096 ];
3307+
3308+ build_node_chain (query , sizeof (query ), 10 );
3309+ strncat (query , " RETURN zzz.name" , sizeof (query ) - strlen (query ) - 1 );
3310+ cbm_cypher_result_t narrow = {0 };
3311+ ASSERT_TRUE (cbm_cypher_execute (s , query , "test" , 0 , & narrow ) != 0 );
3312+ ASSERT_NOT_NULL (narrow .error );
3313+ ASSERT_TRUE (strstr (narrow .error , "zzz" ) != NULL );
3314+ cbm_cypher_result_free (& narrow );
3315+
3316+ /* 35 declared names — this one used to answer a zzz.name column of nothing. */
3317+ build_node_chain (query , sizeof (query ), 35 );
3318+ strncat (query , " RETURN zzz.name" , sizeof (query ) - strlen (query ) - 1 );
3319+ cbm_cypher_result_t wide = {0 };
3320+ ASSERT_TRUE (cbm_cypher_execute (s , query , "test" , 0 , & wide ) != 0 );
3321+ ASSERT_NOT_NULL (wide .error );
3322+ cbm_cypher_result_free (& wide );
32053323
32063324 cbm_store_close (s );
32073325 PASS ();
@@ -4521,6 +4639,10 @@ SUITE(cypher) {
45214639 RUN_TEST (cypher_return_star_dedups_repeated_pattern_var );
45224640 RUN_TEST (cypher_return_star_after_with_names_aliases );
45234641 RUN_TEST (cypher_wide_with_refused_not_truncated );
4642+ RUN_TEST (cypher_wide_pattern_refused );
4643+ RUN_TEST (cypher_wide_edge_pattern_refused );
4644+ RUN_TEST (cypher_unnamed_head_takes_a_slot );
4645+ RUN_TEST (cypher_scope_check_survives_wide_pattern );
45244646 RUN_TEST (cypher_parse_neq );
45254647 RUN_TEST (cypher_parse_in );
45264648 RUN_TEST (cypher_parse_is_null );
0 commit comments