Skip to content

Commit 5c77486

Browse files
authored
Merge pull request DeusData#2474 from DeusData/fix/search-arg-bounds
fix(mcp): clamp search_code context on both sides, add sink ceilings
2 parents 9480125 + 02bc9f2 commit 5c77486

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
@@ -5252,7 +5252,11 @@ static int execute_single(cbm_store_t *store, cbm_query_t *q, const char *projec
52525252

52535253
/* Build initial bindings with early WHERE */
52545254
int bind_cap = scan_count > max_rows ? scan_count : (max_rows > 0 ? max_rows : SKIP_ONE);
5255-
binding_t *bindings = malloc((bind_cap + SKIP_ONE) * sizeof(binding_t));
5255+
binding_t *bindings = malloc(((size_t)bind_cap + SKIP_ONE) * sizeof(binding_t));
5256+
if (!bindings) {
5257+
cbm_store_free_nodes(scanned, scan_count);
5258+
return CBM_NOT_FOUND; /* initial binding array refused */
5259+
}
52565260
int bind_count = 0;
52575261
const char *var_name = pat0->nodes[0].variable ? pat0->nodes[0].variable : CYP_ANON_HEAD_VAR;
52585262

@@ -5562,7 +5566,9 @@ int cbm_cypher_execute(cbm_store_t *store, const char *query, const char *projec
55625566
g_cypher_trail_truncated = 0;
55635567
g_cypher_truncated = false;
55645568
cypher_deadline_arm(); /* #601: start the wall-clock budget for this query */
5565-
if (max_rows <= 0) {
5569+
/* max_rows sizes the initial binding array: non-positive means the
5570+
* ceiling, and nothing above the ceiling is ever materialized anyway. */
5571+
if (max_rows <= 0 || max_rows > CYPHER_RESULT_CEILING) {
55665572
max_rows = CYPHER_RESULT_CEILING;
55675573
}
55685574

‎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
@@ -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};

‎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>
@@ -4955,6 +4956,28 @@ TEST(cypher_exec_deadline_allows_normal_query_issue601) {
49554956
PASS();
49564957
}
49574958

4959+
/* The caller-supplied output-row limit sizes the initial binding array; a
4960+
* value above the engine ceiling is clamped to it before that happens, and a
4961+
* value inside the ceiling still bounds the row count. */
4962+
TEST(cypher_exec_max_rows_above_ceiling_is_clamped) {
4963+
cbm_store_t *s = setup_cypher_store();
4964+
cbm_cypher_result_t r = {0};
4965+
4966+
int rc = cbm_cypher_execute(s, "MATCH (f:Function)", "test", INT_MAX, &r);
4967+
ASSERT_EQ(rc, 0);
4968+
ASSERT_EQ(r.row_count, 4);
4969+
cbm_cypher_result_free(&r);
4970+
4971+
memset(&r, 0, sizeof(r));
4972+
rc = cbm_cypher_execute(s, "MATCH (f:Function)", "test", 2, &r);
4973+
ASSERT_EQ(rc, 0);
4974+
ASSERT_EQ(r.row_count, 2);
4975+
cbm_cypher_result_free(&r);
4976+
4977+
cbm_store_close(s);
4978+
PASS();
4979+
}
4980+
49584981
/* ══════════════════════════════════════════════════════════════════ */
49594982

49604983
SUITE(cypher) {
@@ -5183,4 +5206,5 @@ SUITE(cypher) {
51835206
RUN_TEST(cypher_exec_prop_array_with_internal_commas);
51845207
RUN_TEST(cypher_exec_prop_string_with_escaped_quote);
51855208
RUN_TEST(cypher_single_hop_seeds_from_selective_far_node);
5209+
RUN_TEST(cypher_exec_max_rows_above_ceiling_is_clamped);
51865210
}

0 commit comments

Comments
 (0)