Skip to content

Commit e548d97

Browse files
committed
fix(mcp): free the empty name-lookup result before trace_path's QN fallback
handle_trace_call_path first looks the function up by bare name (cbm_store_find_nodes_by_name). find_nodes_generic allocates its initial 16-slot array before stepping the statement and hands it back even when zero rows match. When the bare name misses and the qualified_name fallback then resolves the node, the handler overwrote `nodes` with a fresh one-element array, leaking the empty 1 KB array on every trace_path call made with a qualified name - for the whole lifetime of the daemon. The malloc-failure branch of the fallback lost the same array. Release the zero-row array before the fallback replaces the pointer. The other find_nodes_* callers (get_code_snippet outline, search_code grep classification, detect_changes seeding, Cypher label scans) already free the container on zero rows; the not-found path in this handler did too. Proof (macOS leak lane, Homebrew clang 22.1.8, `make -f Makefile.cbm test-lsan LSAN_SUITES=mcp`): - with the new test, without the fix: 322 passed, then "ERROR: LeakSanitizer: detected memory leaks - Direct leak of 1024 byte(s) in 1 object(s) allocated from find_nodes_generic store.c:2771 <- handle_trace_call_path mcp.c:9176 <- test_tool_trace_call_path_qn_fallback_frees_name_miss", exit 1 (the only leak in the suite). - with the fix: 322 passed, 4 skipped, no leak report, exit 0. The new test pins its own precondition (the bare-name lookup of the qualified name returns zero rows) and that the trace resolves through the fallback, so it keeps exercising this path. LSan (default-on under Linux ASan, test-lsan on macOS) turns any regression red. Found while attributing CI on DeusData#2305. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 80eb92a commit e548d97

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9178,6 +9178,9 @@ static char *handle_trace_call_path(cbm_mcp_server_t *srv, const char *args) {
91789178
if (node_count == 0) {
91799179
cbm_node_t qn_node = {0};
91809180
if (cbm_store_find_node_by_qn(store, project, func_name, &qn_node) == CBM_STORE_OK) {
9181+
/* A zero-row name lookup still returns its allocated (empty)
9182+
* array; release it before the fallback replaces the pointer. */
9183+
cbm_store_free_nodes(nodes, 0);
91819184
nodes = malloc(sizeof(cbm_node_t));
91829185
if (nodes) {
91839186
nodes[0] = qn_node;

‎tests/test_mcp.c‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7187,6 +7187,67 @@ TEST(tool_trace_call_path_prefers_definition) {
71877187
PASS();
71887188
}
71897189

7190+
/* Leak regression: trace_path by a qualified name resolves only through the
7191+
* qualified_name fallback. The bare-name lookup that precedes it returns a
7192+
* heap array even for zero rows (find_nodes_generic allocates its initial
7193+
* capacity up front), and the fallback used to overwrite that pointer —
7194+
* leaking ~1 KB per call for the daemon's lifetime. The sanitizer lanes
7195+
* (LSan: default-on under Linux ASan, `make -f Makefile.cbm test-lsan` on
7196+
* macOS) report the leak at exit; the assertions pin that this test really
7197+
* takes the fallback path. */
7198+
TEST(tool_trace_call_path_qn_fallback_frees_name_miss) {
7199+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
7200+
cbm_store_t *st = cbm_mcp_server_store(srv);
7201+
const char *proj = "qnfb-proj";
7202+
const char *qn = "qnfb-proj.src.qnfb_entry";
7203+
cbm_mcp_server_set_project(srv, proj);
7204+
cbm_store_upsert_project(st, proj, "/tmp/qnfb");
7205+
cbm_node_t entry = {.project = proj,
7206+
.label = "Function",
7207+
.name = "qnfb_entry",
7208+
.qualified_name = qn,
7209+
.file_path = "src/qnfb.c",
7210+
.start_line = 1,
7211+
.end_line = 10};
7212+
cbm_node_t callee = {.project = proj,
7213+
.label = "Function",
7214+
.name = "qnfb_callee",
7215+
.qualified_name = "qnfb-proj.src.qnfb_callee",
7216+
.file_path = "src/qnfb.c",
7217+
.start_line = 20,
7218+
.end_line = 30};
7219+
int64_t id_entry = cbm_store_upsert_node(st, &entry);
7220+
int64_t id_callee = cbm_store_upsert_node(st, &callee);
7221+
ASSERT_GT(id_entry, 0);
7222+
ASSERT_GT(id_callee, 0);
7223+
cbm_edge_t e = {
7224+
.project = proj, .source_id = id_entry, .target_id = id_callee, .type = "CALLS"};
7225+
cbm_store_insert_edge(st, &e);
7226+
7227+
/* Precondition: the qualified name is NOT a bare name, so the handler's
7228+
* first lookup misses and the fallback is the only way to resolve it. */
7229+
cbm_node_t *by_name = NULL;
7230+
int by_name_count = -1;
7231+
ASSERT_EQ(cbm_store_find_nodes_by_name(st, proj, qn, &by_name, &by_name_count), CBM_STORE_OK);
7232+
ASSERT_EQ(by_name_count, 0);
7233+
cbm_store_free_nodes(by_name, by_name_count);
7234+
7235+
char *resp = cbm_mcp_server_handle(
7236+
srv, "{\"jsonrpc\":\"2.0\",\"id\":63,\"method\":\"tools/call\","
7237+
"\"params\":{\"name\":\"trace_call_path\",\"arguments\":{\"function_name\":"
7238+
"\"qnfb-proj.src.qnfb_entry\",\"project\":\"qnfb-proj\",\"direction\":\"outbound\"}}}");
7239+
ASSERT_NOT_NULL(resp);
7240+
char *inner = extract_text_content(resp);
7241+
ASSERT_NOT_NULL(inner);
7242+
ASSERT_NULL(strstr(inner, "function not found"));
7243+
/* resolved through the fallback -> its outbound CALLS edge shows */
7244+
ASSERT_NOT_NULL(strstr(inner, "qnfb_callee"));
7245+
free(inner);
7246+
free(resp);
7247+
cbm_mcp_server_free(srv);
7248+
PASS();
7249+
}
7250+
71907251
/* CONTRACT PIN for the closed strategy vocabulary published by
71917252
* trace_path(include_evidence:true).
71927253
*
@@ -20638,6 +20699,7 @@ SUITE(mcp) {
2063820699
RUN_TEST(tool_trace_reports_engine_saturation_as_lower_bound);
2063920700
RUN_TEST(store_bfs_edge_data_is_skippable_and_bounded);
2064020701
RUN_TEST(tool_trace_call_path_prefers_definition);
20702+
RUN_TEST(tool_trace_call_path_qn_fallback_frees_name_miss);
2064120703
RUN_TEST(trace_evidence_strategy_class_vocabulary_is_closed);
2064220704
RUN_TEST(tool_trace_path_evidence_is_opt_in_and_class_mapped);
2064320705
RUN_TEST(tool_trace_path_evidence_columns_match_header_issue1542);

0 commit comments

Comments
 (0)