Skip to content

Commit 0efc1ed

Browse files
committed
fix(mcp): replace .db-targeting stat() with cbm_file_size on Windows (>2GB)
On Windows struct stat::st_size is a 32-bit long, so any .db larger than 2 GB makes stat() fail with EOVERFLOW. Three call sites in src/mcp/mcp.c checked .db files this way and silently mishandled the > 2 GB case: list_projects dropped the project from the UI list, maybe_auto_index re-indexed it on every server start, and try_artifact_bootstrap re-imported it from the artifact. Replace all three with the existing cbm_file_size (platform.c), which is wide-char + 64-bit clean on Windows (GetFileAttributesExW) and returns int64_t on both platforms, so the size never rides through the 32-bit st_size. Signed-off-by: Dusk_NM02 <dusk.11th@outlook.com>
1 parent 684e35b commit 0efc1ed

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

src/mcp/mcp.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -953,7 +953,7 @@ static bool is_project_db_file(const char *name, size_t len) {
953953
/* Open a .db file briefly, collect node/edge counts and root_path,
954954
* then append a JSON entry to arr. */
955955
static void build_project_json_entry(yyjson_mut_doc *doc, yyjson_mut_val *arr, const char *dir_path,
956-
const char *name, size_t name_len, const struct stat *st) {
956+
const char *name, size_t name_len, int64_t size_bytes) {
957957
char project_name[CBM_SZ_1K];
958958
snprintf(project_name, sizeof(project_name), "%.*s", (int)(name_len - 3), name);
959959

@@ -985,7 +985,7 @@ static void build_project_json_entry(yyjson_mut_doc *doc, yyjson_mut_val *arr, c
985985
add_git_context_json(doc, p, root_path_buf[0] ? root_path_buf : NULL);
986986
yyjson_mut_obj_add_int(doc, p, "nodes", nodes);
987987
yyjson_mut_obj_add_int(doc, p, "edges", edges);
988-
yyjson_mut_obj_add_int(doc, p, "size_bytes", (int64_t)st->st_size);
988+
yyjson_mut_obj_add_int(doc, p, "size_bytes", size_bytes);
989989
yyjson_mut_arr_add_val(arr, p);
990990
}
991991

@@ -1024,11 +1024,11 @@ static char *handle_list_projects(cbm_mcp_server_t *srv, const char *args) {
10241024
}
10251025
char full_path[CBM_SZ_2K];
10261026
snprintf(full_path, sizeof(full_path), "%s/%s", dir_path, name);
1027-
struct stat st;
1028-
if (stat(full_path, &st) != 0) {
1027+
int64_t size_bytes = cbm_file_size(full_path);
1028+
if (size_bytes < 0) {
10291029
continue;
10301030
}
1031-
build_project_json_entry(doc, arr, dir_path, name, len, &st);
1031+
build_project_json_entry(doc, arr, dir_path, name, len, size_bytes);
10321032
}
10331033
cbm_closedir(d);
10341034

@@ -2653,8 +2653,7 @@ static char *handle_cross_repo_mode(const char *repo_path, const char *args) {
26532653
static void try_artifact_bootstrap(const char *project_name, const char *repo_path) {
26542654
char db_buf[CBM_SZ_1K];
26552655
project_db_path(project_name, db_buf, sizeof(db_buf));
2656-
struct stat db_st;
2657-
if (stat(db_buf, &db_st) != 0 && cbm_artifact_exists(repo_path)) {
2656+
if (cbm_file_size(db_buf) < 0 && cbm_artifact_exists(repo_path)) {
26582657
cbm_log_info("index.artifact_bootstrap", "project", project_name);
26592658
cbm_artifact_import(repo_path, db_buf);
26602659
}
@@ -4490,8 +4489,7 @@ static void maybe_auto_index(cbm_mcp_server_t *srv) {
44904489
char db_check[CBM_SZ_1K];
44914490
snprintf(db_check, sizeof(db_check), "%s/%s.db", cbm_resolve_cache_dir(),
44924491
srv->session_project);
4493-
struct stat st;
4494-
if (stat(db_check, &st) == 0) {
4492+
if (cbm_file_size(db_check) >= 0) {
44954493
/* Already indexed → register watcher for change detection */
44964494
cbm_log_info("autoindex.skip", "reason", "already_indexed", "project",
44974495
srv->session_project);

0 commit comments

Comments
 (0)