Skip to content

Commit 80afcd6

Browse files
committed
fix: close the memory-error paths the clang-analyzer lane surfaced
The memory-diagnostics report's priority-4 lane (path-sensitive clang-analyzer, memory checks only) run over all 111 production files. 21 findings triaged; the real ones, all cold-path (none can explain #581's per-query residual): LEAKS - mcp get_architecture: scope_path leaked on the missing-store early return (REQUIRE_STORE frees only `project`); allocate after the gate. - pass_definitions: cancellation mid-extraction leaked the pass-owned result cache including already-extracted entries; mirror the end-of-pass cleanup. - store package-boundary scan: the row-scan abort path freed the node arrays but not the boundary accumulators or their duplicated package strings. - cbm quarantine set: a duplicate path line leaked the replaced value (and a fresh key copy -- the table borrows key pointers); a partial strdup failure leaked the surviving half. Reuse the stored key for duplicates. - pass_githistory: unchecked malloc/strdup -- an OOM dereferenced NULL and a failed strdup leaked the index cell. Allocate before claiming the slot. NULL/UB - cli config subcommand: NULL argv with nonzero argc slipped the guard (the inner `argv &&` shielded only the help comparison) into argv[0]. - store bfs_multi: a negative max_results broke out before any row was written, then freed fields of an unwritten negative-index slot. Clamp. - pass_calls emit_http_async_edge: the service-pattern call sites pass a NULL target behind a hand-duplicated URL predicate; a drift between the copies turned target->id into a null deref. The callee is now total. - sqlite_writer: both leaf-array OOM paths left leaf_count stale with a NULL array, walking pb_finalize_* into leaves[0]; consistent empty state routes them to the existing root=0 failure return. HARDENED (invariants true but invisible to path-sensitive analysis) - Leiden CSR + aggregate arrays, SCC adjacency: calloc + endpoint guards, so a future degree/collection miscount degrades benignly instead of UB. - SCC cycle fill: the ncyc==0 no-slot invariant made local. RECORDED FALSE POSITIVES (no code change) - yaml sequence starts (loop bound == alloc bound), cypher agg arrays (same count both sides), mcp read_message ch (assigned by fgetc each iteration), pkgmap clean buffer, mcp csize (Tarjan: ncomp>=1 when nverts>=1), vendored verstable x2. Signed-off-by: Martin Vogel <[email protected]> GATE + LANES (user decision: runner cost accepted) - make lint-mem (local triage) and lint-mem-ci (gating: vendored-filtered, any remaining finding fails). The gate is green because every false positive above was restructured for provability -- calloc'd fill-cursor arrays, explicit Tarjan invariant, zeroed buffer tails, min-1-element allocations -- never suppressed. - make diag: pinned newest-LLVM ASan/UBSan lane with straighter stacks. - CI: lint-mem job (_lint.yml) and test-diag job (_test.yml), both on the pinned LLVM 22 apt toolchain. Cost disclosure: roughly +25-40 min and +25-60 min (ccache-warm) per push respectively. Signed-off-by: Martin Vogel <[email protected]>
1 parent 925e662 commit 80afcd6

14 files changed

Lines changed: 255 additions & 27 deletions

File tree

‎.github/workflows/_lint.yml‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,28 @@ jobs:
4949

5050
- name: Lint (cppcheck + clang-format, no clang-tidy — enforced locally)
5151
run: scripts/lint.sh --ci CLANG_FORMAT=clang-format-20
52+
53+
# Memory-analyzer gate (user decision 2026-08-03: runner cost accepted).
54+
# Path-sensitive clang-analyzer over the memory checks only: leak paths,
55+
# null derefs, uninitialized reads. Its first run produced 9 real fixes;
56+
# the gate is green because every false positive was RESTRUCTURED for
57+
# provability (never suppressed — the NOLINT ban applies here too).
58+
# Vendored-tree diagnostics are path-filtered, mirroring .cppcheck.
59+
lint-mem:
60+
runs-on: ubuntu-latest
61+
timeout-minutes: 60
62+
steps:
63+
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
64+
65+
- name: Install build deps
66+
run: sudo apt-get update && sudo apt-get install -y zlib1g-dev
67+
68+
- name: Install LLVM 22 (pinned analyzer toolchain)
69+
run: |
70+
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
71+
echo "deb http://apt.llvm.org/noble/ llvm-toolchain-noble-22 main" | sudo tee /etc/apt/sources.list.d/llvm-22.list
72+
sudo apt-get update
73+
sudo apt-get install -y clang-tidy-22
74+
75+
- name: Memory-analyzer gate
76+
run: make -f Makefile.cbm lint-mem-ci CLANG_TIDY=clang-tidy-22

