Local code intelligence for developers and AI agents. Connect code, docs and infrastructure in a persistent graph, with CBM Lens, MCP tools, agent integrations and change-aware test selection.
C aiderastclaude-codecode-analysiscode-intelligencecodexcursorcypherdeveloper-toolsgemini-cligraph-visualizationkilocodeknowledge-graphmcpmcp-servermodel-context-protocolopencodesqlitetree-sitterwindsurf
fix: avoid null arguments in empty sort and edge-copy operations
Martin Vogel25383bc7a462
diff --git a/src/mcp/mcp.c b/src/mcp/mcp.cindex c877d5110..64789d91b 100644--- a/src/mcp/mcp.c+++ b/src/mcp/mcp.c@@ -3044,7 +3044,11 @@ static char *handle_list_projects(cbm_mcp_server_t *srv, const char *args) {free(records);return cbm_mcp_text_result("out of memory while listing projects", true);}- qsort(records, (size_t)record_count, sizeof(*records), project_record_compare);+ /* An empty cache never allocates the record array, and qsort's base is+ * declared nonnull (glibc): skip the sort when there is nothing to order. */+ if (record_count > 1) {+ qsort(records, (size_t)record_count, sizeof(*records), project_record_compare);+ }int limit = cbm_mcp_get_int_arg(args, "limit", 50);int offset = cbm_mcp_get_int_arg(args, "offset", 0);diff --git a/src/pipeline/pass_semantic_edges.c b/src/pipeline/pass_semantic_edges.cindex 05a7387a9..14597289e 100644--- a/src/pipeline/pass_semantic_edges.c+++ b/src/pipeline/pass_semantic_edges.c@@ -1156,9 +1156,13 @@ static int phase6b_merge_edges(cbm_gbuf_t *gbuf, deferred_edge_buf_t *worker_buf}int n = 0;for (int w = 0; w < worker_count; w++) {- memcpy(&pairs[n], worker_bufs[w].edges,- (size_t)worker_bufs[w].count * sizeof(deferred_edge_t));- n += worker_bufs[w].count;+ /* A worker that emitted no pair never allocated its buffer, and+ * memcpy's source is declared nonnull (glibc) even for zero bytes. */+ if (worker_bufs[w].count > 0) {+ memcpy(&pairs[n], worker_bufs[w].edges,+ (size_t)worker_bufs[w].count * sizeof(deferred_edge_t));+ n += worker_bufs[w].count;+ }deferred_buf_free(&worker_bufs[w]);}qsort(pairs, (size_t)n, sizeof(deferred_edge_t), cmp_deferred_edge_canonical);diff --git a/tests/test_mcp.c b/tests/test_mcp.cindex 22af972af..b78b0fe51 100644--- a/tests/test_mcp.c+++ b/tests/test_mcp.c@@ -2349,6 +2349,53 @@ TEST(tool_list_projects_empty) {PASS();}+/* A cache directory that exists but holds no project is the first-run state.+ * list_projects must answer it with a well-formed empty page in both+ * encodings: zero rows, zero totals, no further page, and the indexing hint.+ *+ * This is also the one input on which the record array is never allocated.+ * The assertions are the behavioural half and hold on every platform; that the+ * sort is not handed a null base is visible only where the C library declares+ * it nonnull and the sanitizer halts on the report (see the same note on+ * pass_similarity_empty_graph_no_entries). Do not read a green run on a+ * recovering lane as proof that the guard is still in place. */+TEST(tool_list_projects_empty_cache_returns_wellformed_empty_page) {+ mcp_search_cache_t cache;+ ASSERT_TRUE(mcp_search_cache_open(&cache, "cbm-list-empty"));+ cbm_mcp_server_t *srv = cbm_mcp_server_new(NULL);++ char *json_response =+ srv ? cbm_mcp_handle_tool(srv, "list_projects", "{\"format\":\"json\"}") : NULL;+ char *json_text = json_response ? extract_text_content(json_response) : NULL;+ bool json_empty_page =+ json_text && strstr(json_text, "\"projects\":[]") && strstr(json_text, "\"total\":0") &&+ strstr(json_text, "\"returned\":0") && strstr(json_text, "\"has_more\":false") &&+ !strstr(json_text, "next_offset") && strstr(json_text, "No projects indexed");+ bool json_not_error = json_response && !strstr(json_response, "\"isError\":true");++ char *tree_response = srv ? cbm_mcp_handle_tool(srv, "list_projects", "{}") : NULL;+ char *tree_text = tree_response ? extract_text_content(tree_response) : NULL;+ bool tree_empty_page =+ tree_text && strstr(tree_text, "projects: 0 ") && strstr(tree_text, "total: 0\n") &&+ strstr(tree_text, "returned: 0\n") && strstr(tree_text, "has_more: false\n") &&+ !strstr(tree_text, "next_offset") && strstr(tree_text, "No projects indexed");+ bool tree_not_error = tree_response && !strstr(tree_response, "\"isError\":true");++ free(json_text);+ free(json_response);+ free(tree_text);+ free(tree_response);+ cbm_mcp_server_free(srv);+ ASSERT_TRUE(mcp_search_cache_close(&cache));++ ASSERT_NOT_NULL(srv);+ ASSERT_TRUE(json_not_error);+ ASSERT_TRUE(json_empty_page);+ ASSERT_TRUE(tree_not_error);+ ASSERT_TRUE(tree_empty_page);+ PASS();+}+TEST(tool_get_graph_schema_empty) {cbm_mcp_server_t *srv = setup_mcp_with_data();@@ -21095,6 +21142,7 @@ SUITE(mcp) {/* Tool handlers */RUN_TEST(tool_list_projects_empty);+ RUN_TEST(tool_list_projects_empty_cache_returns_wellformed_empty_page);RUN_TEST(tool_get_graph_schema_empty);RUN_TEST(tool_unknown_tool);RUN_TEST(tool_compare_graphs_registered_issue525);