Skip to content

Commit fc70534

Browse files
authored
Merge pull request DeusData#2425 from DeusData/fix/trace-path-qn-fallback-leak
fix(mcp): free the empty name-lookup result before trace_path's QN fallback
2 parents bedbc0a + e548d97 commit fc70534

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
@@ -9284,6 +9284,9 @@ static char *handle_trace_call_path(cbm_mcp_server_t *srv, const char *args) {
92849284
if (node_count == 0) {
92859285
cbm_node_t qn_node = {0};
92869286
if (cbm_store_find_node_by_qn(store, project, func_name, &qn_node) == CBM_STORE_OK) {
9287+
/* A zero-row name lookup still returns its allocated (empty)
9288+
* array; release it before the fallback replaces the pointer. */
9289+
cbm_store_free_nodes(nodes, 0);
92879290
nodes = malloc(sizeof(cbm_node_t));
92889291
if (nodes) {
92899292
nodes[0] = qn_node;

‎tests/test_mcp.c‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7264,6 +7264,67 @@ TEST(tool_trace_call_path_prefers_definition) {
72647264
PASS();
72657265
}
72667266

7267+
/* Leak regression: trace_path by a qualified name resolves only through the
7268+
* qualified_name fallback. The bare-name lookup that precedes it returns a
7269+
* heap array even for zero rows (find_nodes_generic allocates its initial
7270+
* capacity up front), and the fallback used to overwrite that pointer —
7271+
* leaking ~1 KB per call for the daemon's lifetime. The sanitizer lanes
7272+
* (LSan: default-on under Linux ASan, `make -f Makefile.cbm test-lsan` on
7273+
* macOS) report the leak at exit; the assertions pin that this test really
7274+
* takes the fallback path. */
7275+
TEST(tool_trace_call_path_qn_fallback_frees_name_miss) {
7276+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
7277+
cbm_store_t *st = cbm_mcp_server_store(srv);
7278+
const char *proj = "qnfb-proj";
7279+
const char *qn = "qnfb-proj.src.qnfb_entry";
7280+
cbm_mcp_server_set_project(srv, proj);
7281+
cbm_store_upsert_project(st, proj, "/tmp/qnfb");
7282+
cbm_node_t entry = {.project = proj,
7283+
.label = "Function",
7284+
.name = "qnfb_entry",
7285+
.qualified_name = qn,
7286+
.file_path = "src/qnfb.c",
7287+
.start_line = 1,
7288+
.end_line = 10};
7289+
cbm_node_t callee = {.project = proj,
7290+
.label = "Function",
7291+
.name = "qnfb_callee",
7292+
.qualified_name = "qnfb-proj.src.qnfb_callee",
7293+
.file_path = "src/qnfb.c",
7294+
.start_line = 20,
7295+
.end_line = 30};
7296+
int64_t id_entry = cbm_store_upsert_node(st, &entry);
7297+
int64_t id_callee = cbm_store_upsert_node(st, &callee);
7298+
ASSERT_GT(id_entry, 0);
7299+
ASSERT_GT(id_callee, 0);
7300+
cbm_edge_t e = {
7301+
.project = proj, .source_id = id_entry, .target_id = id_callee, .type = "CALLS"};
7302+
cbm_store_insert_edge(st, &e);
7303+
7304+
/* Precondition: the qualified name is NOT a bare name, so the handler's
7305+
* first lookup misses and the fallback is the only way to resolve it. */
7306+
cbm_node_t *by_name = NULL;
7307+
int by_name_count = -1;
7308+
ASSERT_EQ(cbm_store_find_nodes_by_name(st, proj, qn, &by_name, &by_name_count), CBM_STORE_OK);
7309+
ASSERT_EQ(by_name_count, 0);
7310+
cbm_store_free_nodes(by_name, by_name_count);
7311+
7312+
char *resp = cbm_mcp_server_handle(
7313+
srv, "{\"jsonrpc\":\"2.0\",\"id\":63,\"method\":\"tools/call\","
7314+
"\"params\":{\"name\":\"trace_call_path\",\"arguments\":{\"function_name\":"
7315+
"\"qnfb-proj.src.qnfb_entry\",\"project\":\"qnfb-proj\",\"direction\":\"outbound\"}}}");
7316+
ASSERT_NOT_NULL(resp);
7317+
char *inner = extract_text_content(resp);
7318+
ASSERT_NOT_NULL(inner);
7319+
ASSERT_NULL(strstr(inner, "function not found"));
7320+
/* resolved through the fallback -> its outbound CALLS edge shows */
7321+
ASSERT_NOT_NULL(strstr(inner, "qnfb_callee"));
7322+
free(inner);
7323+
free(resp);
7324+
cbm_mcp_server_free(srv);
7325+
PASS();
7326+
}
7327+
72677328
/* CONTRACT PIN for the closed strategy vocabulary published by
72687329
* trace_path(include_evidence:true).
72697330
*
@@ -21110,6 +21171,7 @@ SUITE(mcp) {
2111021171
RUN_TEST(tool_trace_reports_engine_saturation_as_lower_bound);
2111121172
RUN_TEST(store_bfs_edge_data_is_skippable_and_bounded);
2111221173
RUN_TEST(tool_trace_call_path_prefers_definition);
21174+
RUN_TEST(tool_trace_call_path_qn_fallback_frees_name_miss);
2111321175
RUN_TEST(trace_evidence_strategy_class_vocabulary_is_closed);
2111421176
RUN_TEST(tool_trace_path_evidence_is_opt_in_and_class_mapped);
2111521177
RUN_TEST(tool_trace_path_evidence_columns_match_header_issue1542);

0 commit comments

Comments
 (0)