Skip to content

Commit 7729a8e

Browse files
committed
fix(mcp,cypher): allocate the new query-path memory through the memory core
lint-memory-core failed on DeusData#2642: the new search_code scanner, result index, root containment cache and Cypher sort/group/distinct scratch used malloc/calloc/realloc/free directly (mcp.c +22, cypher.c +26, git_context.c +1). They now go through cbm_alloc/cbm_calloc/cbm_realloc/cbm_free/cbm_mem_strdup: file buffers as CBM_MEM_CLASS_EXTRACT, the rest as CBM_MEM_CLASS_OTHER, each block freed with the class it was allocated with. The result index reserves its slot before a result's strings exist, so its insert cannot fail and needs no cleanup; cbm_git_branch_resolve writes into caller buffers instead of returning heap strings. cypher.c ends below its baseline (128 < 132), which is tightened. Output unchanged: 42/42 tool and Cypher responses identical on django; mcp, cypher and git_context suites green under ASan. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 04a67f8 commit 7729a8e

6 files changed

Lines changed: 123 additions & 105 deletions

File tree

‎scripts/memory-core-baseline.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ src/cli/config_text_edit.c 72
2424
src/cli/config_toml_edit.c 73
2525
src/cli/config_yaml_edit.c 117
2626
src/cli/hook_augment.c 31
27-
src/cypher/cypher.c 132
27+
src/cypher/cypher.c 128
2828
src/daemon/application.c 107
2929
src/daemon/bootstrap.c 13
3030
src/daemon/daemon.c 31