‎.github/workflows/_test.yml‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,47 @@ jobs:
183183
# platform can surface a race the others miss. Windows has no TSan runtime on
184184
# any toolchain (documented irreducible gap); this shared-code coverage is
185185
# its substitute.
186+
# Memory-diagnostics lane (user decision 2026-08-03: runner cost accepted).
187+
# Newest pinned LLVM sanitizer runtime + straighter stacks over the full
188+
# parallel suite — catches what an older compiler-rt can miss. Same
189+
# canonical scripts/test.sh wave as every other leg.
190+
test-diag:
191+
runs-on: ubuntu-latest
192+
timeout-minutes: 120
193+
steps:
194+
- uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
195+
196+
- name: Install deps
197+
run: sudo apt-get update && sudo apt-get install -y zlib1g-dev ccache
198+
199+
- name: Install LLVM 22 (pinned diagnostic toolchain)
200+
run: |
201+
wget -qO- https://apt.llvm.org/llvm-snapshot.gpg.key | sudo tee /etc/apt/trusted.gpg.d/apt.llvm.org.asc
202+
echo "deb http://apt.llvm.org/noble/ llvm-toolchain-noble-22 main" | sudo tee /etc/apt/sources.list.d/llvm-22.list
203+
sudo apt-get update
204+
sudo apt-get install -y clang-22
205+
206+
- name: Compiler cache (content-verified, restore)
207+
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
208+
with:
209+
path: ${{ github.workspace }}/.ccache
210+
key: ccache-diag-${{ github.ref }}-${{ github.sha }}
211+
restore-keys: |
212+
ccache-diag-${{ github.ref }}-
213+
ccache-diag-
214+
215+
- name: Test (clang-22, ASan+UBSan, -fno-optimize-sibling-calls)
216+
run: >
217+
scripts/test.sh CC=clang-22 CXX=clang++-22
218+
SANITIZE="-fsanitize=address,undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls"
219+
220+
- name: Compiler cache (save)
221+
if: always()
222+
uses: actions/cache/save@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
223+
with:
224+
path: ${{ github.workspace }}/.ccache
225+
key: ccache-diag-${{ github.ref }}-${{ github.sha }}
226+
186227
test-tsan:
187228
strategy:
188229
fail-fast: false

‎Makefile.cbm‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,47 @@ lint: lint-tidy lint-cppcheck lint-format lint-no-suppress
10621062
lint-ci: lint-cppcheck lint-format lint-no-suppress
10631063
@echo "=== CI linters passed ==="
10641064

1065+
# ── Local memory-diagnostic lanes (not PR-CI gates by decision: the diag
1066+
# build doubles a runner's bill for a marginal delta, and a gating analyzer
1067+
# lane needs a suppression story the NOLINT ban deliberately forbids.
1068+
# Run these on the local ladder; both found real bugs on first use.) ──
1069+
1070+
# Newest pinned toolchain + ASan/UBSan + straighter stacks. Separate BUILD_DIR
1071+
# so the shipping-toolchain build stays untouched.
1072+
DIAG_LLVM_BIN ?= /opt/homebrew/opt/llvm/bin
1073+
diag:
1074+
@echo "=== diagnostic lane: $(shell $(DIAG_LLVM_BIN)/clang --version | head -1) ==="
1075+
$(MAKE) -f Makefile.cbm build/diag/test-runner \
1076+
CC=$(DIAG_LLVM_BIN)/clang CXX=$(DIAG_LLVM_BIN)/clang++ BUILD_DIR=build/diag \
1077+
SANITIZE="-fsanitize=address,undefined -fno-omit-frame-pointer -fno-optimize-sibling-calls"
1078+
./build/diag/test-runner
1079+
1080+
# Path-sensitive memory analysis: leak paths, null derefs, uninitialized reads.
1081+
# Non-gating: triage findings against the code (this lane's first run produced
1082+
# 9 real fixes and 6 recorded false positives — see the analyzer batch commit).
1083+
LINT_MEM_CHECKS = -*,clang-analyzer-unix.Malloc,clang-analyzer-unix.MallocSizeof,clang-analyzer-core.NullDereference,clang-analyzer-core.CallAndMessage,clang-analyzer-core.UndefinedBinaryOperatorResult,clang-analyzer-core.uninitialized.*,clang-analyzer-cplusplus.NewDelete,clang-analyzer-cplusplus.NewDeleteLeaks
1084+
lint-mem:
1085+
@echo "=== clang-analyzer memory lane (non-gating; triage, don't suppress) ==="
1086+
@printf '%s\n' $(LINT_SRCS) | xargs -P 10 -I{} $(CLANG_TIDY) --quiet \
1087+
--checks='$(LINT_MEM_CHECKS)' {} -- $(CFLAGS_COMMON) $(SYSROOT_FLAG) 2>/dev/null; true
1088+
1089+
# Gating variant: identical run, but vendored-tree diagnostics are excluded by
1090+
# an explicit path filter (analyzer diagnostics bypass --header-filter by
1091+
# design; this mirrors the .cppcheck vendored suppression) and any remaining
1092+
# finding fails the target. A finding here is a leak path, null deref, or
1093+
# uninitialized read the analyzer can prove — triage it against the code; a
1094+
# false positive gets restructured-for-clarity or recorded in the analyzer
1095+
# batch commit, never NOLINT'd.
1096+
lint-mem-ci:
1097+
@echo "=== clang-analyzer memory gate ==="
1098+
@printf '%s\n' $(LINT_SRCS) | xargs -P 4 -I{} $(CLANG_TIDY) --quiet \
1099+
--checks='$(LINT_MEM_CHECKS)' {} -- $(CFLAGS_COMMON) $(SYSROOT_FLAG) 2>/dev/null \
1100+
| grep -E "error:.*clang-analyzer" | grep -v "/vendored/" > lint-mem-findings.txt; \
1101+
if [ -s lint-mem-findings.txt ]; then \
1102+
echo "=== memory-analyzer findings (gate FAILS) ==="; \
1103+
cat lint-mem-findings.txt; rm -f lint-mem-findings.txt; exit 1; \
1104+
fi; rm -f lint-mem-findings.txt; echo "=== memory gate clean ==="
1105+
10651106
# ── Security audit (6 layers) ────────────────────────────────────
10661107

