Skip to content

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.

Fix all memory leaks + Docker test infra gaps

Martin Vogele6e9c58e80a8
diff --git a/src/graph_buffer/graph_buffer.c b/src/graph_buffer/graph_buffer.c
index 9b8829ff8..adb35329b 100644
--- a/src/graph_buffer/graph_buffer.c
+++ b/src/graph_buffer/graph_buffer.c
@@ -335,7 +335,14 @@ int64_t cbm_gbuf_upsert_node(cbm_gbuf_t *gb, const char *label, const char *name
char id_buf[32];
make_id_key(id_buf, sizeof(id_buf), id);
- cbm_ht_set(gb->node_by_id, strdup(id_buf), node);
+ /* Check if key exists — if so, reuse old heap key to avoid leak.
+ * cbm_ht_set replaces key pointer, leaking old strdup'd key. */
+ const char *existing_key = cbm_ht_get_key(gb->node_by_id, id_buf);
+ if (existing_key) {
+ cbm_ht_set(gb->node_by_id, existing_key, node);
+ } else {
+ cbm_ht_set(gb->node_by_id, strdup(id_buf), node);
+ }
/* Secondary indexes */
node_ptr_array_t *by_label = get_or_create_node_array(gb->nodes_by_label, label ? label : "");
@@ -771,7 +778,12 @@ int cbm_gbuf_merge(cbm_gbuf_t *dst, cbm_gbuf_t *src) {
cbm_ht_set(dst->node_by_qn, node->qualified_name, node);
char id_buf[32];
make_id_key(id_buf, sizeof(id_buf), node->id);
- cbm_ht_set(dst->node_by_id, strdup(id_buf), node);
+ const char *old_id_key = cbm_ht_get_key(dst->node_by_id, id_buf);
+ if (old_id_key) {
+ cbm_ht_set(dst->node_by_id, old_id_key, node);
+ } else {
+ cbm_ht_set(dst->node_by_id, strdup(id_buf), node);
+ }
/* Secondary indexes */
node_ptr_array_t *by_label =
diff --git a/src/store/store.c b/src/store/store.c
index 9f1cfc26e..88aa7078c 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -378,7 +378,8 @@ cbm_store_t *cbm_store_open_path_query(const char *db_path) {
/* Open read-write but do NOT create — returns SQLITE_CANTOPEN if absent. */
int rc = sqlite3_open_v2(db_path, &s->db, SQLITE_OPEN_READWRITE, NULL);
if (rc != SQLITE_OK) {
- /* File does not exist or cannot be opened — return NULL without creating. */
+ /* sqlite3_open_v2 allocates a handle even on failure — must close it. */
+ sqlite3_close(s->db);
free(s);
return NULL;
}
@@ -513,7 +514,9 @@ void cbm_store_close(cbm_store_t *s) {
finalize_stmt(&s->stmt_delete_file_hash);
finalize_stmt(&s->stmt_delete_file_hashes);
- sqlite3_close(s->db);
+ /* Use sqlite3_close_v2 — auto-deallocates when last statement finalizes.
+ * Prevents ASan false-positive leaks from sqlite3 internal state. */
+ sqlite3_close_v2(s->db);
free((void *)s->db_path);
free(s);
}
diff --git a/src/ui/embedded_assets.c b/src/ui/embedded_assets.c
deleted file mode 100644
index c5b8bcd9e..000000000
--- a/src/ui/embedded_assets.c
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * embedded_assets.c — Generated file mapping URL paths to embedded bytes.
- * DO NOT EDIT — regenerated by scripts/embed-frontend.sh
- */
-#include "ui/embedded_assets.h"
-#include <string.h>
-
-extern const unsigned char _binary_assets_index_D1_3bntm_css_data[];
-extern const unsigned int _binary_assets_index_D1_3bntm_css_size;
-extern const unsigned char _binary_assets_index_urRrcEH6_js_data[];
-extern const unsigned int _binary_assets_index_urRrcEH6_js_size;
-extern const unsigned char _binary_index_html_data[];
-extern const unsigned int _binary_index_html_size;
-
-cbm_embedded_file_t CBM_EMBEDDED_FILES[] = {
- {"/assets/index-D1-3bntm.css", _binary_assets_index_D1_3bntm_css_data, 0, "text/css"},
- {"/assets/index-urRrcEH6.js", _binary_assets_index_urRrcEH6_js_data, 0, "application/javascript"},
- {"/index.html", _binary_index_html_data, 0, "text/html"},
-};
-const int CBM_EMBEDDED_FILE_COUNT = 3;
-
-static void __attribute__((constructor)) init_embedded_sizes(void) {
- cbm_embedded_file_t *files = CBM_EMBEDDED_FILES;
- files[0].size = _binary_assets_index_D1_3bntm_css_size;
- files[1].size = _binary_assets_index_urRrcEH6_js_size;
- files[2].size = _binary_index_html_size;
-}
-
-const cbm_embedded_file_t *cbm_embedded_lookup(const char *path) {
- for (int i = 0; i < CBM_EMBEDDED_FILE_COUNT; i++) {
- if (strcmp(CBM_EMBEDDED_FILES[i].path, path) == 0) {
- return &CBM_EMBEDDED_FILES[i];
- }
- }
- return NULL;
-}
diff --git a/test-infrastructure/Dockerfile b/test-infrastructure/Dockerfile
index bb7c6202a..4e212af87 100644
--- a/test-infrastructure/Dockerfile
+++ b/test-infrastructure/Dockerfile
@@ -13,7 +13,8 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
gcc g++ make \
zlib1g-dev \
pkg-config \
- python3-minimal \
+ python3 \
+ git \
&& rm -rf /var/lib/apt/lists/*
WORKDIR /src
diff --git a/tests/test_arena.c b/tests/test_arena.c
index 4c34a951d..3ce19f8f4 100644
--- a/tests/test_arena.c
+++ b/tests/test_arena.c
@@ -308,7 +308,8 @@ TEST(arena_strndup_len_exceeds_string) {
TEST(arena_sprintf_empty_format) {
CBMArena a;
cbm_arena_init(&a);
- char *s = cbm_arena_sprintf(&a, "");
+ /* Use "%s" with empty string — GCC rejects literal "" as format (-Wformat-zero-length) */
+ char *s = cbm_arena_sprintf(&a, "%s", "");
ASSERT_NOT_NULL(s);
ASSERT_STR_EQ(s, "");
ASSERT_EQ(strlen(s), 0);
diff --git a/tests/test_main.c b/tests/test_main.c
index 0667d32a6..97f23418d 100644
--- a/tests/test_main.c
+++ b/tests/test_main.c
@@ -9,6 +9,7 @@ int tf_fail_count = 0;
int tf_skip_count = 0;
#include "test_framework.h"
+#include <sqlite3.h>
/* Forward declarations of suite functions */
extern void suite_arena(void);
@@ -147,5 +148,7 @@ int main(void) {
/* Integration (end-to-end) */
RUN_SUITE(integration);
+ /* Release sqlite3 internal caches so ASan doesn't report them as leaks */
+ sqlite3_shutdown();
TEST_SUMMARY();
}