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 Ubuntu CI: stack-use-after-return, L2 cache detection, memory leaks

Martin Vogelc9ec1d1eb4b0
diff --git a/internal/cbm/lsp/generated/cpp_stdlib_data.c b/internal/cbm/lsp/generated/cpp_stdlib_data.c
index e62c3ab49..c23342ecb 100644
--- a/internal/cbm/lsp/generated/cpp_stdlib_data.c
+++ b/internal/cbm/lsp/generated/cpp_stdlib_data.c
@@ -290,11 +290,11 @@ void cbm_cpp_stdlib_register(CBMTypeRegistry* reg, CBMArena* arena) {
const char* pair_qn = "std.pair";
{
static const char* pair_field_names[] = {"first", "second", NULL};
- const CBMType* pair_field_types[] = {
- cbm_type_type_param(arena, "T1"),
- cbm_type_type_param(arena, "T2"),
- NULL
- };
+ const CBMType** pair_field_types = (const CBMType**)cbm_arena_alloc(arena,
+ 3 * sizeof(const CBMType*));
+ pair_field_types[0] = cbm_type_type_param(arena, "T1");
+ pair_field_types[1] = cbm_type_type_param(arena, "T2");
+ pair_field_types[2] = NULL;
reg_type_with_fields(reg, arena, pair_qn, "pair", pair_field_names, pair_field_types);
}
diff --git a/src/foundation/system_info.c b/src/foundation/system_info.c
index 606cbba0b..0cca50257 100644
--- a/src/foundation/system_info.c
+++ b/src/foundation/system_info.c
@@ -150,8 +150,35 @@ static cbm_system_info_t detect_system_linux(void) {
if (avail > 0)
info.free_ram = avail;
- /* L2 cache */
+ /* L2 cache: sysconf works on x86 (glibc reads CPUID), but returns 0
+ * on ARM. Fall back to sysfs cache topology when sysconf fails. */
long l2 = sysconf(_SC_LEVEL2_CACHE_SIZE);
+ if (l2 <= 0) {
+ /* Scan /sys/devices/system/cpu/cpu0/cache/index* for level==2 */
+ for (int idx = 0; idx < 8; idx++) {
+ char path[128];
+ snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu0/cache/index%d/level", idx);
+ FILE *f = fopen(path, "r");
+ if (!f)
+ break;
+ int level = 0;
+ if (fscanf(f, "%d", &level) != 1)
+ level = 0;
+ fclose(f);
+ if (level == 2) {
+ snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu0/cache/index%d/size",
+ idx);
+ f = fopen(path, "r");
+ if (f) {
+ unsigned long kb = 0;
+ if (fscanf(f, "%lu", &kb) == 1 && kb > 0)
+ l2 = (long)(kb * 1024UL);
+ fclose(f);
+ }
+ break;
+ }
+ }
+ }
info.l2_cache_perf = l2 > 0 ? (size_t)l2 : 0;
info.l2_cache_eff = 0;
diff --git a/src/store/store.c b/src/store/store.c
index 0eca32f70..7d31fbdf6 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -4247,7 +4247,7 @@ int cbm_store_find_architecture_docs(cbm_store_t *s, const char *project, char *
/* ── Memory management ──────────────────────────────────────────── */
-static void free_node_fields(cbm_node_t *n) {
+void cbm_node_free_fields(cbm_node_t *n) {
free((void *)n->project);
free((void *)n->label);
free((void *)n->name);
@@ -4261,7 +4261,7 @@ void cbm_store_free_nodes(cbm_node_t *nodes, int count) {
return;
}
for (int i = 0; i < count; i++) {
- free_node_fields(&nodes[i]);
+ cbm_node_free_fields(&nodes[i]);
}
free(nodes);
}
diff --git a/src/store/store.h b/src/store/store.h
index 2eeca1b5f..fa619edfa 100644
--- a/src/store/store.h
+++ b/src/store/store.h
@@ -564,6 +564,9 @@ int cbm_louvain(const int64_t *nodes, int node_count, const cbm_louvain_edge_t *
/* ── Memory management helpers ──────────────────────────────────── */
+/* Free heap-allocated strings in a stack-allocated node (does NOT free the node itself). */
+void cbm_node_free_fields(cbm_node_t *n);
+
/* Free an array of nodes returned by find_nodes_by_* functions. */
void cbm_store_free_nodes(cbm_node_t *nodes, int count);
diff --git a/tests/test_pipeline.c b/tests/test_pipeline.c
index e9e374ef1..ea42889db 100644
--- a/tests/test_pipeline.c
+++ b/tests/test_pipeline.c
@@ -237,10 +237,11 @@ TEST(pipeline_structure_nodes) {
ASSERT_GTE(node_count, 9); /* 6 structure + at least 3 definitions */
/* Verify project node exists */
- cbm_node_t proj_node;
+ cbm_node_t proj_node = {0};
rc = cbm_store_find_node_by_qn(s, project, project, &proj_node);
ASSERT_EQ(rc, CBM_STORE_OK);
ASSERT_STR_EQ(proj_node.label, "Project");
+ cbm_node_free_fields(&proj_node);
/* Verify folder nodes */
cbm_node_t *folders = NULL;
@@ -894,13 +895,15 @@ TEST(usages_creates_edges) {
bool found_usage = false;
for (int i = 0; i < edge_count; i++) {
- cbm_node_t src, tgt;
+ cbm_node_t src = {0}, tgt = {0};
if (cbm_store_find_node_by_id(s, edges[i].source_id, &src) == CBM_STORE_OK &&
cbm_store_find_node_by_id(s, edges[i].target_id, &tgt) == CBM_STORE_OK) {
if (strcmp(src.name, "Register") == 0 && strcmp(tgt.name, "Process") == 0) {
found_usage = true;
}
}
+ cbm_node_free_fields(&src);
+ cbm_node_free_fields(&tgt);
}
if (edges)
cbm_store_free_edges(edges, edge_count);
@@ -946,13 +949,15 @@ TEST(usages_no_duplicate_calls) {
bool found_call = false;
for (int i = 0; i < call_count; i++) {
- cbm_node_t src, tgt;
+ cbm_node_t src = {0}, tgt = {0};
if (cbm_store_find_node_by_id(s, call_edges[i].source_id, &src) == CBM_STORE_OK &&
cbm_store_find_node_by_id(s, call_edges[i].target_id, &tgt) == CBM_STORE_OK) {
if (strcmp(src.name, "Main") == 0 && strcmp(tgt.name, "Helper") == 0) {
found_call = true;
}
}
+ cbm_node_free_fields(&src);
+ cbm_node_free_fields(&tgt);
}
if (call_edges)
cbm_store_free_edges(call_edges, call_count);
@@ -964,11 +969,13 @@ TEST(usages_no_duplicate_calls) {
cbm_store_find_edges_by_type(s, project, "USAGE", &usage_edges, &usage_count);
for (int i = 0; i < usage_count; i++) {
- cbm_node_t src, tgt;
+ cbm_node_t src = {0}, tgt = {0};
if (cbm_store_find_node_by_id(s, usage_edges[i].source_id, &src) == CBM_STORE_OK &&
cbm_store_find_node_by_id(s, usage_edges[i].target_id, &tgt) == CBM_STORE_OK) {
ASSERT_FALSE(strcmp(src.name, "Main") == 0 && strcmp(tgt.name, "Helper") == 0);
}
+ cbm_node_free_fields(&src);
+ cbm_node_free_fields(&tgt);
}
if (usage_edges)
cbm_store_free_edges(usage_edges, usage_count);
@@ -1054,13 +1061,15 @@ TEST(usages_kotlin_no_duplicate_calls) {
bool found_call = false;
for (int i = 0; i < call_count; i++) {
- cbm_node_t src, tgt;
+ cbm_node_t src = {0}, tgt = {0};
if (cbm_store_find_node_by_id(s, call_edges[i].source_id, &src) == CBM_STORE_OK &&
cbm_store_find_node_by_id(s, call_edges[i].target_id, &tgt) == CBM_STORE_OK) {
if (strcmp(src.name, "main") == 0 && strcmp(tgt.name, "helper") == 0) {
found_call = true;
}
}
+ cbm_node_free_fields(&src);
+ cbm_node_free_fields(&tgt);
}
if (call_edges)
cbm_store_free_edges(call_edges, call_count);
@@ -1072,11 +1081,13 @@ TEST(usages_kotlin_no_duplicate_calls) {
cbm_store_find_edges_by_type(s, project, "USAGE", &usage_edges, &usage_count);
for (int i = 0; i < usage_count; i++) {
- cbm_node_t src, tgt;
+ cbm_node_t src = {0}, tgt = {0};
if (cbm_store_find_node_by_id(s, usage_edges[i].source_id, &src) == CBM_STORE_OK &&
cbm_store_find_node_by_id(s, usage_edges[i].target_id, &tgt) == CBM_STORE_OK) {
ASSERT_FALSE(strcmp(src.name, "main") == 0 && strcmp(tgt.name, "helper") == 0);
}
+ cbm_node_free_fields(&src);
+ cbm_node_free_fields(&tgt);
}
if (usage_edges)
cbm_store_free_edges(usage_edges, usage_count);
@@ -1597,11 +1608,12 @@ TEST(pipeline_form_call_resolution) {
cbm_store_find_edges_by_type(s, proj, "CALLS", &edges, &ec);
bool found = false;
for (int i = 0; i < ec; i++) {
- cbm_node_t tgt;
+ cbm_node_t tgt = {0};
if (cbm_store_find_node_by_id(s, edges[i].target_id, &tgt) == CBM_STORE_OK &&
strcmp(tgt.name, "callee") == 0) {
found = true;
}
+ cbm_node_free_fields(&tgt);
}
ASSERT_TRUE(found);
if (edges)
diff --git a/tests/test_store_nodes.c b/tests/test_store_nodes.c
index 7d15f2f1f..f69548604 100644
--- a/tests/test_store_nodes.c
+++ b/tests/test_store_nodes.c
@@ -136,12 +136,14 @@ TEST(store_node_crud) {
ASSERT_EQ(found.start_line, 10);
ASSERT_EQ(found.end_line, 20);
ASSERT_NOT_NULL(found.properties_json);
+ cbm_node_free_fields(&found);
/* Find by ID */
cbm_node_t found2 = {0};
rc = cbm_store_find_node_by_id(s, id, &found2);
ASSERT_EQ(rc, CBM_STORE_OK);
ASSERT_STR_EQ(found2.qualified_name, "test.main.Foo");
+ cbm_node_free_fields(&found2);
/* Find by name */
cbm_node_t *nodes = NULL;
@@ -184,6 +186,7 @@ TEST(store_node_dedup) {
ASSERT_NOT_NULL(found.properties_json);
/* Should contain "updated" */
ASSERT(strstr(found.properties_json, "updated") != NULL);
+ cbm_node_free_fields(&found);
cbm_store_close(s);
PASS();
@@ -449,6 +452,7 @@ TEST(store_file_hash_crud) {
ASSERT_EQ(rc, CBM_STORE_OK);
rc = cbm_store_get_file_hashes(s, "test", &hashes, &count);
ASSERT_EQ(count, 0);
+ cbm_store_free_file_hashes(hashes, count);
cbm_store_close(s);
PASS();
@@ -472,6 +476,7 @@ TEST(store_node_properties_json) {
ASSERT_NOT_NULL(found.properties_json);
ASSERT(strstr(found.properties_json, "visibility") != NULL);
ASSERT(strstr(found.properties_json, "public") != NULL);
+ cbm_node_free_fields(&found);
cbm_store_close(s);
PASS();
@@ -493,6 +498,7 @@ TEST(store_node_null_properties) {
cbm_store_find_node_by_qn(s, "test", "test.Baz", &found);
ASSERT_NOT_NULL(found.properties_json);
ASSERT_STR_EQ(found.properties_json, "{}");
+ cbm_node_free_fields(&found);
cbm_store_close(s);
PASS();
@@ -829,6 +835,7 @@ TEST(store_restore_from) {
rc = cbm_store_find_node_by_qn(dst, "test", "test.main.Func5", &found);
ASSERT_EQ(rc, CBM_STORE_OK);
ASSERT_STR_EQ(found.name, "Func5");
+ cbm_node_free_fields(&found);
int cnt = cbm_store_count_nodes(dst, "test");
ASSERT_EQ(cnt, 10);
diff --git a/tests/test_store_search.c b/tests/test_store_search.c
index 56823c63a..c78a3e923 100644
--- a/tests/test_store_search.c
+++ b/tests/test_store_search.c
@@ -451,6 +451,7 @@ TEST(store_dump_to_file) {
rc = cbm_store_find_node_by_qn(disk, "test", "test.main.Hello", &found);
ASSERT_EQ(rc, CBM_STORE_OK);
ASSERT_STR_EQ(found.name, "Hello");
+ cbm_node_free_fields(&found);
cbm_store_close(disk);
unlink(path);