Skip to content

Commit 4e3c417

Browse files
authored
Merge pull request DeusData#735 from DeusData/fix/available-projects-json-truncation
fix(mcp): keep available_projects error JSON valid when many projects (DeusData#235)
2 parents 4a42285 + d4c0b6b commit 4e3c417

2 files changed

Lines changed: 119 additions & 7 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,17 +1054,22 @@ static int collect_db_project_names(const char *dir_path, char *out, size_t out_
10541054
if (!db_internal_project_name(full_path, iname, sizeof(iname), NULL)) {
10551055
continue;
10561056
}
1057-
if ((size_t)offset >= out_sz)
1058-
break; /* bounds check before write */
1059-
if (count > 0 && offset < (int)out_sz - MCP_SEPARATOR) {
1057+
/* Element-boundary write: only emit this name if the WHOLE element —
1058+
* optional leading comma + "iname" — plus the NUL fits in what remains.
1059+
* Never truncate mid-token; a partial name would corrupt the JSON array
1060+
* (issue #235). Stop cleanly at the last name that fits: the array then
1061+
* always holds complete names and `count` == its length. */
1062+
size_t off = (size_t)offset;
1063+
size_t need = strlen(iname) + 2 /* quotes */ + (count > 0 ? 1u : 0u) /* comma */;
1064+
if (off + need + 1 > out_sz) {
1065+
break; /* would not fit entirely — stop at this element boundary */
1066+
}
1067+
if (count > 0) {
10601068
out[offset++] = ',';
10611069
}
10621070
int wrote = snprintf(out + offset, out_sz - (size_t)offset, "\"%s\"", iname);
10631071
if (wrote > 0) {
1064-
offset += wrote;
1065-
if ((size_t)offset >= out_sz) {
1066-
offset = (int)out_sz - 1; /* clamp on truncation */
1067-
}
1072+
offset += wrote; /* guaranteed to fit (checked above) — no truncation */
10681073
}
10691074
count++;
10701075
}

‎tests/test_mcp.c‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)