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