Skip to content

Commit 1a42a23

Browse files
cklausclaude
andcommitted
fix(store): OOM safety, SQL truncation guard, WAL timeout, JSON property validation
- Bug DeusData#7: query_neighbor_names() — check malloc() return value and handle strdup() failure by freeing previously-duped strings, finalizing the statement, and returning CBM_NOT_FOUND instead of crashing. - Bug DeusData#11: cbm_store_search() — check snprintf return value for the order_limit buffer and verify the suffix fits in the remaining sql capacity before calling strncat; return CBM_STORE_ERR on overflow. - Bug DeusData#19: configure_pragmas() — set sqlite3_busy_timeout(5000 ms) before the WAL checkpoint to prevent startup hangs on a corrupt/locked DB; log a warning via cbm_log_warn if the checkpoint fails (non-fatal). - Bug DeusData#24: cbm_store_upsert_node() — validate properties JSON before binding: if the first non-whitespace byte is not '{' or '[', log a warning and substitute '{}' to prevent malformed JSON from propagating into the nodes table. Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
1 parent 9d1beb1 commit 1a42a23

1 file changed

Lines changed: 61 additions & 6 deletions

File tree

‎src/store/store.c‎

Lines changed: 61 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,17 @@ static int configure_pragmas(cbm_store_t *s, bool in_memory) {
347347
/* Recover stale WAL from previous crash (best-effort).
348348
* PASSIVE never blocks readers and never ftruncates.
349349
* May fail with SQLITE_BUSY if another process holds a lock. */
350-
(void)sqlite3_exec(s->db, "PRAGMA wal_checkpoint(PASSIVE)", NULL, NULL, NULL);
350+
sqlite3_busy_timeout(s->db, 5000); /* 5 s — prevents hang on corrupt/locked WAL */
351+
{
352+
char *ckpt_err = NULL;
353+
int ckpt_rc = sqlite3_exec(s->db, "PRAGMA wal_checkpoint(PASSIVE)", NULL, NULL,
354+
&ckpt_err);
355+
if (ckpt_rc != SQLITE_OK) {
356+
cbm_log_warn("store.wal_checkpoint_failed", "rc",
357+
ckpt_err ? ckpt_err : "unknown", NULL);
358+
sqlite3_free(ckpt_err);
359+
}
360+
}
351361
rc = exec_sql(s, "PRAGMA synchronous = NORMAL;");
352362
if (rc != CBM_STORE_OK) {
353363
return rc;
@@ -1051,7 +1061,24 @@ int64_t cbm_store_upsert_node(cbm_store_t *s, const cbm_node_t *n) {
10511061
bind_text(stmt, ST_COL_5, safe_str(n->file_path));
10521062
sqlite3_bind_int(stmt, ST_COL_6, n->start_line);
10531063
sqlite3_bind_int(stmt, ST_COL_7, n->end_line);
1054-
bind_text(stmt, ST_COL_8, safe_props(n->properties_json));
1064+
1065+
/* Validate properties JSON: if the first non-whitespace character is not
1066+
* '{' or '[' the string is not valid JSON — substitute an empty object
1067+
* rather than letting malformed JSON propagate into the database. */
1068+
const char *props = safe_props(n->properties_json);
1069+
{
1070+
const char *p = props;
1071+
while (*p == ' ' || *p == '\t' || *p == '\r' || *p == '\n') {
1072+
p++;
1073+
}
1074+
if (*p != '{' && *p != '[') {
1075+
cbm_log_warn("store.upsert_node.invalid_json",
1076+
"qn", safe_str(n->qualified_name),
1077+
"props_preview", props, NULL);
1078+
props = "{}";
1079+
}
1080+
}
1081+
bind_text(stmt, ST_COL_8, props);
10551082

10561083
int rc = sqlite3_step(stmt);
10571084
if (rc == SQLITE_ROW) {
@@ -1861,6 +1888,12 @@ static int query_neighbor_names(sqlite3 *db, const char *sql, int64_t node_id, i
18611888

18621889
int cap = ST_INIT_CAP_8;
18631890
char **names = malloc((size_t)cap * sizeof(char *));
1891+
if (!names) {
1892+
sqlite3_finalize(stmt);
1893+
*out = NULL;
1894+
*out_count = 0;
1895+
return CBM_NOT_FOUND;
1896+
}
18641897
int count = 0;
18651898
while (sqlite3_step(stmt) == SQLITE_ROW) {
18661899
const char *name = (const char *)sqlite3_column_text(stmt, 0);
@@ -1871,7 +1904,18 @@ static int query_neighbor_names(sqlite3 *db, const char *sql, int64_t node_id, i
18711904
cap *= ST_GROWTH;
18721905
names = safe_realloc(names, (size_t)cap * sizeof(char *));
18731906
}
1874-
names[count++] = strdup(name);
1907+
char *duped = strdup(name);
1908+
if (!duped) {
1909+
for (int i = 0; i < count; i++) {
1910+
free(names[i]);
1911+
}
1912+
free(names);
1913+
sqlite3_finalize(stmt);
1914+
*out = NULL;
1915+
*out_count = 0;
1916+
return CBM_NOT_FOUND;
1917+
}
1918+
names[count++] = duped;
18751919
}
18761920
sqlite3_finalize(stmt);
18771921
*out = names;
@@ -2507,9 +2551,20 @@ int cbm_store_search(cbm_store_t *s, const cbm_search_params_t *params, cbm_sear
25072551
int offset = params->offset;
25082552
const char *name_col = has_degree_filter ? "name" : "n.name";
25092553
char order_limit[CBM_SZ_128];
2510-
snprintf(order_limit, sizeof(order_limit), " ORDER BY %s LIMIT %d OFFSET %d", name_col, limit,
2511-
offset);
2512-
strncat(sql, order_limit, sizeof(sql) - strlen(sql) - 1);
2554+
int ol_len = snprintf(order_limit, sizeof(order_limit), " ORDER BY %s LIMIT %d OFFSET %d",
2555+
name_col, limit, offset);
2556+
if (ol_len < 0 || ol_len >= (int)sizeof(order_limit)) {
2557+
store_set_error(s, "search: order_limit buffer overflow");
2558+
like_pool_free(&like_pool);
2559+
return CBM_STORE_ERR;
2560+
}
2561+
size_t sql_remaining = sizeof(sql) - strlen(sql) - 1;
2562+
if ((size_t)ol_len > sql_remaining) {
2563+
store_set_error(s, "search: sql buffer too small for ORDER BY clause");
2564+
like_pool_free(&like_pool);
2565+
return CBM_STORE_ERR;
2566+
}
2567+
strncat(sql, order_limit, sql_remaining);
25132568

25142569
/* Execute count query */
25152570
sqlite3_stmt *cnt_stmt = NULL;

0 commit comments

Comments
 (0)