‎src/cypher/cypher.c‎

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3733,7 +3733,7 @@ static void cypher_stable_sort(int *idx, int n, cypher_index_cmp_fn cmp, const v
37333733
if (n < PAIR_LEN) {
37343734
return;
37353735
}
3736-
int *tmp = malloc((size_t)n * sizeof(int));
3736+
int *tmp = cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)n * sizeof(int));
37373737
if (!tmp) {
37383738
for (int i = SKIP_ONE; i < n; i++) {
37393739
for (int j = i; j > 0 && cmp(ctx, idx[j - SKIP_ONE], idx[j]) > 0; j--) {
@@ -3763,7 +3763,7 @@ static void cypher_stable_sort(int *idx, int n, cypher_index_cmp_fn cmp, const v
37633763
}
37643764
memcpy(idx, tmp, (size_t)n * sizeof(int));
37653765
}
3766-
free(tmp);
3766+
cbm_free(CBM_MEM_CLASS_OTHER, tmp);
37673767
}
37683768

37693769
/* String key → the index it was first recorded with. Keys are copied: the
@@ -3797,13 +3797,13 @@ static void key_index_add(cypher_key_index_t *ix, const char *key, int index) {
37973797
}
37983798
if (ix->table && ix->count >= ix->cap) {
37993799
int cap = ix->cap ? ix->cap * PAIR_LEN : CBM_SZ_256;
3800-
char **keys = realloc(ix->keys, (size_t)cap * sizeof(char *));
3800+
char **keys = cbm_realloc(CBM_MEM_CLASS_OTHER, ix->keys, (size_t)cap * sizeof(char *));
38013801
if (keys) {
38023802
ix->keys = keys;
38033803
ix->cap = cap;
38043804
}
38053805
}
3806-
char *copy = ix->table && ix->count < ix->cap ? heap_strdup(key) : NULL;
3806+
char *copy = ix->table && ix->count < ix->cap ? cbm_mem_strdup(CBM_MEM_CLASS_OTHER, key) : NULL;
38073807
if (!copy) {
38083808
ix->failed = true;
38093809
return;
@@ -3817,9 +3817,9 @@ static void key_index_free(cypher_key_index_t *ix) {
38173817
cbm_ht_free(ix->table);
38183818
}
38193819
for (int i = 0; i < ix->count; i++) {
3820-
free(ix->keys[i]);
3820+
cbm_free(CBM_MEM_CLASS_OTHER, ix->keys[i]);
38213821
}
3822-
free(ix->keys);
3822+
cbm_free(CBM_MEM_CLASS_OTHER, ix->keys);
38233823
memset(ix, 0, sizeof(*ix));
38243824
}
38253825

@@ -3925,12 +3925,12 @@ static void rb_apply_order_by(result_builder_t *rb, const cbm_return_clause_t *r
39253925
}
39263926
int n = rb->row_count;
39273927
rb_order_ctx_t ctx = {.cols = cols, .numeric = numeric, .descs = descs, .keys = keys};
3928-
int *idx = malloc((size_t)n * sizeof(int));
3929-
const char ***rows = malloc((size_t)n * sizeof(const char **));
3928+
int *idx = cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)n * sizeof(int));
3929+
const char ***rows = cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)n * sizeof(const char **));
39303930
if (!idx || !rows) {
39313931
/* No scratch memory: insertion sort in place, same comparisons. */
3932-
free(idx);
3933-
free(rows);
3932+
cbm_free(CBM_MEM_CLASS_OTHER, idx);
3933+
cbm_free(CBM_MEM_CLASS_OTHER, rows);
39343934
for (int i = SKIP_ONE; i < n; i++) {
39353935
for (int j = i; j > 0 && rb_order_compare_rows(&ctx, rb->rows[j - SKIP_ONE],
39363936
rb->rows[j], NULL, NULL) > 0;
@@ -3942,7 +3942,7 @@ static void rb_apply_order_by(result_builder_t *rb, const cbm_return_clause_t *r
39423942
}
39433943
return;
39443944
}
3945-
int *values = malloc((size_t)n * (size_t)keys * sizeof(int));
3945+
int *values = cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)n * (size_t)keys * sizeof(int));
39463946
for (int r = 0; r < n; r++) {
39473947
idx[r] = r;
39483948
rows[r] = rb->rows[r];
@@ -3957,9 +3957,9 @@ static void rb_apply_order_by(result_builder_t *rb, const cbm_return_clause_t *r
39573957
for (int r = 0; r < n; r++) {
39583958
rb->rows[r] = rows[idx[r]];
39593959
}
3960-
free(values);
3961-
free(rows);
3962-
free(idx);
3960+
cbm_free(CBM_MEM_CLASS_OTHER, values);
3961+
cbm_free(CBM_MEM_CLASS_OTHER, rows);
3962+
cbm_free(CBM_MEM_CLASS_OTHER, idx);
39633963
}
39643964

39653965
static void rb_apply_skip_limit(result_builder_t *rb, int skip_n, int limit,
@@ -4008,7 +4008,7 @@ static const char *rb_row_key(const result_builder_t *rb, const char **row, char
40084008
need += strlen(row[c]) + CBM_SZ_32;
40094009
}
40104010
if (need > *cap) {
4011-
char *grown = realloc(*buf, need);
4011+
char *grown = cbm_realloc(CBM_MEM_CLASS_OTHER, *buf, need);
40124012
if (!grown) {
40134013
return NULL;
40144014
}
@@ -4075,7 +4075,7 @@ static void rb_apply_distinct(result_builder_t *rb) {
40754075
}
40764076
rb->row_count = kept;
40774077
key_index_free(&seen);
4078-
free(key_buf);
4078+
cbm_free(CBM_MEM_CLASS_OTHER, key_buf);
40794079
}
40804080

40814081
static void rb_free(result_builder_t *rb) {
@@ -4457,17 +4457,18 @@ static void sort_bindings(binding_t *vbindings, int count, const cbm_return_clau
44574457
size_t keys = (size_t)wc->order_key_count;
44584458
size_t cells = (size_t)count * keys;
44594459
with_order_ctx_t ctx = {.wc = wc,
4460-
.values = calloc(cells, sizeof(char *)),
4461-
.numbered = calloc(cells, sizeof(bool)),
4462-
.numbers = calloc(cells, sizeof(double))};
4463-
int *idx = malloc((size_t)count * sizeof(int));
4464-
binding_t *sorted = malloc((size_t)count * sizeof(binding_t));
4460+
.values = cbm_calloc(CBM_MEM_CLASS_OTHER, cells * sizeof(char *)),
4461+
.numbered = cbm_calloc(CBM_MEM_CLASS_OTHER, cells * sizeof(bool)),
4462+
.numbers = cbm_calloc(CBM_MEM_CLASS_OTHER, cells * sizeof(double))};
4463+
int *idx = cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)count * sizeof(int));
4464+
binding_t *sorted = cbm_alloc(CBM_MEM_CLASS_OTHER, (size_t)count * sizeof(binding_t));
44654465
bool ready = ctx.values && ctx.numbered && ctx.numbers && idx && sorted;
44664466
for (int i = 0; ready && i < count; i++) {
44674467
idx[i] = i;
44684468
for (size_t k = 0; ready && k < keys; k++) {
44694469
size_t cell = (size_t)i * keys + k;
4470-
ctx.values[cell] = heap_strdup(order_key_value(&vbindings[i], wc->order_keys[k]));
4470+
ctx.values[cell] = cbm_mem_strdup(CBM_MEM_CLASS_OTHER,
4471+
order_key_value(&vbindings[i], wc->order_keys[k]));
44714472
ready = ctx.values[cell] != NULL;
44724473
char *end = NULL;
44734474
ctx.numbers[cell] = ready ? strtod(ctx.values[cell], &end) : 0;
@@ -4493,13 +4494,13 @@ static void sort_bindings(binding_t *vbindings, int count, const cbm_return_clau
44934494
}
44944495
}
44954496
for (size_t c = 0; ctx.values && c < cells; c++) {
4496-
free(ctx.values[c]);
4497+
cbm_free(CBM_MEM_CLASS_OTHER, ctx.values[c]);
44974498
}
4498-
free(ctx.values);
4499-
free(ctx.numbered);
4500-
free(ctx.numbers);
4501-
free(idx);
4502-
free(sorted);
4499+
cbm_free(CBM_MEM_CLASS_OTHER, ctx.values);
4500+
cbm_free(CBM_MEM_CLASS_OTHER, ctx.numbered);
4501+
cbm_free(CBM_MEM_CLASS_OTHER, ctx.numbers);
4502+
cbm_free(CBM_MEM_CLASS_OTHER, idx);
4503+
cbm_free(CBM_MEM_CLASS_OTHER, sorted);
45034504
}
45044505

45054506
/* Apply skip and limit to a binding array, freeing discarded entries */
@@ -4629,7 +4630,8 @@ static int with_agg_find_or_create(with_agg_t **aggs, int *agg_cnt, int *agg_cap
46294630
(*aggs)[found].maxs = calloc(wc->count, sizeof(double));
46304631
(*aggs)[found].distinct_lists = calloc(wc->count, sizeof(char **));
46314632
(*aggs)[found].distinct_n = calloc(wc->count, sizeof(int));
4632-
(*aggs)[found].distinct_sets = calloc(wc->count, sizeof(CBMHashTable *));
4633+
(*aggs)[found].distinct_sets =
4634+
cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)wc->count * sizeof(CBMHashTable *));
46334635
(*aggs)[found].group_nodes = calloc(wc->count, sizeof(cbm_node_t));
46344636
for (int ci = 0; ci < wc->count; ci++) {
46354637
(*aggs)[found].mins[ci] = CYP_DBL_MAX;
@@ -4724,7 +4726,7 @@ static void with_agg_free(with_agg_t *aggs, int agg_cnt, int item_count) {
47244726
free(aggs[a].maxs);
47254727
free(aggs[a].distinct_lists);
47264728
free(aggs[a].distinct_n);
4727-
free(aggs[a].distinct_sets);
4729+
cbm_free(CBM_MEM_CLASS_OTHER, aggs[a].distinct_sets);
47284730
if (aggs[a].group_nodes) {
47294731
for (int ci = 0; ci < item_count; ci++) {
47304732
node_fields_free(&aggs[a].group_nodes[ci]);
@@ -5134,7 +5136,8 @@ static void ret_agg_init_group(ret_agg_entry_t *entry, const char *key, int item
51345136
entry->maxs = calloc(item_count, sizeof(double));
51355137
entry->collect_lists = calloc(item_count, sizeof(char **));
51365138
entry->collect_counts = calloc(item_count, sizeof(int));
5137-
entry->distinct_sets = calloc(item_count, sizeof(CBMHashTable *));
5139+
entry->distinct_sets =
5140+
cbm_calloc(CBM_MEM_CLASS_OTHER, (size_t)item_count * sizeof(CBMHashTable *));
51385141
for (int ci = 0; ci < item_count; ci++) {
51395142
entry->mins[ci] = CYP_DBL_MAX;
51405143
entry->maxs[ci] = -CYP_DBL_MAX;
@@ -5217,7 +5220,7 @@ static void ret_agg_free(ret_agg_entry_t *aggs, int agg_count, int item_count) {
52175220
free(aggs[a].maxs);
52185221
free(aggs[a].collect_lists);
52195222
free(aggs[a].collect_counts);
5220-
free(aggs[a].distinct_sets);
5223+
cbm_free(CBM_MEM_CLASS_OTHER, aggs[a].distinct_sets);
52215224
}
52225225
free(aggs);
52235226
}

‎src/git/git_context.c‎

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,14 @@ static bool git_validate_repo_path(const char *repo_path) {
4444
return cbm_validate_shell_path_arg(repo_path);
4545
}
4646

47-
static int git_capture(const char *repo_path, const char *git_args, char **out) {
48-
if (!out) {
47+
/* Run `git -C repo_path git_args` and copy the first output line (newlines
48+
* trimmed) into buf. Returns 0 only when git exited 0 with a non-empty line. */
49+
static int git_capture_into(const char *repo_path, const char *git_args, char *buf,
50+
size_t buf_size) {
51+
if (!buf || buf_size == 0) {
4952
return CBM_NOT_FOUND;
5053
}
51-
*out = NULL;
54+
buf[0] = '\0';
5255
if (!repo_path || !git_args || !git_validate_repo_path(repo_path)) {
5356
return CBM_NOT_FOUND;
5457
}
@@ -76,8 +79,8 @@ static int git_capture(const char *repo_path, const char *git_args, char **out)
7679
}
7780

7881
cbm_log_debug("git.capture", "stage", "read", "args", git_args);
79-
char buf[GIT_OUTPUT_MAX];
80-
if (!fgets(buf, sizeof(buf), fp)) {
82+
if (!fgets(buf, (int)buf_size, fp)) {
83+
buf[0] = '\0';
8184
cbm_log_debug("git.capture", "stage", "close", "args", git_args, "output", "none");
8285
cbm_pclose(fp);
8386
cbm_log_debug("git.capture", "stage", "done", "args", git_args);
@@ -89,9 +92,21 @@ static int git_capture(const char *repo_path, const char *git_args, char **out)
8992
int rc = cbm_pclose(fp);
9093
cbm_log_debug("git.capture", "stage", "done", "args", git_args);
9194
if (rc != 0 || buf[0] == '\0') {
95+
buf[0] = '\0';
9296
return CBM_NOT_FOUND;
9397
}
98+
return 0;
99+
}
94100

101+
static int git_capture(const char *repo_path, const char *git_args, char **out) {
102+
if (!out) {
103+
return CBM_NOT_FOUND;
104+
}
105+
*out = NULL;
106+
char buf[GIT_OUTPUT_MAX];
107+
if (git_capture_into(repo_path, git_args, buf, sizeof(buf)) != 0) {
108+
return CBM_NOT_FOUND;
109+
}
95110
*out = git_strdup(buf);
96111
return *out ? 0 : CBM_NOT_FOUND;
97112
}
@@ -313,28 +328,28 @@ int cbm_git_context_resolve(const char *path, cbm_git_context_t *out) {
313328
return 0;
314329
}
315330

316-
int cbm_git_branch_resolve(const char *path, bool *is_git, char **branch) {
317-
if (!is_git || !branch) {
331+
int cbm_git_branch_resolve(const char *path, bool *is_git, char *branch, size_t branch_size) {
332+
if (!is_git || !branch || branch_size == 0) {
318333
return CBM_NOT_FOUND;
319334
}
320335
*is_git = false;
321-
*branch = NULL;
336+
branch[0] = '\0';
322337
if (!path || !path[0]) {
323338
return CBM_NOT_FOUND;
324339
}
325340
if (!cbm_is_dir(path)) {
326341
return 0;
327342
}
328-
char *worktree_root = NULL;
329-
if (git_capture(path, "rev-parse --show-toplevel", &worktree_root) != 0) {
343+
char worktree_root[GIT_OUTPUT_MAX];
344+
if (git_capture_into(path, "rev-parse --show-toplevel", worktree_root, sizeof(worktree_root)) !=
345+
0) {
330346
return 0;
331347
}
332-
free(worktree_root);
333348
*is_git = true;
334-
if (git_capture(path, "symbolic-ref --quiet --short HEAD", branch) != 0) {
335-
*branch = git_strdup("DETACHED");
349+
if (git_capture_into(path, "symbolic-ref --quiet --short HEAD", branch, branch_size) != 0) {
350+
snprintf(branch, branch_size, "%s", "DETACHED");
336351
}
337-
return *branch ? 0 : CBM_NOT_FOUND;
352+
return 0;
338353
}
339354

340355
char *cbm_git_context_branch_qn(const char *project_name, const cbm_git_context_t *ctx) {

‎src/git/git_context.h‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define CBM_GIT_CONTEXT_H
33

44
#include <stdbool.h>
5+
#include <stddef.h>
56

67
typedef struct {
78
bool is_git;
@@ -21,9 +22,11 @@ typedef struct {
2122

2223
int cbm_git_context_resolve(const char *path, cbm_git_context_t *out);
2324
/* Only the is_git and branch fields of cbm_git_context_resolve, from the same
24-
* two git commands it uses for them (2 git processes instead of 7). *branch
25-
* is heap-owned, NULL when path is not in a git work tree. */
26-
int cbm_git_branch_resolve(const char *path, bool *is_git, char **branch);
25+
* two git commands it uses for them (2 git processes instead of 7). branch
26+
* gets "" when path is not in a git work tree; pass CBM_GIT_BRANCH_MAX bytes
27+
* to read exactly what cbm_git_context_resolve reads. */
28+
#define CBM_GIT_BRANCH_MAX 4096
29+
int cbm_git_branch_resolve(const char *path, bool *is_git, char *branch, size_t branch_size);
2730
void cbm_git_context_free(cbm_git_context_t *ctx);
2831
char *cbm_git_context_branch_qn(const char *project_name, const cbm_git_context_t *ctx);
2932
int cbm_git_context_props_json(const cbm_git_context_t *ctx, char *buf, int buf_size);

0 commit comments

Comments
 (0)