@@ -41,6 +41,10 @@ enum {
4141 MCP_COMPARE_MAX_SCAN_LIMIT = 10000000,
4242 MCP_COMPARE_SET_BYTE_BUDGET = 512 * 1024,
4343 MCP_QUERY_MAX_VISIBLE_ROWS = 99998,
44+ /* search_code `context`: lines of surrounding source per hit, the same
45+ * ceiling as `source_max_lines`. Clamped at the handler and again where
46+ * the window is computed, so the arithmetic never overflows. */
47+ MCP_SEARCH_CONTEXT_MAX_LINES = 200,
4448 /* max_output_tokens is model-neutral sizing guidance, not a tokenizer
4549 * promise. The actual cross-platform contract is this deterministic UTF-8
4650 * byte ceiling, applied only at whole semantic-unit boundaries. */
@@ -701,7 +705,7 @@ static const tool_def_t TOOLS[] = {
701705 "\"string\"},\"file_pattern\":{\"type\":\"string\"},\"path_filter\":{\"type\":\"string\"},"
702706 "\"mode\":{\"type\":\"string\","
703707 "\"enum\":[\"compact\",\"full\",\"files\"],\"default\":\"compact\"},"
704- "\"context\":{\"type\":\"integer\"},"
708+ "\"context\":{\"type\":\"integer\",\"default\":0,\"minimum\":0,\"maximum\":200 },"
705709 "\"regex\":{\"type\":\"boolean\",\"default\":false},"
706710 "\"debug\":{\"type\":\"boolean\",\"default\":false,"
707711 "\"description\":\"Add scope_ms/scan_ms/enrich_ms phase timings.\"},"
@@ -13464,11 +13468,15 @@ static void attach_result_source(yyjson_mut_doc *doc, yyjson_mut_val *item, sear
1346413468 }
1346513469 }
1346613470 } else if (context_lines > 0 && r->match_count > 0) {
13467- int ctx_start = r->match_lines[0] - context_lines;
13468- int ctx_end = r->match_lines[r->match_count - SKIP_ONE] + context_lines;
13469- if (ctx_start < SKIP_ONE) {
13470- ctx_start = SKIP_ONE;
13471- }
13471+ /* Bounded here as well as at the handler: the window must stay
13472+ * match ± MCP_SEARCH_CONTEXT_MAX_LINES for any caller, and the
13473+ * arithmetic must not overflow for any value. */
13474+ int ctx_lines = context_lines > MCP_SEARCH_CONTEXT_MAX_LINES ? MCP_SEARCH_CONTEXT_MAX_LINES
13475+ : context_lines;
13476+ int first_match = r->match_lines[0];
13477+ int last_match = r->match_lines[r->match_count - SKIP_ONE];
13478+ int ctx_start = first_match > ctx_lines ? first_match - ctx_lines : SKIP_ONE;
13479+ int ctx_end = last_match > INT_MAX - ctx_lines ? INT_MAX : last_match + ctx_lines;
1347213480 char *ctx = read_file_lines(abs_path, ctx_start, ctx_end);
1347313481 if (ctx) {
1347413482 char *safe_context = sanitize_utf8_lossy(ctx);
@@ -15038,6 +15046,11 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
1503815046 int result_limit = cbm_mcp_get_int_arg(args, "result_limit", legacy_limit);
1503915047 int result_offset = cbm_mcp_get_int_arg(args, "result_offset", 0);
1504015048 int context_lines = cbm_mcp_get_int_arg(args, "context", 0);
15049+ if (context_lines < 0) {
15050+ context_lines = 0;
15051+ } else if (context_lines > MCP_SEARCH_CONTEXT_MAX_LINES) {
15052+ context_lines = MCP_SEARCH_CONTEXT_MAX_LINES;
15053+ }
1504115054 bool use_regex = cbm_mcp_get_bool_arg(args, "regex");
1504215055 uint64_t search_t0 = cbm_now_ms();
1504315056 search_metrics_t metrics = {0};
0 commit comments