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(cypher,store): close residual OOM gaps in #206

Martin Vogel9653e7230904
diff --git a/src/cypher/cypher.c b/src/cypher/cypher.c
index 648e8607c..c75b7eed4 100644
--- a/src/cypher/cypher.c
+++ b/src/cypher/cypher.c
@@ -516,8 +516,20 @@ static int parse_props(parser_t *p, cbm_prop_filter_t **out, int *count) {
arr = tmp;
cap = new_cap;
}
- arr[n].key = heap_strdup(key->text);
- arr[n].value = heap_strdup(val->text);
+ const char *new_key = heap_strdup(key->text);
+ const char *new_val = heap_strdup(val->text);
+ if (!new_key || !new_val) {
+ free((void *)new_key);
+ free((void *)new_val);
+ for (int i = 0; i < n; i++) {
+ free((void *)arr[i].key);
+ free((void *)arr[i].value);
+ }
+ free(arr);
+ return CBM_NOT_FOUND;
+ }
+ arr[n].key = new_key;
+ arr[n].value = new_val;
n++;
match(p, TOK_COMMA); /* optional comma */
@@ -604,7 +616,12 @@ static int parse_rel_types(parser_t *p, cbm_rel_pattern_t *out) {
free(types);
return CBM_NOT_FOUND;
}
- types[n++] = heap_strdup(t->text);
+ const char *first_type = heap_strdup(t->text);
+ if (!first_type) {
+ free(types);
+ return CBM_NOT_FOUND;
+ }
+ types[n++] = first_type;
while (match(p, TOK_PIPE)) {
t = expect(p, TOK_IDENT);
@@ -628,7 +645,15 @@ static int parse_rel_types(parser_t *p, cbm_rel_pattern_t *out) {
types = (const char **)tmp;
cap = new_cap;
}
- types[n++] = heap_strdup(t->text);
+ const char *next_type = heap_strdup(t->text);
+ if (!next_type) {
+ for (int i = 0; i < n; i++) {
+ free((void *)types[i]);
+ }
+ free(types);
+ return CBM_NOT_FOUND;
+ }
+ types[n++] = next_type;
}
out->types = types;
@@ -791,6 +816,11 @@ static cbm_expr_t *parse_or_expr(parser_t *p);
static cbm_expr_t *parse_in_list(parser_t *p, cbm_condition_t *c) {
advance(p);
c->op = heap_strdup("IN");
+ if (!c->op) {
+ free((void *)c->variable);
+ free((void *)c->property);
+ return NULL;
+ }
if (!expect(p, TOK_LBRACKET)) {
free((void *)c->variable);
free((void *)c->property);
@@ -827,7 +857,18 @@ static cbm_expr_t *parse_in_list(parser_t *p, cbm_condition_t *c) {
vals = (const char **)tmp;
vcap = new_vcap;
}
- vals[vn++] = heap_strdup(advance(p)->text);
+ const char *new_val = heap_strdup(advance(p)->text);
+ if (!new_val) {
+ for (int i = 0; i < vn; i++) {
+ free((void *)vals[i]);
+ }
+ free((void *)vals);
+ free((void *)c->variable);
+ free((void *)c->property);
+ free((void *)c->op);
+ return NULL;
+ }
+ vals[vn++] = new_val;
} else {
break;
}
@@ -1140,8 +1181,10 @@ static cbm_case_expr_t *parse_case_expr(parser_t *p) {
void *tmp = realloc(kase->branches, new_bcap * sizeof(cbm_case_branch_t));
if (!tmp) {
expr_free(when);
+ free((void *)then_val);
for (int i = 0; i < kase->branch_count; i++) {
expr_free(kase->branches[i].when_expr);
+ free((void *)kase->branches[i].then_val);
}
free(kase->branches);
free(kase);
diff --git a/src/store/store.c b/src/store/store.c
index 2460557c7..1c8434726 100644
--- a/src/store/store.c
+++ b/src/store/store.c
@@ -2748,10 +2748,24 @@ int cbm_store_get_schema(cbm_store_t *s, const char *project, cbm_schema_info_t
int cap = ST_INIT_CAP_8;
int n = 0;
cbm_label_count_t *arr = malloc(cap * sizeof(cbm_label_count_t));
+ if (!arr) {
+ sqlite3_finalize(stmt);
+ return CBM_NOT_FOUND;
+ }
while (sqlite3_step(stmt) == SQLITE_ROW) {
if (n >= cap) {
- cap *= ST_GROWTH;
- arr = safe_realloc(arr, cap * sizeof(cbm_label_count_t));
+ int new_cap = cap * ST_GROWTH;
+ void *tmp = realloc(arr, new_cap * sizeof(cbm_label_count_t));
+ if (!tmp) {
+ for (int i = 0; i < n; i++) {
+ free((void *)arr[i].label);
+ }
+ free(arr);
+ sqlite3_finalize(stmt);
+ return CBM_NOT_FOUND;
+ }
+ arr = tmp;
+ cap = new_cap;
}
arr[n].label = heap_strdup((const char *)sqlite3_column_text(stmt, 0));
arr[n].count = sqlite3_column_int(stmt, SKIP_ONE);
@@ -2800,10 +2814,26 @@ int cbm_store_get_schema(cbm_store_t *s, const char *project, cbm_schema_info_t
int cap = ST_INIT_CAP_8;
int n = 0;
cbm_type_count_t *arr = malloc(cap * sizeof(cbm_type_count_t));
+ if (!arr) {
+ sqlite3_finalize(stmt);
+ cbm_store_schema_free(out);
+ return CBM_NOT_FOUND;
+ }
while (sqlite3_step(stmt) == SQLITE_ROW) {
if (n >= cap) {
- cap *= ST_GROWTH;
- arr = safe_realloc(arr, cap * sizeof(cbm_type_count_t));
+ int new_cap = cap * ST_GROWTH;
+ void *tmp = realloc(arr, new_cap * sizeof(cbm_type_count_t));
+ if (!tmp) {
+ for (int i = 0; i < n; i++) {
+ free((void *)arr[i].type);
+ }
+ free(arr);
+ sqlite3_finalize(stmt);
+ cbm_store_schema_free(out);
+ return CBM_NOT_FOUND;
+ }
+ arr = tmp;
+ cap = new_cap;
}
arr[n].type = heap_strdup((const char *)sqlite3_column_text(stmt, 0));
arr[n].count = sqlite3_column_int(stmt, SKIP_ONE);