Skip to content

Commit 02bc9f2

Browse files
committed
fix(mcp): clamp search_code context on both sides, add sink ceilings
The search_code `context` argument had no upper bound while every sibling sizing argument (result_limit, match_limit, source_max_lines, raw_limit, directory_limit) is clamped on both sides. A large value made every hit return its file from the match to the end, and the window arithmetic in attach_result_source could overflow int. `context` now gets the same 0..200 clamp as source_max_lines (MCP_SEARCH_CONTEXT_MAX_LINES), the window computation is overflow-safe on its own, and the tool schema states default, minimum and maximum like its siblings. Values inside the bound return exactly the lines they did before. Sink ceilings: cbm_cypher_execute clamps a caller-supplied max_rows to CYPHER_RESULT_CEILING (nothing above it is ever materialized) and execute_single checks the binding array it sizes from that value instead of writing through a failed allocation. Tests pin the context window and the extreme values (INT_MAX, negative), the query_graph max_rows ceiling, the search_graph limit ceiling, the trace cursor token truncated after the leg character, the quote rejection in file_pattern, and the engine-side max_rows clamp. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent cb7d937 commit 02bc9f2

5 files changed

Lines changed: 399 additions & 9 deletions

File tree

‎src/cypher/cypher.c‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5138,7 +5138,11 @@ static int execute_single(cbm_store_t *store, cbm_query_t *q, const char *projec
51385138

51395139
/* Build initial bindings with early WHERE */
51405140
int bind_cap = scan_count > max_rows ? scan_count : (max_rows > 0 ? max_rows : SKIP_ONE);
5141-
binding_t *bindings = malloc((bind_cap + SKIP_ONE) * sizeof(binding_t));
5141+
binding_t *bindings = malloc(((size_t)bind_cap + SKIP_ONE) * sizeof(binding_t));
5142+
if (!bindings) {
5143+
cbm_store_free_nodes(scanned, scan_count);
5144+
return CBM_NOT_FOUND; /* initial binding array refused */
5145+
}
51425146
int bind_count = 0;
51435147
const char *var_name = pat0->nodes[0].variable ? pat0->nodes[0].variable : CYP_ANON_HEAD_VAR;
51445148

@@ -5448,7 +5452,9 @@ int cbm_cypher_execute(cbm_store_t *store, const char *query, const char *projec
54485452
g_cypher_trail_truncated = 0;
54495453
g_cypher_truncated = false;
54505454
cypher_deadline_arm(); /* #601: start the wall-clock budget for this query */
5451-
if (max_rows <= 0) {
5455+
/* max_rows sizes the initial binding array: non-positive means the
5456+
* ceiling, and nothing above the ceiling is ever materialized anyway. */
5457+
if (max_rows <= 0 || max_rows > CYPHER_RESULT_CEILING) {
54525458
max_rows = CYPHER_RESULT_CEILING;
54535459
}
54545460

‎src/cypher/cypher.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,7 +334,8 @@ typedef struct {
334334
} cbm_cypher_result_t;
335335

336336
/* Execute a Cypher query against a store.
337-
* max_rows: limit on output rows (0 = use virtual ceiling of 100k).
337+
* max_rows: limit on output rows; 0 or any value above the 100k ceiling uses
338+
* the ceiling.
338339
* project: project name filter (NULL = all projects).
339340
* Returns -1 on error (check out->error for message). */
340341
int cbm_cypher_execute(cbm_store_t *store, const char *query, const char *project, int max_rows,

‎src/mcp/mcp.c‎

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ enum {
4242
MCP_COMPARE_MAX_SCAN_LIMIT = 10000000,
4343
MCP_COMPARE_SET_BYTE_BUDGET = 512 * 1024,
4444
MCP_QUERY_MAX_VISIBLE_ROWS = 99998,
45+
/* search_code `context`: lines of surrounding source per hit, the same
46+
* ceiling as `source_max_lines`. Clamped at the handler and again where
47+
* the window is computed, so the arithmetic never overflows. */
48+
MCP_SEARCH_CONTEXT_MAX_LINES = 200,
4549
/* max_output_tokens is model-neutral sizing guidance, not a tokenizer
4650
* promise. The actual cross-platform contract is this deterministic UTF-8
4751
* 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.\"},"
@@ -13328,11 +13332,15 @@ static void attach_result_source(yyjson_mut_doc *doc, yyjson_mut_val *item, sear
1332813332
}
1332913333
}
1333013334
} else if (context_lines > 0 && r->match_count > 0) {
13331-
int ctx_start = r->match_lines[0] - context_lines;
13332-
int ctx_end = r->match_lines[r->match_count - SKIP_ONE] + context_lines;
13333-
if (ctx_start < SKIP_ONE) {
13334-
ctx_start = SKIP_ONE;
13335-
}
13335+
/* Bounded here as well as at the handler: the window must stay
13336+
* match ± MCP_SEARCH_CONTEXT_MAX_LINES for any caller, and the
13337+
* arithmetic must not overflow for any value. */
13338+
int ctx_lines = context_lines > MCP_SEARCH_CONTEXT_MAX_LINES ? MCP_SEARCH_CONTEXT_MAX_LINES
13339+
: context_lines;
13340+
int first_match = r->match_lines[0];
13341+
int last_match = r->match_lines[r->match_count - SKIP_ONE];
13342+
int ctx_start = first_match > ctx_lines ? first_match - ctx_lines : SKIP_ONE;
13343+
int ctx_end = last_match > INT_MAX - ctx_lines ? INT_MAX : last_match + ctx_lines;
1333613344
char *ctx = read_file_lines(abs_path, ctx_start, ctx_end);
1333713345
if (ctx) {
1333813346
char *safe_context = sanitize_utf8_lossy(ctx);
@@ -14902,6 +14910,11 @@ static char *handle_search_code(cbm_mcp_server_t *srv, const char *args) {
1490214910
int result_limit = cbm_mcp_get_int_arg(args, "result_limit", legacy_limit);
1490314911
int result_offset = cbm_mcp_get_int_arg(args, "result_offset", 0);
1490414912
int context_lines = cbm_mcp_get_int_arg(args, "context", 0);
14913+
if (context_lines < 0) {
14914+
context_lines = 0;
14915+
} else if (context_lines > MCP_SEARCH_CONTEXT_MAX_LINES) {
14916+
context_lines = MCP_SEARCH_CONTEXT_MAX_LINES;
14917+
}
1490514918
bool use_regex = cbm_mcp_get_bool_arg(args, "regex");
1490614919
uint64_t search_t0 = cbm_now_ms();
1490714920
search_metrics_t metrics = {0};

‎tests/test_cypher.c‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "../src/foundation/compat_thread.h"
1010
#include <cypher/cypher.h>
1111
#include <store/store.h>
12+
#include <limits.h>
1213
#include <stdatomic.h>
1314
#include <stdio.h>
1415
#include <string.h>
@@ -4862,6 +4863,28 @@ TEST(cypher_exec_deadline_allows_normal_query_issue601) {
48624863
PASS();
48634864
}
48644865

4866+
/* The caller-supplied output-row limit sizes the initial binding array; a
4867+
* value above the engine ceiling is clamped to it before that happens, and a
4868+
* value inside the ceiling still bounds the row count. */
4869+
TEST(cypher_exec_max_rows_above_ceiling_is_clamped) {
4870+
cbm_store_t *s = setup_cypher_store();
4871+
cbm_cypher_result_t r = {0};
4872+
4873+
int rc = cbm_cypher_execute(s, "MATCH (f:Function)", "test", INT_MAX, &r);
4874+
ASSERT_EQ(rc, 0);
4875+
ASSERT_EQ(r.row_count, 4);
4876+
cbm_cypher_result_free(&r);
4877+
4878+
memset(&r, 0, sizeof(r));
4879+
rc = cbm_cypher_execute(s, "MATCH (f:Function)", "test", 2, &r);
4880+
ASSERT_EQ(rc, 0);
4881+
ASSERT_EQ(r.row_count, 2);
4882+
cbm_cypher_result_free(&r);
4883+
4884+
cbm_store_close(s);
4885+
PASS();
4886+
}
4887+
48654888
/* ══════════════════════════════════════════════════════════════════ */
48664889

48674890
SUITE(cypher) {
@@ -5089,4 +5112,5 @@ SUITE(cypher) {
50895112
RUN_TEST(cypher_exec_prop_array_with_internal_commas);
50905113
RUN_TEST(cypher_exec_prop_string_with_escaped_quote);
50915114
RUN_TEST(cypher_single_hop_seeds_from_selective_far_node);
5115+
RUN_TEST(cypher_exec_max_rows_above_ceiling_is_clamped);
50925116
}

0 commit comments

Comments
 (0)