From 1f935cbd85d79d865e387234cd2eb50d77adb363 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 11 Sep 2019 00:22:32 +0200 Subject: [PATCH] db: Strengthen some null-checks on queries sqlite3 will just report 0 for anything that it thinks should be numeric, or is accessed using a numeric accessor. Postgres does not, so we need to check for is_null before trying to read it. Signed-off-by: Christian Decker --- wallet/wallet.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/wallet/wallet.c b/wallet/wallet.c index abded008c..28459532c 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -2829,8 +2829,10 @@ void wallet_transaction_annotate(struct wallet *w, fatal("Attempting to annotate a transaction we don't have: %s", type_to_string(tmpctx, struct bitcoin_txid, txid)); - type |= db_column_int(stmt, 0); - if (channel_id == 0) + if (!db_column_is_null(stmt, 0)) + type |= db_column_int(stmt, 0); + + if (channel_id == 0 && !db_column_is_null(stmt, 1)) channel_id = db_column_u64(stmt, 1); tal_free(stmt); @@ -3085,7 +3087,7 @@ struct amount_msat wallet_total_forward_fees(struct wallet *w) bool res; stmt = db_prepare_v2(w->db, SQL("SELECT" - " SUM(in_msatoshi - out_msatoshi) " + " CAST(COALESCE(SUM(in_msatoshi - out_msatoshi), 0) AS BIGINT)" "FROM forwarded_payments " "WHERE state = ?;")); db_bind_int(stmt, 0, wallet_forward_status_in_db(FORWARD_SETTLED));