Skip to content

Commit b7b3cbc

Browse files
committed
db: enforce that bindings be done in order.
This is almost always true already; fix up the few non-standard ones. This is enforced with an assert, and I ran the entire test suite to double-check. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent d17506b commit b7b3cbc

File tree

5 files changed

+35
-25
lines changed

5 files changed

+35
-25
lines changed

db/bindings.c

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616

1717
#define NSEC_IN_SEC 1000000000
1818

19+
static int check_bind_pos(struct db_stmt *stmt, int pos)
20+
{
21+
assert(pos == ++stmt->bind_pos);
22+
return pos;
23+
}
24+
1925
/* Local helpers once you have column number */
2026
static bool db_column_is_null(struct db_stmt *stmt, int col)
2127
{
@@ -37,7 +43,7 @@ static bool db_column_null_warn(struct db_stmt *stmt, const char *colname,
3743

3844
void db_bind_int(struct db_stmt *stmt, int pos, int val)
3945
{
40-
assert(pos < tal_count(stmt->bindings));
46+
pos = check_bind_pos(stmt, pos);
4147
memcheck(&val, sizeof(val));
4248
stmt->bindings[pos].type = DB_BINDING_INT;
4349
stmt->bindings[pos].v.i = val;
@@ -60,29 +66,29 @@ int db_col_is_null(struct db_stmt *stmt, const char *colname)
6066

6167
void db_bind_null(struct db_stmt *stmt, int pos)
6268
{
63-
assert(pos < tal_count(stmt->bindings));
69+
pos = check_bind_pos(stmt, pos);
6470
stmt->bindings[pos].type = DB_BINDING_NULL;
6571
}
6672

6773
void db_bind_u64(struct db_stmt *stmt, int pos, u64 val)
6874
{
6975
memcheck(&val, sizeof(val));
70-
assert(pos < tal_count(stmt->bindings));
76+
pos = check_bind_pos(stmt, pos);
7177
stmt->bindings[pos].type = DB_BINDING_UINT64;
7278
stmt->bindings[pos].v.u64 = val;
7379
}
7480

7581
void db_bind_blob(struct db_stmt *stmt, int pos, const u8 *val, size_t len)
7682
{
77-
assert(pos < tal_count(stmt->bindings));
83+
pos = check_bind_pos(stmt, pos);
7884
stmt->bindings[pos].type = DB_BINDING_BLOB;
7985
stmt->bindings[pos].v.blob = memcheck(val, len);
8086
stmt->bindings[pos].len = len;
8187
}
8288

8389
void db_bind_text(struct db_stmt *stmt, int pos, const char *val)
8490
{
85-
assert(pos < tal_count(stmt->bindings));
91+
pos = check_bind_pos(stmt, pos);
8692
stmt->bindings[pos].type = DB_BINDING_TEXT;
8793
stmt->bindings[pos].v.text = val;
8894
stmt->bindings[pos].len = strlen(val);

db/common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ struct db_stmt {
104104
/* Our entry in the list of pending statements. */
105105
struct list_node list;
106106

107+
/* Bind counter */
108+
int bind_pos;
109+
107110
/* Database we are querying */
108111
struct db *db;
109112

db/utils.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ static struct db_stmt *db_prepare_core(struct db *db,
8181
stmt->query = db_query;
8282
stmt->executed = false;
8383
stmt->inner_stmt = NULL;
84+
stmt->bind_pos = -1;
8485

8586
tal_add_destructor(stmt, db_stmt_free);
8687

plugins/bkpr/recorder.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -731,11 +731,11 @@ static struct chain_event *find_chain_event(const tal_t *ctx,
731731
" LEFT OUTER JOIN accounts a"
732732
" ON e.account_id = a.id"
733733
" WHERE "
734-
" e.account_id = ?"
734+
" e.spending_txid = ?"
735+
" AND e.account_id = ?"
735736
" AND e.utxo_txid = ?"
736-
" AND e.outnum = ?"
737-
" AND e.spending_txid = ?"));
738-
db_bind_txid(stmt, 3, spending_txid);
737+
" AND e.outnum = ?"));
738+
db_bind_txid(stmt, 0, spending_txid);
739739
} else {
740740
stmt = db_prepare_v2(db, SQL("SELECT"
741741
" e.id"
@@ -760,18 +760,17 @@ static struct chain_event *find_chain_event(const tal_t *ctx,
760760
" LEFT OUTER JOIN accounts a"
761761
" ON e.account_id = a.id"
762762
" WHERE "
763-
" e.account_id = ?"
763+
" e.tag = ?"
764+
" AND e.account_id = ?"
764765
" AND e.utxo_txid = ?"
765766
" AND e.outnum = ?"
766-
" AND e.spending_txid IS NULL"
767-
" AND e.tag = ?"));
768-
769-
db_bind_text(stmt, 3, tag);
767+
" AND e.spending_txid IS NULL"));
768+
db_bind_text(stmt, 0, tag);
770769
}
771770

772-
db_bind_u64(stmt, 0, acct->db_id);
773-
db_bind_txid(stmt, 1, &outpoint->txid);
774-
db_bind_int(stmt, 2, outpoint->n);
771+
db_bind_u64(stmt, 1, acct->db_id);
772+
db_bind_txid(stmt, 2, &outpoint->txid);
773+
db_bind_int(stmt, 3, outpoint->n);
775774

776775
db_query_prepared(stmt);
777776
if (db_step(stmt))

wallet/wallet.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2736,7 +2736,6 @@ void wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid,
27362736
" WHERE id=?"));
27372737