10671108
# Run all security checks: static audit, binary strings, UI, install, network

‎internal/cbm/cbm.c‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -653,10 +653,23 @@ static void cbm_quarantine_load(void) {
653653
* never freed: the set lives for the whole (short-lived worker) process.
654654
* The value stores the phase so cbm_index_quarantine_phase() can report
655655
* "crash" vs "hang"; membership (cbm_index_is_quarantined) is value != NULL. */
656-
char *key = cbm_strdup(line);
657656
char *pval = cbm_strdup(phase);
658-
if (key && pval) {
659-
cbm_ht_set(set, key, (void *)pval);
657+
if (!pval) {
658+
continue;
659+
}
660+
if (cbm_ht_has(set, line)) {
661+
/* Duplicate path line: reuse the stored key (the table borrows key
662+
* pointers, so a fresh copy would leak on replace) and free the
663+
* value it displaces. */
664+
free(cbm_ht_set(set, line, (void *)pval));
665+
} else {
666+
char *key = cbm_strdup(line);
667+
if (key) {
668+
cbm_ht_set(set, key, (void *)pval);
669+
} else {
670+
/* Partial failure: don't leak the value copy. */
671+
free(pval);
672+
}
660673
}
661674
}
662675
(void)fclose(f);

‎internal/cbm/sqlite_writer.c‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,15 @@ static void pb_flush_leaf(PageBuilder *pb) {
513513
pb->leaf_cap = old_cap == 0 ? INITIAL_LEAF_CAP : old_cap * GROWTH_FACTOR;
514514
void *tmp = realloc(pb->leaves, (size_t)pb->leaf_cap * sizeof(PageRef));
515515
if (!tmp) {
516+
/* Leave a CONSISTENT empty state: leaf_count stale at >=1 with a
517+
* NULL leaves array walked pb_finalize_* straight into
518+
* leaves[0] (null deref, clang-analyzer traced it) whenever the
519+
* final cell block was already flushed. Empty state routes every
520+
* finalize path to its existing root=0 failure return. */
516521
free(pb->leaves);
517522
pb->leaves = NULL;
523+
pb->leaf_count = 0;
524+
pb->leaf_cap = 0;
518525
return;
519526
}
520527
pb->leaves = (PageRef *)tmp;
@@ -1050,8 +1057,11 @@ static bool pb_ensure_leaf_cap(PageBuilder *pb) {
10501057
pb->leaf_cap = pb->leaf_cap == 0 ? INITIAL_LEAF_CAP : pb->leaf_cap * GROWTH_FACTOR;
10511058
void *tmp = realloc(pb->leaves, (size_t)pb->leaf_cap * sizeof(PageRef));
10521059
if (!tmp) {
1060+
/* Same consistent-empty contract as pb_flush_leaf's growth path. */
10531061
free(pb->leaves);
10541062
pb->leaves = NULL;
1063+
pb->leaf_count = 0;
1064+
pb->leaf_cap = 0;
10551065
return false;
10561066
}
10571067
pb->leaves = (PageRef *)tmp;

‎src/cli/cli.c‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6445,7 +6445,10 @@ int cbm_config_delete(cbm_config_t *cfg, const char *key) {
64456445
/* ── Config CLI subcommand ────────────────────────────────────── */
64466446

64476447
int cbm_cmd_config(int argc, char **argv) {
6448-
if (argc == 0 || (argv && (strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "-h") == 0))) {
6448+
/* NULL argv with a nonzero argc previously slipped past this guard (the
6449+
* inner `argv &&` shielded only the help comparison) and dereferenced
6450+
* argv[0] below -- caught by the clang-analyzer lane. */
6451+
if (argc == 0 || !argv || strcmp(argv[0], "--help") == 0 || strcmp(argv[0], "-h") == 0) {
64496452
printf("Usage: codebase-memory-mcp config <command> [args]\n\n");
64506453
printf("Commands:\n");
64516454
printf(" list Show all config values\n");

‎src/cli/config_yaml_edit.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2557,8 +2557,11 @@ static int yaml_sequence_parse_block(const yaml_doc_t *doc, size_t begin_line, s
25572557
const char *identity_value,
25582558
yaml_mapping_sequence_target_t *target) {
25592559
size_t range_len = end_line - begin_line;
2560-
size_t *starts = range_len ? (size_t *)calloc(range_len, sizeof(*starts)) : NULL;
2561-
if (range_len && !starts) {
2560+
/* Minimum one element: a NULL-for-empty-range starts is only safe through
2561+
* the loop-bound == alloc-bound invariant, which a path-sensitive
2562+
* analyzer (rightly) refuses to assume across expressions. */
2563+
size_t *starts = (size_t *)calloc(range_len ? range_len : 1, sizeof(*starts));
2564+
if (!starts) {
25622565
return YAML_ERROR;
25632566
}
25642567
size_t start_count = 0U;

‎src/cypher/cypher.c‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4204,8 +4204,11 @@ static void ret_agg_init_group(ret_agg_entry_t *entry, const char *key, int item
42044204
entry->group_vals = calloc(item_count, sizeof(const char *));
42054205
entry->sums = calloc(item_count, sizeof(double));
42064206
entry->counts = calloc(item_count, sizeof(int));
4207-
entry->mins = malloc(item_count * sizeof(double));
4208-
entry->maxs = malloc(item_count * sizeof(double));
4207+
/* calloc: the sentinel loop below overwrites every slot, but the bound
4208+
* equality with the accumulate loop is invisible to path-sensitive
4209+
* analysis; zeroed backing keeps any modeled mismatch defined. */
4210+
entry->mins = calloc(item_count, sizeof(double));
4211+
entry->maxs = calloc(item_count, sizeof(double));
42094212
entry->collect_lists = calloc(item_count, sizeof(char **));
42104213
entry->collect_counts = calloc(item_count, sizeof(int));
42114214
for (int ci = 0; ci < item_count; ci++) {

‎src/mcp/mcp.c‎

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4598,9 +4598,17 @@ static bool scc_build(const int64_t *src, const int64_t *tgt, int ecount, scc_gr
45984598
memset(g, 0, sizeof(*g));
45994599
return false;
46004600
}
4601-
/* two-pass CSR fill */
4601+
/* two-pass CSR fill. Every endpoint is in ids[] by construction (ids is
4602+
* built from these same edge arrays), so a negative lookup is unreachable
4603+
* -- but that invariant is invisible to path-sensitive analysis, and a
4604+
* guard keeps a future collection bug from becoming an OOB write. Both
4605+
* passes must skip identically or the cursors desync. */
46024606
for (int i = 0; i < ecount; i++) {
46034607
int u = scc_id_index(g->ids, nv, src[i]);
4608+
int v = scc_id_index(g->ids, nv, tgt[i]);
4609+
if (u < 0 || v < 0) {
4610+
continue;
4611+
}
46044612
g->adj_head[u + 1]++;
46054613
}
46064614
for (int i = 0; i < nv; i++) {
@@ -4617,13 +4625,18 @@ static bool scc_build(const int64_t *src, const int64_t *tgt, int ecount, scc_gr
46174625
for (int i = 0; i < nv; i++) {
46184626
cursor[i] = g->adj_head[i];
46194627
}
4628+
int filled = 0;
46204629
for (int i = 0; i < ecount; i++) {
46214630
int u = scc_id_index(g->ids, nv, src[i]);
46224631
int v = scc_id_index(g->ids, nv, tgt[i]);
4632+
if (u < 0 || v < 0) {
4633+
continue;
4634+
}
46234635
g->adj[cursor[u]++] = v;
4636+
filled++;
46244637
}
46254638
free(cursor);
4626-
g->nedges = ecount;
4639+
g->nedges = filled;
46274640
return true;
46284641
}
46294642

@@ -4856,6 +4869,14 @@ static int arch_compute_cycles(cbm_store_t *store, const char *project, int64_t
48564869
scc_free(&g);
48574870
return CBM_STORE_ERR;
48584871
}
4872+
/* Tarjan assigns every vertex a component, so ncomp >= 1 whenever
4873+
* nverts >= 1 -- state it, so the zero-size-allocation path below is
4874+
* provably confined to the empty graph. */
4875+
if (g.nverts > 0 && ncomp <= 0) {
4876+
free(comp);
4877+
scc_free(&g);
4878+
return CBM_STORE_ERR;
4879+
}
48594880
/* size per component */
48604881
int *csize = calloc((size_t)ncomp, sizeof(int));
48614882
if (!csize) {
@@ -4915,10 +4936,15 @@ static int arch_compute_cycles(cbm_store_t *store, const char *project, int64_t
49154936
scc_free(&g);
49164937
return CBM_STORE_ERR;
49174938
}
4918-
for (int v = 0; v < g.nverts; v++) {
4919-
int sl = slot[comp[v]];
4920-
if (sl >= 0) {
4921-
members[sl][fill[sl]++] = g.ids[v];
4939+
/* With ncyc == 0 no slot is ever >= 0 (the slot loop can't assign one),
4940+
* so members -- NULL in that case -- is never indexed; make that
4941+
* invariant local instead of cross-loop so it is checkable. */
4942+
if (ncyc > 0) {
4943+
for (int v = 0; v < g.nverts; v++) {
4944+
int sl = slot[comp[v]];
4945+
if (sl >= 0) {
4946+
members[sl][fill[sl]++] = g.ids[v];
4947+
}
49224948
}
49234949
}
49244950
free(fill);
@@ -4961,9 +4987,12 @@ static void arch_node_qn(cbm_store_t *store, int64_t id, char *out, size_t outsz
49614987

49624988
static char *handle_get_architecture(cbm_mcp_server_t *srv, const char *args) {
49634989
char *project = get_project_arg(args);
4964-
char *scope_path = cbm_mcp_get_string_arg(args, "path");
49654990
cbm_store_t *store = resolve_store(srv, project);
4991+
/* REQUIRE_STORE returns without freeing anything but `project`, so every
4992+
* other allocation must come after it (scope_path leaked here before —
4993+
* caught by the clang-analyzer unix.Malloc lane). */
49664994
REQUIRE_STORE(store, project);
4995+
char *scope_path = cbm_mcp_get_string_arg(args, "path");
49674996

49684997
char *not_indexed = verify_project_indexed(store, project);
49694998
if (not_indexed) {
@@ -11408,6 +11437,11 @@ static int read_bounded_line(FILE *in, char **line, size_t *cap, size_t max_byte
1140811437
if (!grown) {
1140911438
return CBM_NOT_FOUND;
1141011439
}
11440+
/* Zero the tail: every byte of the buffer is then defined in any
11441+
* caller's model (parse_content_length reads up to one byte past
11442+
* the matched prefix), and a future over-read degrades to reading
11443+
* NULs instead of undefined memory. One memset per growth step. */
11444+
memset(grown + len, 0, new_cap - len);
1141111445
*line = grown;
1141211446
*cap = new_cap;
1141311447
}

‎src/pipeline/pass_calls.c‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,13 @@ static void emit_http_async_edge(cbm_pipeline_ctx_t *ctx, const CBMCall *call,
345345
* substring coincidence in the resolved QN (e.g. "SalesforceRestClient"
346346
* matches the "RestClient" HTTP lib). Emit a plain CALLS edge — unless a
347347
* weak TS/JS member-call match should be suppressed (#592/#606). */
348-
if (suppress_plain_calls) {
348+
/* !target: the service-pattern call sites pass NULL (the route node is
349+
* synthesized below) behind a hand-duplicated copy of the is_url/
350+
* is_topic predicate above. While the copies agree this branch is
351+
* unreachable for them -- but a drift between the two would turn
352+
* target->id into a null dereference (clang-analyzer traced exactly
353+
* that), and with no callee node there is nothing to emit anyway. */
354+
if (suppress_plain_calls || !target) {
349355
return;
350356
}
351357
char esc_callee[CBM_SZ_256];

0 commit comments

Comments
 (0)