Skip to content

Commit fe8dd9d

Browse files
committed
fix(mcp): release path filters on search setup errors
Free the compiled path_filter on missing pattern, missing project, unknown project root, and failed scratch creation. Exercise the three argument and project validation exits with a valid filter for leak-sanitizer coverage. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 96c3f41 commit fe8dd9d

2 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎src/mcp/mcp.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14984,13 +14984,19 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
1498414984
path_filter = NULL;
1498514985

1498614986
if (!pattern) {
14987+
if (has_path_filter) {
14988+
cbm_regfree(&path_regex);
14989+
}
1498714990
free(project);
1498814991
free(file_pattern);
1498914992
return cbm_mcp_text_result("pattern is required", true);
1499014993
}
1499114994

1499214995
/* Project is required */
1499314996
if (!project) {
14997+
if (has_path_filter) {
14998+
cbm_regfree(&path_regex);
14999+
}
1499415000
free(pattern);
1499515001
free(file_pattern);
1499615002
char *_err = build_project_list_error("project is required");
@@ -15001,6 +15007,9 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
1500115007

1500215008
char *root_path = get_project_root(srv, project);
1500315009
if (!root_path) {
15010+
if (has_path_filter) {
15011+
cbm_regfree(&path_regex);
15012+
}
1500415013
free(pattern);
1500515014
free(project);
1500615015
free(file_pattern);
@@ -15094,6 +15103,9 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
1509415103
char errmsg[CBM_SZ_256];
1509515104
snprintf(errmsg, sizeof(errmsg), "search failed: cannot create temp file (%s)",
1509615105
strerror(errno));
15106+
if (has_path_filter) {
15107+
cbm_regfree(&path_regex);
15108+
}
1509715109
free(root_path);
1509815110
free(pattern);
1509915111
free(project);

‎tests/test_mcp.c‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9045,6 +9045,41 @@ TEST(tool_search_code_no_project) {
90459045
PASS();
90469046
}
90479047

9048+
/* search_code compiles path_filter into a regex before it checks pattern,
9049+
* project and the project root. Every one of those early error returns must
9050+
* release the compiled regex. The leak itself is only visible to a leak
9051+
* detector such as LeakSanitizer, so this test drives each return with a valid
9052+
* path_filter and asserts the error taken. */
9053+
TEST(search_code_early_errors_release_path_filter_regex) {
9054+
cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);
9055+
ASSERT_NOT_NULL(srv);
9056+
9057+
/* No pattern. */
9058+
char *resp = cbm_mcp_handle_tool(
9059+
srv, "search_code", "{\"project\":\"nonexistent\",\"path_filter\":\"^cbm_leak_probe/\"}");
9060+
bool pattern_error = resp && strstr(resp, "pattern is required") != NULL;
9061+
free(resp);
9062+
9063+
/* No project. */
9064+
resp = cbm_mcp_handle_tool(srv, "search_code",
9065+
"{\"pattern\":\"main\",\"path_filter\":\"^cbm_leak_probe/\"}");
9066+
bool project_error = resp && strstr(resp, "project is required") != NULL;
9067+
free(resp);
9068+
9069+
/* Unknown project, with a file_pattern alongside the path filter. */
9070+
resp = cbm_mcp_handle_tool(srv, "search_code",
9071+
"{\"pattern\":\"main\",\"project\":\"nonexistent\","
9072+
"\"file_pattern\":\"*.c\",\"path_filter\":\"^cbm_leak_probe/\"}");
9073+
bool root_error = resp && strstr(resp, "project not found or not indexed") != NULL;
9074+
free(resp);
9075+
9076+
cbm_mcp_server_free(srv);
9077+
ASSERT_TRUE(pattern_error);
9078+
ASSERT_TRUE(project_error);
9079+
ASSERT_TRUE(root_error);
9080+
PASS();
9081+
}
9082+
90489083
TEST(search_code_multi_word) {
90499084
char tmp[512];
90509085
cbm_mcp_server_t *srv = setup_snippet_server(tmp, sizeof(tmp));
@@ -21203,6 +21238,7 @@ SUITE(mcp) {
2120321238
RUN_TEST(tool_search_code_limit_declares_a_minimum_issue1511);
2120421239
RUN_TEST(tool_search_code_declares_independent_result_and_raw_content_paging);
2120521240
RUN_TEST(tool_search_code_no_project);
21241+
RUN_TEST(search_code_early_errors_release_path_filter_regex);
2120621242
RUN_TEST(search_code_multi_word);
2120721243
RUN_TEST(search_code_full_preserves_utf8_source);
2120821244
RUN_TEST(search_code_raw_match_preserves_utf8_content);

0 commit comments

Comments
 (0)