27382738
db_bind_int(stmt, 0, htlc_state_in_db(new_state));
2739-
db_bind_u64(stmt, 7, htlc_dbid);
27402739

27412740
if (payment_key)
27422741
db_bind_preimage(stmt, 1, payment_key);
@@ -2763,6 +2762,7 @@ void wallet_htlc_update(struct wallet *wallet, const u64 htlc_dbid,
27632762
else
27642763
db_bind_null(stmt, 6);
27652764

2765+
db_bind_u64(stmt, 7, htlc_dbid);
27662766
db_exec_prepared_v2(take(stmt));
27672767

27682768
if (terminal) {
@@ -3278,6 +3278,7 @@ void wallet_payment_delete(struct wallet *wallet,
32783278
" AND groupid = ?"
32793279
" AND partid = ?"
32803280
" AND status = ?"));
3281+
db_bind_sha256(stmt, 0, payment_hash);
32813282
db_bind_u64(stmt, 1, *groupid);
32823283
db_bind_u64(stmt, 2, *partid);
32833284
db_bind_u64(stmt, 3, *status);
@@ -3287,9 +3288,9 @@ void wallet_payment_delete(struct wallet *wallet,
32873288
SQL("DELETE FROM payments"
32883289
" WHERE payment_hash = ?"
32893290
" AND status = ?"));
3291+
db_bind_sha256(stmt, 0, payment_hash);
32903292
db_bind_u64(stmt, 1, *status);
32913293
}
3292-
db_bind_sha256(stmt, 0, payment_hash);
32933294
db_exec_prepared_v2(take(stmt));
32943295
}
32953296

@@ -3572,9 +3573,9 @@ void wallet_payment_set_failinfo(struct wallet *wallet,
35723573
" , failcode=?"
35733574
" , failnode=?"
35743575
" , failscid=?"
3576+
" , faildirection=?"
35753577
" , failupdate=?"
35763578
" , faildetail=?"
3577-
" , faildirection=?"
35783579
" WHERE payment_hash=?"
35793580
" AND partid=?;"));
35803581
if (failonionreply)
@@ -3592,18 +3593,18 @@ void wallet_payment_set_failinfo(struct wallet *wallet,
35923593

35933594
if (failchannel) {
35943595
db_bind_short_channel_id(stmt, 5, failchannel);
3595-
db_bind_int(stmt, 8, faildirection);
3596+
db_bind_int(stmt, 6, faildirection);
35963597
} else {
35973598
db_bind_null(stmt, 5);
3598-
db_bind_null(stmt, 8);
3599+
db_bind_null(stmt, 6);
35993600
}
36003601

3601-
db_bind_talarr(stmt, 6, failupdate);
3602+
db_bind_talarr(stmt, 7, failupdate);
36023603

36033604
if (faildetail != NULL)
3604-
db_bind_text(stmt, 7, faildetail);
3605+
db_bind_text(stmt, 8, faildetail);
36053606
else
3606-
db_bind_null(stmt, 7);
3607+
db_bind_null(stmt, 8);
36073608

36083609
db_bind_sha256(stmt, 9, payment_hash);
36093610
db_bind_u64(stmt, 10, partid);

0 commit comments

Comments
 (0)