@@ -2500,6 +2500,112 @@ TEST(tool_bad_project_name_no_overflow_issue235) {
25002500}
25012501#undef ISSUE235_DBNAME
25022502
2503+ /* Issue #235 (follow-up): with many long-named projects indexed,
2504+ * collect_db_project_names overflowed projects[CBM_SZ_4K] and truncated the
2505+ * LAST name MID-TOKEN, then clamped offset to out_sz-1 — emitting malformed,
2506+ * unterminated JSON like
2507+ * ...,"available_projects":["a",...,"vjson_49_bbb],"count":50}
2508+ * (unclosed string + unclosed array). build_project_list_error wrapped that
2509+ * invalid body into the tool error, so a "project not found" reply was NOT
2510+ * valid JSON once enough projects were indexed.
2511+ *
2512+ * Reproduce-first: fill an isolated cache dir with enough long INTERNAL-named
2513+ * dbs to overflow the 4 KB buffer, hit the bad-project path, then assert the
2514+ * ERROR BODY (the inner MCP text content) parses as valid JSON and that
2515+ * available_projects is a JSON array whose length == count. RED on the
2516+ * truncating code (yyjson_read returns NULL on the mid-token cut); GREEN after
2517+ * the element-boundary fix, which only ever writes whole "name" tokens. */
2518+ #define BADPROJ_JSON_DBNAME (buf , dir , i ) \
2519+ snprintf((buf), sizeof(buf), \
2520+ "%s/vjson_%02d_aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" \
2521+ "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.db", \
2522+ (dir), (i))
2523+ TEST (tool_bad_project_error_valid_json_issue235 ) {
2524+ char cache [256 ];
2525+ snprintf (cache , sizeof (cache ), "/tmp/cbm-badproj-vjson-XXXXXX" );
2526+ if (!cbm_mkdtemp (cache )) {
2527+ PASS (); /* skip if mkdtemp fails */
2528+ }
2529+
2530+ const char * saved = getenv ("CBM_CACHE_DIR" );
2531+ char * saved_copy = saved ? strdup (saved ) : NULL ;
2532+ cbm_setenv ("CBM_CACHE_DIR" , cache , 1 );
2533+
2534+ /* 50 * ~120-char INTERNAL names >> 4 KB → the available_projects buffer
2535+ * overflows and the last name is cut mid-token on the unfixed code. */
2536+ enum { BADPROJ_N = 50 };
2537+ for (int i = 0 ; i < BADPROJ_N ; i ++ ) {
2538+ char name [512 ];
2539+ BADPROJ_JSON_DBNAME (name , cache , i );
2540+ char iname [256 ];
2541+ snprintf (iname , sizeof (iname ),
2542+ "vjson_%02d_bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
2543+ "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb" ,
2544+ i );
2545+ cbm_store_t * st = cbm_store_open_path (name );
2546+ if (st ) {
2547+ cbm_store_upsert_project (st , iname , cache );
2548+ cbm_store_close (st );
2549+ }
2550+ }
2551+
2552+ cbm_mcp_server_t * srv = cbm_mcp_server_new (NULL );
2553+ ASSERT_NOT_NULL (srv );
2554+ char * resp = cbm_mcp_server_handle (
2555+ srv , "{\"jsonrpc\":\"2.0\",\"id\":1,\"method\":\"tools/call\",\"params\":{\"name\":"
2556+ "\"search_graph\",\"arguments\":{\"label\":\"Function\","
2557+ "\"project\":\"definitely-not-a-real-project-xyz\"}}}" );
2558+ ASSERT_NOT_NULL (resp );
2559+ ASSERT_NOT_NULL (strstr (resp , "not found" ));
2560+
2561+ /* The inner MCP text content is the error body built by
2562+ * build_project_list_error. Capture its validity BEFORE cleanup so a RED
2563+ * failure still restores the environment. */
2564+ char * body = extract_text_content (resp );
2565+ bool body_valid = false;
2566+ bool aps_ok = false; /* available_projects is an array whose len == count */
2567+ if (body ) {
2568+ yyjson_doc * bdoc = yyjson_read (body , strlen (body ), 0 );
2569+ if (bdoc ) {
2570+ body_valid = true;
2571+ yyjson_val * broot = yyjson_doc_get_root (bdoc );
2572+ yyjson_val * aps = yyjson_obj_get (broot , "available_projects" );
2573+ yyjson_val * cnt = yyjson_obj_get (broot , "count" );
2574+ if (aps && yyjson_is_arr (aps ) && cnt && yyjson_is_int (cnt )) {
2575+ aps_ok = (yyjson_arr_size (aps ) == (size_t )yyjson_get_int (cnt ));
2576+ }
2577+ yyjson_doc_free (bdoc );
2578+ }
2579+ }
2580+ free (body );
2581+ free (resp );
2582+ cbm_mcp_server_free (srv );
2583+
2584+ if (saved_copy ) {
2585+ cbm_setenv ("CBM_CACHE_DIR" , saved_copy , 1 );
2586+ free (saved_copy );
2587+ } else {
2588+ cbm_unsetenv ("CBM_CACHE_DIR" );
2589+ }
2590+ for (int i = 0 ; i < BADPROJ_N ; i ++ ) {
2591+ char name [512 ];
2592+ BADPROJ_JSON_DBNAME (name , cache , i );
2593+ cbm_unlink (name );
2594+ char side [540 ];
2595+ snprintf (side , sizeof (side ), "%s-wal" , name );
2596+ cbm_unlink (side );
2597+ snprintf (side , sizeof (side ), "%s-shm" , name );
2598+ cbm_unlink (side );
2599+ }
2600+ cbm_rmdir (cache );
2601+
2602+ /* RED on the unfixed code: mid-token truncation → invalid JSON body. */
2603+ ASSERT_TRUE (body_valid );
2604+ ASSERT_TRUE (aps_ok );
2605+ PASS ();
2606+ }
2607+ #undef BADPROJ_JSON_DBNAME
2608+
25032609/* ── #704: project resolution must key on the db's INTERNAL project name ──
25042610 *
25052611 * Issue #704: project resolution is registry-less and filename-addressed.
@@ -3085,5 +3191,6 @@ SUITE(mcp) {
30853191 RUN_TEST (snippet_include_neighbors_enabled );
30863192 RUN_TEST (snippet_source_invalid_utf8 );
30873193 RUN_TEST (tool_bad_project_name_no_overflow_issue235 );
3194+ RUN_TEST (tool_bad_project_error_valid_json_issue235 );
30883195 RUN_TEST (tool_resolve_store_by_internal_name_issue704 );
30893196}
0 commit comments