From ae17c64c4afd753666c56f2f26cba40ba9ff9db0 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 27 Apr 2018 11:09:56 +0930 Subject: [PATCH] db: full location tags for callers, make it implicit. For better leak tracking. Signed-off-by: Rusty Russell --- wallet/db.c | 15 +++++++-------- wallet/db.h | 9 ++++++--- wallet/wallet.c | 17 ++++++++--------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/wallet/db.c b/wallet/db.c index b25b087b6..1106dfe3d 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -396,7 +396,7 @@ void db_stmt_done(sqlite3_stmt *stmt) sqlite3_finalize(stmt); } -sqlite3_stmt *db_prepare_(const char *caller, struct db *db, const char *query) +sqlite3_stmt *db_prepare_(const char *location, struct db *db, const char *query) { int err; sqlite3_stmt *stmt; @@ -406,9 +406,9 @@ sqlite3_stmt *db_prepare_(const char *caller, struct db *db, const char *query) err = sqlite3_prepare_v2(db->sql, query, -1, &stmt, NULL); if (err != SQLITE_OK) - fatal("%s: %s: %s", caller, query, sqlite3_errmsg(db->sql)); + fatal("%s: %s: %s", location, query, sqlite3_errmsg(db->sql)); - dev_statement_start(stmt, caller); + dev_statement_start(stmt, location); return stmt; } @@ -468,7 +468,7 @@ fail: } sqlite3_stmt *PRINTF_FMT(3, 4) - db_query(const char *caller, struct db *db, const char *fmt, ...) + db_query_(const char *location, struct db *db, const char *fmt, ...) { va_list ap; char *query; @@ -484,7 +484,7 @@ sqlite3_stmt *PRINTF_FMT(3, 4) sqlite3_prepare_v2(db->sql, query, -1, &stmt, NULL); tal_free(query); if (stmt) - dev_statement_start(stmt, caller); + dev_statement_start(stmt, location); return stmt; } @@ -554,8 +554,7 @@ static int db_get_version(struct db *db) { int err; u64 res = -1; - sqlite3_stmt *stmt = - db_query(__func__, db, "SELECT version FROM version LIMIT 1"); + sqlite3_stmt *stmt = db_query(db, "SELECT version FROM version LIMIT 1"); if (!stmt) return -1; @@ -658,7 +657,7 @@ s64 db_get_intvar(struct db *db, char *varname, s64 defval) int err; s64 res = defval; sqlite3_stmt *stmt = - db_query(__func__, db, + db_query(db, "SELECT val FROM vars WHERE name='%s' LIMIT 1", varname); if (!stmt) diff --git a/wallet/db.h b/wallet/db.h index 5c7efdccd..71faac6ff 100644 --- a/wallet/db.h +++ b/wallet/db.h @@ -37,7 +37,9 @@ struct db *db_setup(const tal_t *ctx, struct log *log); * db_query - Prepare and execute a query, and return the result (or NULL) */ sqlite3_stmt *PRINTF_FMT(3, 4) - db_query(const char *caller, struct db *db, const char *fmt, ...); + db_query_(const char *location, struct db *db, const char *fmt, ...); +#define db_query(db, ...) \ + db_query_(__FILE__ ":" stringify(__LINE__), db, __VA_ARGS__) /** * db_begin_transaction - Begin a transaction @@ -84,8 +86,9 @@ s64 db_get_intvar(struct db *db, char *varname, s64 defval); * @db: Database to query/exec * @query: The SQL statement to compile */ -#define db_prepare(db,query) db_prepare_(__func__,db,query) -sqlite3_stmt *db_prepare_(const char *caller, struct db *db, const char *query); +#define db_prepare(db,query) \ + db_prepare_(__FILE__ ":" stringify(__LINE__), db, query) +sqlite3_stmt *db_prepare_(const char *location, struct db *db, const char *query); /** * db_exec_prepared -- Execute a prepared statement diff --git a/wallet/wallet.c b/wallet/wallet.c index fcb303c0c..c22c90277 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -496,7 +496,7 @@ static struct peer *wallet_peer_load(struct wallet *w, const u64 dbid) struct wireaddr *addrp, addr; sqlite3_stmt *stmt = - db_query(__func__, w->db, + db_query(w->db, "SELECT id, node_id, address FROM peers WHERE id=%"PRIu64";", dbid); if (!stmt || sqlite3_step(stmt) != SQLITE_ROW) { @@ -685,9 +685,8 @@ bool wallet_channels_load_active(const tal_t *ctx, struct wallet *w) sqlite3_stmt *stmt; /* We load all channels */ - stmt = db_query( - __func__, w->db, "SELECT %s FROM channels;", - channel_fields); + stmt = db_query(w->db, "SELECT %s FROM channels;", + channel_fields); w->max_channel_dbid = 0; @@ -828,7 +827,7 @@ bool wallet_channel_config_load(struct wallet *w, const u64 id, "SELECT id, dust_limit_satoshis, max_htlc_value_in_flight_msat, " "channel_reserve_satoshis, htlc_minimum_msat, to_self_delay, " "max_accepted_htlcs FROM channel_configs WHERE id=%" PRIu64 ";"; - sqlite3_stmt *stmt = db_query(__func__, w->db, query, id); + sqlite3_stmt *stmt = db_query(w->db, query, id); if (!stmt || sqlite3_step(stmt) != SQLITE_ROW) { db_stmt_done(stmt); return false; @@ -1008,7 +1007,7 @@ void wallet_peer_delete(struct wallet *w, u64 peer_dbid) sqlite3_stmt *stmt; /* Must not have any channels still using this peer */ - stmt = db_query(__func__, w->db, + stmt = db_query(w->db, "SELECT * FROM channels WHERE peer_id = %"PRIu64, peer_dbid); assert(sqlite3_step(stmt) == SQLITE_DONE); @@ -1280,7 +1279,7 @@ bool wallet_htlcs_load_for_channel(struct wallet *wallet, log_debug(wallet->log, "Loading HTLCs for channel %"PRIu64, chan->dbid); sqlite3_stmt *stmt = db_query( - __func__, wallet->db, + wallet->db, "SELECT id, channel_htlc_id, msatoshi, cltv_expiry, hstate, " "payment_hash, shared_secret, payment_key, routing_onion FROM channel_htlcs WHERE " "direction=%d AND channel_id=%" PRIu64 " AND hstate != %d", @@ -1301,7 +1300,7 @@ bool wallet_htlcs_load_for_channel(struct wallet *wallet, db_stmt_done(stmt); stmt = db_query( - __func__, wallet->db, + wallet->db, "SELECT id, channel_htlc_id, msatoshi, cltv_expiry, hstate, " "payment_hash, origin_htlc, payment_key, routing_onion FROM channel_htlcs WHERE " "direction=%d AND channel_id=%" PRIu64 " AND hstate != %d", @@ -1907,7 +1906,7 @@ void wallet_htlc_sigs_save(struct wallet *w, u64 channel_id, bool wallet_network_check(struct wallet *w, const struct chainparams *chainparams) { - sqlite3_stmt *stmt = db_query(__func__, w->db, + sqlite3_stmt *stmt = db_query(w->db, "SELECT val FROM vars WHERE name='genesis_hash'"); struct bitcoin_blkid chainhash;