From b267b24c082d1995a15a260c171eef855cff8d7f Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 30 Oct 2017 16:05:00 +0100 Subject: [PATCH] db: db_exec_prepared takes ownership of the statement Technically it's the caller that'll own the statement, but it is nice to have db_exec_prepared dispose of it. Signed-off-by: Christian Decker --- wallet/db.c | 16 +++++++++++----- wallet/db.h | 3 ++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/wallet/db.c b/wallet/db.c index f7c5f5ffa..b7c56ba05 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -150,17 +150,23 @@ sqlite3_stmt *db_prepare_(const char *caller, struct db *db, const char *query) bool db_exec_prepared_(const char *caller, struct db *db, sqlite3_stmt *stmt) { - if (db->in_transaction && db->err) - return false; + if (db->in_transaction && db->err) { + goto fail; + } + db_clear_error(db); if (sqlite3_step(stmt) != SQLITE_DONE) { db->err = tal_fmt(db, "%s: %s", caller, sqlite3_errmsg(db->sql)); - return false; - } else { - return true; + goto fail; } + + sqlite3_finalize(stmt); + return true; +fail: + sqlite3_finalize(stmt); + return false; } bool PRINTF_FMT(3, 4) diff --git a/wallet/db.h b/wallet/db.h index 4217f87bf..86015d117 100644 --- a/wallet/db.h +++ b/wallet/db.h @@ -102,7 +102,8 @@ sqlite3_stmt *db_prepare_(const char *caller, struct db *db, const char *query); * all non-null variables using the `sqlite3_bind_*` functions, it can * be executed with this function. It is a small, transaction-aware, * wrapper around `sqlite3_step`, that also sets `db->err` if the - * execution fails. + * execution fails. This will take ownership of `stmt` and will free + * it before returning. * * @db: The database to execute on * @stmt: The prepared statement to execute