Skip to content

Commit 99c3bb4

Browse files
committed
fix(store): report a failed COUNT read instead of returning zero
cbm_store_count_nodes and cbm_store_count_edges treat every sqlite3_step result other than SQLITE_ROW as a count of zero. A read that failed — SQLITE_CORRUPT, SQLITE_BUSY, SQLITE_IOERR — is therefore indistinguishable from a project that genuinely holds no rows, and index_status renders it as the positive assertion status "empty". A user or agent reading that concludes the repository was never indexed and starts a multi-minute re-index, while the corruption itself is never surfaced. Both functions already have an error channel: each returns CBM_STORE_ERR when prepare_cached fails. Only the step result was not reported through it. index_status is already written for that value — it sets degraded on a negative node count and clamps a negative edge count — so the guard existed and simply never fired. Initialise count to CBM_STORE_ERR so a non-row step is reported as a failed read. A successful step still overwrites it with the real count, so the healthy path is unchanged. Closes DeusData#2012 Signed-off-by: kavish-19 <[email protected]>
1 parent fe85a6b commit 99c3bb4

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

‎src/store/store.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3069,7 +3069,11 @@ int cbm_store_count_nodes(cbm_store_t *s, const char *project) {
30693069
}
30703070

30713071
bind_text(stmt, SKIP_ONE, project);
3072-
int count = 0;
3072+
/* A step that does not yield a row is a failed read (SQLITE_CORRUPT,
3073+
* SQLITE_BUSY, SQLITE_IOERR), not a count of zero. Report it through the
3074+
* error channel this function already uses for a failed prepare, so a
3075+
* caller cannot mistake an unreadable table for an empty project. */
3076+
int count = CBM_STORE_ERR;
30733077
if (sqlite3_step(stmt) == SQLITE_ROW) {
30743078
count = sqlite3_column_int(stmt, 0);
30753079
}
@@ -3389,7 +3393,10 @@ int cbm_store_count_edges(cbm_store_t *s, const char *project) {
33893393
}
33903394

33913395
bind_text(stmt, SKIP_ONE, project);
3392-
int count = 0;
3396+
/* See cbm_store_count_nodes: a non-row step is a failed read, not zero.
3397+
* index_status reads this alongside the node count and already treats a
3398+
* negative value as degraded. */
3399+
int count = CBM_STORE_ERR;
33933400
if (sqlite3_step(stmt) == SQLITE_ROW) {
33943401
count = sqlite3_column_int(stmt, 0);
33953402
}

‎tests/test_store_nodes.c‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2030,6 +2030,37 @@ TEST(store_count_nodes_unknown_project) {
20302030
PASS();
20312031
}
20322032

2033+
/* A COUNT(*) that cannot be read must not be reported as a count of zero:
2034+
* index_status renders that as the positive assertion status "empty", so a
2035+
* corrupt project looks like one that was never indexed. Dropping the table
2036+
* after the statement is cached makes the step fail deterministically. */
2037+
TEST(store_count_failed_read_is_not_zero) {
2038+
cbm_store_t *s = cbm_store_open_memory();
2039+
cbm_store_upsert_project(s, "test", "/tmp/test");
2040+
2041+
cbm_node_t n = {.project = "test",
2042+
.label = "File",
2043+
.name = "main.c",
2044+
.qualified_name = "test.main.c",
2045+
.file_path = "main.c"};
2046+
cbm_store_upsert_node(s, &n);
2047+
2048+
/* Sanity: a readable table still counts normally. */
2049+
ASSERT_EQ(cbm_store_count_nodes(s, "test"), 1);
2050+
ASSERT_TRUE(cbm_store_count_edges(s, "test") >= 0);
2051+
2052+
/* Make the read fail. The statements are cached by the calls above, so the
2053+
* step (not the prepare) is what fails once the tables are gone. */
2054+
ASSERT_EQ(cbm_store_exec(s, "DROP TABLE nodes;"), 0);
2055+
ASSERT_EQ(cbm_store_exec(s, "DROP TABLE edges;"), 0);
2056+
2057+
ASSERT_TRUE(cbm_store_count_nodes(s, "test") < 0);
2058+
ASSERT_TRUE(cbm_store_count_edges(s, "test") < 0);
2059+
2060+
cbm_store_close(s);
2061+
PASS();
2062+
}
2063+
20332064
/* ── Index coverage (#963) ──────────────────────────────────────── */
20342065

20352066
/* Round-trip + deleted-file prune + shadow miss-graph materialization +
@@ -2432,4 +2463,5 @@ SUITE(store_nodes) {
24322463
RUN_TEST(store_node_properties_special_chars);
24332464
RUN_TEST(store_delete_nodes_nonexistent);
24342465
RUN_TEST(store_count_nodes_unknown_project);
2466+
RUN_TEST(store_count_failed_read_is_not_zero);
24352467
}

0 commit comments

Comments
 (0)