@@ -123,6 +123,7 @@ enum {
123123 * growing getline buffers without bound through ignored extension headers. */
124124#define MCP_MAX_MESSAGE_SIZE ((size_t)10U * 1024U * 1024U)
125125#define MCP_MAX_HEADER_SIZE ((size_t)8U * 1024U)
126+ #define MCP_SEARCH_OUTPUT_MAX ((size_t)64U * 1024U * 1024U)
126127
127128/* ── Helpers ────────────────────────────────────────────────────── */
128129
@@ -1575,6 +1576,7 @@ struct cbm_mcp_server {
15751576 void *quarantine_test_context;
15761577 cbm_mcp_command_test_hook_fn command_test_hook;
15771578 void *command_test_context;
1579+ size_t search_output_limit_override;
15781580 cbm_thread_t autoindex_tid;
15791581 bool autoindex_active; /* true if auto-index thread was started */
15801582
@@ -1873,6 +1875,12 @@ void cbm_mcp_server_set_command_test_hook(cbm_mcp_server_t *srv, cbm_mcp_command
18731875 srv->command_test_context = context;
18741876}
18751877
1878+ void cbm_mcp_server_set_search_output_limit_for_test(cbm_mcp_server_t *srv, size_t limit) {
1879+ if (srv) {
1880+ srv->search_output_limit_override = limit;
1881+ }
1882+ }
1883+
18761884/* ── Cache dir + project DB path helpers ───────────────────────── */
18771885
18781886/* Returns the cache directory. Writes to buf, returns buf for convenience. */
@@ -9779,6 +9787,32 @@ static bool compile_path_filter(const char *filter, cbm_regex_t *re) {
97799787 return cbm_regcomp(re, filter, CBM_REG_EXTENDED | CBM_REG_NOSUB) == CBM_REG_OK;
97809788}
97819789
9790+ static int mcp_run_shell_command_cancellable_bounded(cbm_mcp_server_t *srv, const char *command,
9791+ char output_path[CBM_SZ_2K],
9792+ size_t output_limit,
9793+ bool *output_limit_exceeded,
9794+ cbm_proc_result_t *result_out);
9795+
9796+ #ifdef _WIN32
9797+ static char *search_code_scan_error(search_scratch_t *scratch, const char *output_path,
9798+ bool has_path_filter, cbm_regex_t *path_regex, char *root_path,
9799+ char *pattern, char *project, char *file_pattern,
9800+ const char *message) {
9801+ if (output_path && output_path[0]) {
9802+ (void)cbm_unlink(output_path);
9803+ }
9804+ search_scratch_close(scratch);
9805+ if (has_path_filter) {
9806+ cbm_regfree(path_regex);
9807+ }
9808+ free(root_path);
9809+ free(pattern);
9810+ free(project);
9811+ free(file_pattern);
9812+ return cbm_mcp_text_result(message, true);
9813+ }
9814+ #endif
9815+
97829816static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
97839817 char *pattern = cbm_mcp_get_string_arg(args, "pattern");
97849818 char *project = get_project_arg(args);
@@ -9962,6 +9996,44 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
99629996 cbm_search_code_build_grep_cmd(cmd, sizeof(cmd), use_regex, scoped, file_pattern, tmpfile,
99639997 filelist, root_path);
99649998
9999+ #ifdef _WIN32
10000+ char output_path[CBM_SZ_2K] = {0};
10001+ cbm_proc_result_t scan_result = {0};
10002+ bool scan_output_exceeded = false;
10003+ size_t scan_output_limit = srv->search_output_limit_override
10004+ ? srv->search_output_limit_override
10005+ : MCP_SEARCH_OUTPUT_MAX;
10006+ int scan_run = mcp_run_shell_command_cancellable_bounded(
10007+ srv, cmd, output_path, scan_output_limit, &scan_output_exceeded, &scan_result);
10008+ if (scan_output_exceeded) {
10009+ char message[CBM_SZ_128];
10010+ snprintf(message, sizeof(message),
10011+ "search failed: output exceeded the %zu-byte safety limit", scan_output_limit);
10012+ return search_code_scan_error(&scratch, output_path, has_path_filter, &path_regex,
10013+ root_path, pattern, project, file_pattern, message);
10014+ }
10015+ bool scan_cancelled = scan_result.cancellation_requested || mcp_request_cancelled(srv);
10016+ if (scan_cancelled) {
10017+ return search_code_scan_error(&scratch, output_path, has_path_filter, &path_regex,
10018+ root_path, pattern, project, file_pattern,
10019+ "search_code cancelled for this request");
10020+ }
10021+ if (scan_run != 0) {
10022+ return search_code_scan_error(
10023+ &scratch, output_path, has_path_filter, &path_regex, root_path, pattern, project,
10024+ file_pattern, "search failed: the contained command could not complete");
10025+ }
10026+ FILE *fp = cbm_fopen(output_path, "rb");
10027+ if (!fp) {
10028+ return search_code_scan_error(&scratch, output_path, has_path_filter, &path_regex,
10029+ root_path, pattern, project, file_pattern,
10030+ "search failed: contained output could not be read");
10031+ }
10032+ gm = collect_grep_matches(fp, root_path, strlen(root_path), has_path_filter, &path_regex,
10033+ grep_limit, &gm_count);
10034+ (void)fclose(fp);
10035+ (void)cbm_unlink(output_path);
10036+ #else
996510037 FILE *fp = cbm_popen(cmd, "r");
996610038 if (!fp) {
996710039 search_scratch_close(&scratch);
@@ -9978,6 +10050,7 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
997810050 gm = collect_grep_matches(fp, root_path, strlen(root_path), has_path_filter, &path_regex,
997910051 grep_limit, &gm_count);
998010052 cbm_pclose(fp);
10053+ #endif
998110054 /* Both scratch files and the private directory go here — unlike the old
998210055 * code, the file list is removed even when the scan was not scoped. */
998310056 search_scratch_close(&scratch);
@@ -10138,10 +10211,16 @@ static bool mcp_resolve_windows_cmd(char out[CBM_SZ_4K]) {
1013810211}
1013910212#endif
1014010213
10141- static int mcp_run_shell_command_cancellable (cbm_mcp_server_t * srv , const char * command ,
10142- char output_path [CBM_SZ_2K ],
10143- cbm_proc_result_t * result_out ) {
10144- if (!srv || !command || !output_path || !result_out || !mcp_command_output_path (output_path )) {
10214+ static int mcp_run_shell_command_cancellable_bounded(cbm_mcp_server_t *srv, const char *command,
10215+ char output_path[CBM_SZ_2K],
10216+ size_t output_limit,
10217+ bool *output_limit_exceeded,
10218+ cbm_proc_result_t *result_out) {
10219+ if (output_limit_exceeded) {
10220+ *output_limit_exceeded = false;
10221+ }
10222+ if (!srv || !command || !output_path || !result_out ||
10223+ (output_limit > 0 && !output_limit_exceeded) || !mcp_command_output_path(output_path)) {
1014510224 return -1;
1014610225 }
1014710226 /* Internal test seam: rejecting after output allocation exercises the same
@@ -10180,10 +10259,18 @@ static int mcp_run_shell_command_cancellable(cbm_mcp_server_t *srv, const char *
1018010259 }
1018110260
1018210261 cbm_proc_poll_t state;
10262+ bool limit_exceeded = false;
1018310263 for (;;) {
1018410264 if (mcp_request_cancelled(srv)) {
1018510265 (void)cbm_subprocess_request_cancel(process);
1018610266 }
10267+ if (!limit_exceeded && output_limit > 0) {
10268+ int64_t output_size = cbm_file_size(output_path);
10269+ if (output_size > 0 && (uint64_t)output_size > output_limit) {
10270+ limit_exceeded = true;
10271+ (void)cbm_subprocess_request_cancel(process);
10272+ }
10273+ }
1018710274 state = cbm_subprocess_poll(process, result_out);
1018810275 if (state != CBM_PROC_POLL_RUNNING) {
1018910276 break;
@@ -10193,9 +10280,23 @@ static int mcp_run_shell_command_cancellable(cbm_mcp_server_t *srv, const char *
1019310280 bool contained = state == CBM_PROC_POLL_TERMINAL && result_out->tree_quiesced &&
1019410281 !result_out->supervision_failed;
1019510282 cbm_subprocess_destroy(process);
10283+ if (!limit_exceeded && output_limit > 0) {
10284+ int64_t final_size = cbm_file_size(output_path);
10285+ limit_exceeded = final_size > 0 && (uint64_t)final_size > output_limit;
10286+ }
10287+ if (output_limit_exceeded) {
10288+ *output_limit_exceeded = limit_exceeded;
10289+ }
1019610290 return contained ? 0 : -1;
1019710291}
1019810292
10293+ static int mcp_run_shell_command_cancellable(cbm_mcp_server_t *srv, const char *command,
10294+ char output_path[CBM_SZ_2K],
10295+ cbm_proc_result_t *result_out) {
10296+ return mcp_run_shell_command_cancellable_bounded(srv, command, output_path, 0, NULL,
10297+ result_out);
10298+ }
10299+
1019910300/* Does `node`'s line range overlap any recorded hunk for `file`? Used to scope
1020010301 * seed detection to the actually-changed lines rather than the whole file.
1020110302 * Non-static (declared in mcp_internal.h) so tests can exercise the overlap
0 commit comments