Reject interrupted membership channel queries

This commit is contained in:
evgeny committed 2026-10-24 14:10:28 +02:00
1 parent 81e2a9aa4a
commit 19c3cc0434
2 files changed
+34 -11

No files matched your search

+27 -8
View File
@@ -206,24 +206,35 @@ int topo_node_sqlite_member_exists(sqlite3* db, uint64_t node_id) {
int topo_node_sqlite_get_member_channels(sqlite3* db, uint64_t node_id,
uint64_t** out_ch_ids, int* out_count) {
if (!db || !out_ch_ids || !out_count) return -1;
*out_ch_ids = NULL; *out_count = 0;
if (out_ch_ids) *out_ch_ids = NULL;
if (out_count) *out_count = 0;
if (!db || !out_ch_ids || !out_count) { DEBUG_ERROR(DEBUG_CATEGORY_BGP, "member channels: invalid arguments"); return -1; }
sqlite3_stmt* s = NULL;
if (sqlite3_prepare_v2(db,
"SELECT name FROM sqlite_master WHERE type='table' AND name LIKE 'peers\\_%' ESCAPE '\\'",
-1, &s, NULL) != SQLITE_OK) return -1;
-1, &s, NULL) != SQLITE_OK) {
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "member channels: enumerate tables: %s", sqlite3_errmsg(db)); return -1;
}
uint64_t* ids = NULL; int count = 0, cap = 0;
char sql[256];
while (sqlite3_step(s) == SQLITE_ROW) {
int rc;
while ((rc = sqlite3_step(s)) == SQLITE_ROW) {
const char* tbl = (const char*)sqlite3_column_text(s, 0);
if (!tbl || strncmp(tbl, "peers_", 6) != 0) continue;
snprintf(sql, sizeof(sql), "SELECT 1 FROM \"%s\" WHERE node_id=?", tbl);
sqlite3_stmt* m = NULL;
if (sqlite3_prepare_v2(db, sql, -1, &m, NULL) == SQLITE_OK) {
sqlite3_bind_int64(m, 1, (sqlite3_int64)node_id);
int is_member = (sqlite3_step(m) == SQLITE_ROW);
int member_rc = sqlite3_prepare_v2(db, sql, -1, &m, NULL);
if (member_rc == SQLITE_OK) member_rc = sqlite3_bind_int64(m, 1, (sqlite3_int64)node_id);
if (member_rc == SQLITE_OK) member_rc = sqlite3_step(m);
if (member_rc != SQLITE_ROW && member_rc != SQLITE_DONE) {
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "member channels: table=%s node=%016llx rc=%d: %s",
tbl, (unsigned long long)node_id, member_rc, sqlite3_errmsg(db));
sqlite3_finalize(m); u_free(ids); sqlite3_finalize(s); return -1;
}
{
int is_member = member_rc == SQLITE_ROW;
sqlite3_finalize(m);
if (is_member) {
uint64_t ch = strtoull(tbl + 6, NULL, 10);
@@ -231,7 +242,10 @@ int topo_node_sqlite_get_member_channels(sqlite3* db, uint64_t node_id,
if (count == cap) {
cap = cap ? cap * 2 : 8;
uint64_t* tmp = u_realloc(ids, (size_t)cap * sizeof(uint64_t));
if (!tmp) { u_free(ids); sqlite3_finalize(s); return -1; }
if (!tmp) {
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "member channels: allocation failed count=%d", count);
u_free(ids); sqlite3_finalize(s); return -1;
}
ids = tmp;
}
ids[count++] = ch;
@@ -239,6 +253,11 @@ int topo_node_sqlite_get_member_channels(sqlite3* db, uint64_t node_id,
}
}
}
if (rc != SQLITE_DONE) {
DEBUG_ERROR(DEBUG_CATEGORY_BGP, "member channels: enumeration failed node=%016llx rc=%d: %s",
(unsigned long long)node_id, rc, sqlite3_errmsg(db));
u_free(ids); sqlite3_finalize(s); return -1;
}
sqlite3_finalize(s);
*out_ch_ids = ids; *out_count = count;
return 0;