Skip to content

Commit 77f8a92

Browse files
author
Your Name
committed
fix(mcp): use strcpy variants in detect_changes to prevent use-after-free
detect_changes was using yyjson_mut_arr_add_str / yyjson_mut_obj_add_str which borrow pointers. The file name came from a stack buffer reused each fgets() iteration, and node names were freed by cbm_store_free_nodes before serialization. This caused corrupted output with null bytes embedded in filenames (e.g. 'CLAUDE.md\0\0\0ings.json'). Switch to yyjson_mut_arr_add_strcpy / yyjson_mut_obj_add_strcpy which copy the strings into yyjson's internal allocator, making them safe across the buffer reuse and free boundaries.
1 parent b9f4c82 commit 77f8a92

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

src/mcp/mcp.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2602,7 +2602,10 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
26022602
continue;
26032603
}
26042604

2605-
yyjson_mut_arr_add_str(doc, changed, line);
2605+
/* Use strcpy variants: line is a stack buffer reused each iteration,
2606+
* and node strings are freed by cbm_store_free_nodes below.
2607+
* yyjson_mut_*_add_str only borrows pointers — strcpy makes copies. */
2608+
yyjson_mut_arr_add_strcpy(doc, changed, line);
26062609
file_count++;
26072610

26082611
/* Find symbols defined in this file */
@@ -2614,9 +2617,9 @@ static char *handle_detect_changes(cbm_mcp_server_t *srv, const char *args) {
26142617
if (nodes[i].label && strcmp(nodes[i].label, "File") != 0 &&
26152618
strcmp(nodes[i].label, "Folder") != 0 && strcmp(nodes[i].label, "Project") != 0) {
26162619
yyjson_mut_val *item = yyjson_mut_obj(doc);
2617-
yyjson_mut_obj_add_str(doc, item, "name", nodes[i].name ? nodes[i].name : "");
2618-
yyjson_mut_obj_add_str(doc, item, "label", nodes[i].label);
2619-
yyjson_mut_obj_add_str(doc, item, "file", line);
2620+
yyjson_mut_obj_add_strcpy(doc, item, "name", nodes[i].name ? nodes[i].name : "");
2621+
yyjson_mut_obj_add_strcpy(doc, item, "label", nodes[i].label);
2622+
yyjson_mut_obj_add_strcpy(doc, item, "file", line);
26202623
yyjson_mut_arr_add_val(impacted, item);
26212624
}
26222625
}

0 commit comments

Comments
 (0)