From ed8baf5f8861233c16f21de832fd3af5ab5bf80a Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Fri, 15 Dec 2017 20:59:32 +1030 Subject: [PATCH] funding_spent: fix leaks. Signed-off-by: Rusty Russell --- lightningd/peer_control.c | 11 +++++++++-- wallet/wallet.c | 2 +- wallet/wallet.h | 2 +- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lightningd/peer_control.c b/lightningd/peer_control.c index 07342cb82..7af4550e7 100644 --- a/lightningd/peer_control.c +++ b/lightningd/peer_control.c @@ -1374,6 +1374,7 @@ static enum watch_result funding_spent(struct peer *peer, s64 keyindex; struct pubkey ourkey; struct htlc_stub *stubs; + const tal_t *tmpctx = tal_tmpctx(peer); peer_fail_permanent_str(peer, "Funding transaction spent"); @@ -1389,12 +1390,14 @@ static enum watch_result funding_spent(struct peer *peer, if (!peer->owner) { log_broken(peer->log, "Could not subdaemon onchain: %s", strerror(errno)); + tal_free(tmpctx); return KEEP_WATCHING; } - stubs = wallet_htlc_stubs(peer, peer->ld->wallet, peer->channel); + stubs = wallet_htlc_stubs(tmpctx, peer->ld->wallet, peer->channel); if (!stubs) { log_broken(peer->log, "Could not load htlc_stubs"); + tal_free(tmpctx); return KEEP_WATCHING; } @@ -1406,14 +1409,16 @@ static enum watch_result funding_spent(struct peer *peer, keyindex = wallet_get_newindex(peer->ld); if (keyindex < 0) { log_broken(peer->log, "Could not get keyindex"); + tal_free(tmpctx); return KEEP_WATCHING; } } - scriptpubkey = p2wpkh_for_keyidx(peer, peer->ld, keyindex); + scriptpubkey = p2wpkh_for_keyidx(tmpctx, peer->ld, keyindex); if (!scriptpubkey) { peer_internal_error(peer, "Can't get shutdown script %"PRIu64, keyindex); + tal_free(tmpctx); return DELETE_WATCH; } @@ -1421,6 +1426,7 @@ static enum watch_result funding_spent(struct peer *peer, peer_internal_error(peer, "Can't get shutdown key %"PRIu64, keyindex); + tal_free(tmpctx); return DELETE_WATCH; } @@ -1470,6 +1476,7 @@ static enum watch_result funding_spent(struct peer *peer, watch_tx_and_outputs(peer, tx); + tal_free(tmpctx); /* We keep watching until peer finally deleted, for reorgs. */ return KEEP_WATCHING; } diff --git a/wallet/wallet.c b/wallet/wallet.c index 8ec34580c..f47bb0274 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -1160,7 +1160,7 @@ bool wallet_invoice_remove(struct wallet *wallet, struct invoice *inv) return sqlite3_changes(wallet->db->sql) == 1; } -struct htlc_stub *wallet_htlc_stubs(tal_t *ctx, struct wallet *wallet, +struct htlc_stub *wallet_htlc_stubs(const tal_t *ctx, struct wallet *wallet, struct wallet_channel *chan) { struct htlc_stub *stubs; diff --git a/wallet/wallet.h b/wallet/wallet.h index 236ca9707..c2f35ef97 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -374,7 +374,7 @@ bool wallet_invoice_remove(struct wallet *wallet, struct invoice *inv); * @wallet: Wallet to load from * @chan: Channel to fetch stubs for */ -struct htlc_stub *wallet_htlc_stubs(tal_t *ctx, struct wallet *wallet, +struct htlc_stub *wallet_htlc_stubs(const tal_t *ctx, struct wallet *wallet, struct wallet_channel *chan); /**