From d7aa2749c3f2d596a33f8e86de8d276410b4aa9b Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 9 Sep 2022 17:02:15 +0930 Subject: [PATCH] db: fix migrations which write to db. valgrind noticed that this was uninitialized when I tried a complex migration. Signed-off-by: Rusty Russell --- db/exec.c | 7 +++++-- wallet/db.c | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/db/exec.c b/db/exec.c index 13341c1e7..96ef38556 100644 --- a/db/exec.c +++ b/db/exec.c @@ -45,8 +45,11 @@ u32 db_data_version_get(struct db *db) u32 version; stmt = db_prepare_v2(db, SQL("SELECT intval FROM vars WHERE name = 'data_version'")); db_query_prepared(stmt); - db_step(stmt); - version = db_col_int(stmt, "intval"); + /* This fails on uninitialized db, so "0" */ + if (db_step(stmt)) + version = db_col_int(stmt, "intval"); + else + version = 0; tal_free(stmt); return version; } diff --git a/wallet/db.c b/wallet/db.c index 41010d84d..a0fd0e8da 100644 --- a/wallet/db.c +++ b/wallet/db.c @@ -950,10 +950,10 @@ struct db *db_setup(const tal_t *ctx, struct lightningd *ld, db->report_changes_fn = plugin_hook_db_sync; db_begin_transaction(db); + db->data_version = db_data_version_get(db); migrated = db_migrate(ld, db, bip32_base); - db->data_version = db_data_version_get(db); db_commit_transaction(db); /* This needs to be done outside a transaction, apparently.