Skip to content

Commit 493c2ab

Browse files
rustyrussellcdecker
authored andcommitted
openingd: clean up and fix minor leak.
test_openchannel_hook_1: MEMLEAK: 0x557593c164e8' label=wire/fromwire.c:320:char[]' backtrace:' ccan/ccan/tal/tal.c:437 (tal_alloc_)' ccan/ccan/tal/tal.c:466 (tal_alloc_arr_)' wire/fromwire.c:320 (fromwire_wirestring)' openingd/gen_opening_wire.c:205 (fromwire_opening_got_offer_reply)' openingd/openingd.c:1067 (fundee_channel)' openingd/openingd.c:1279 (handle_peer_in)' openingd/openingd.c:1535 (main)' parents: fromwire_opening_got_offer_reply() allocates two fields off NULL: err_reason and our_upfront_shutdown_script. err_reason is used immediately afterwards (and was the leak detected here), so fixing that is easy. To fix the leak of our_upfront_shutdown_script, it makes sense to simply make it a member of 'state'. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 670f920 commit 493c2ab

1 file changed

Lines changed: 36 additions & 35 deletions

File tree

openingd/openingd.c

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,9 @@ struct state {
9797
u32 feerate_per_kw;
9898
struct bitcoin_txid funding_txid;
9999
u16 funding_txout;
100-
/* If set, this is the scriptpubkey they *must* close with */
101-
u8 *remote_upfront_shutdown_script;
100+
101+
/* If non-NULL, this is the scriptpubkey we/they *must* close with */
102+
u8 *upfront_shutdown_script[NUM_SIDES];
102103

103104
/* This is a cluster of fields in open_channel and accept_channel which
104105
* indicate the restrictions each side places on the channel. */
@@ -143,6 +144,12 @@ static void negotiation_aborted(struct state *state, bool am_funder,
143144
wire_sync_write(REQ_FD, take(msg));
144145
}
145146

147+
/* Default is no shutdown_scriptpubkey: free any leftover ones. */
148+
state->upfront_shutdown_script[LOCAL]
149+
= tal_free(state->upfront_shutdown_script[LOCAL]);
150+
state->upfront_shutdown_script[REMOTE]
151+
= tal_free(state->upfront_shutdown_script[REMOTE]);
152+
146153
/*~ Reset state. We keep gossipping with them, even though this open
147154
* failed. */
148155
memset(&state->channel_id, 0, sizeof(state->channel_id));
@@ -493,9 +500,7 @@ static bool setup_channel_funder(struct state *state)
493500

494501
/* We start the 'fund a channel' negotation with the supplied peer, but
495502
* stop when we get to the part where we need the funding txid */
496-
static u8 *funder_channel_start(struct state *state,
497-
u8 *our_upfront_shutdown_script,
498-
u8 channel_flags)
503+
static u8 *funder_channel_start(struct state *state, u8 channel_flags)
499504
{
500505
u8 *msg;
501506
u8 *funding_output_script;
@@ -514,8 +519,8 @@ static u8 *funder_channel_start(struct state *state,
514519
* - otherwise:
515520
* - MAY include a`shutdown_scriptpubkey`.
516521
*/
517-
if (!our_upfront_shutdown_script)
518-
our_upfront_shutdown_script = dev_upfront_shutdown_script(tmpctx);
522+
if (!state->upfront_shutdown_script[LOCAL])
523+
state->upfront_shutdown_script[LOCAL] = dev_upfront_shutdown_script(state);
519524

520525
msg = towire_open_channel_option_upfront_shutdown_script(NULL,
521526
&chainparams->genesis_blockhash,
@@ -536,7 +541,7 @@ static u8 *funder_channel_start(struct state *state,
536541
&state->our_points.htlc,
537542
&state->first_per_commitment_point[LOCAL],
538543
channel_flags,
539-
our_upfront_shutdown_script);
544+
state->upfront_shutdown_script[LOCAL]);
540545
sync_crypto_write(state->pps, take(msg));
541546

542547
/* This is usually a very transient state... */
@@ -548,10 +553,6 @@ static u8 *funder_channel_start(struct state *state,
548553
if (!msg)
549554
return NULL;
550555

551-
/* Default is no shutdown_scriptpubkey: free any leftover one. */
552-
state->remote_upfront_shutdown_script
553-
= tal_free(state->remote_upfront_shutdown_script);
554-
555556
/* BOLT #2:
556557
*
557558
* The receiving node MUST fail the channel if:
@@ -577,7 +578,7 @@ static u8 *funder_channel_start(struct state *state,
577578
&state->their_points.delayed_payment,
578579
&state->their_points.htlc,
579580
&state->first_per_commitment_point[REMOTE],
580-
&state->remote_upfront_shutdown_script))
581+
&state->upfront_shutdown_script[REMOTE]))
581582
peer_failed(state->pps,
582583
&state->channel_id,
583584
"Parsing accept_channel with option_upfront_shutdown_script %s", tal_hex(msg, msg));
@@ -866,7 +867,7 @@ static u8 *funder_channel_complete(struct state *state)
866867
state->funding_txout,
867868
state->feerate_per_kw,
868869
state->localconf.channel_reserve,
869-
state->remote_upfront_shutdown_script);
870+
state->upfront_shutdown_script[REMOTE]);
870871
}
871872

872873
/*~ The peer sent us an `open_channel`, that means we're the fundee. */
@@ -878,15 +879,11 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
878879
struct bitcoin_signature theirsig, sig;
879880
struct bitcoin_tx *local_commit, *remote_commit;
880881
struct bitcoin_blkid chain_hash;
881-
u8 *msg, *our_upfront_shutdown_script;
882+
u8 *msg;
882883
const u8 *wscript;
883884
u8 channel_flags;
884885
char* err_reason;
885886

886-
/* Default is no shutdown_scriptpubkey: free any leftover one. */
887-
state->remote_upfront_shutdown_script
888-
= tal_free(state->remote_upfront_shutdown_script);
889-
890887
/* BOLT #2:
891888
*
892889
* The receiving node MUST fail the channel if:
@@ -916,7 +913,7 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
916913
&theirs.htlc,
917914
&state->first_per_commitment_point[REMOTE],
918915
&channel_flags,
919-
&state->remote_upfront_shutdown_script))
916+
&state->upfront_shutdown_script[REMOTE]))
920917
peer_failed(state->pps,
921918
&state->channel_id,
922919
"Parsing open_channel with option_upfront_shutdown_script %s", tal_hex(tmpctx, open_channel_msg));
@@ -1060,24 +1057,27 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
10601057
state->remoteconf.to_self_delay,
10611058
state->remoteconf.max_accepted_htlcs,
10621059
channel_flags,
1063-
state->remote_upfront_shutdown_script);
1060+
state->upfront_shutdown_script[REMOTE]);
10641061
wire_sync_write(REQ_FD, take(msg));
10651062
msg = wire_sync_read(tmpctx, REQ_FD);
10661063

1067-
if (!fromwire_opening_got_offer_reply(NULL, msg, &err_reason,
1068-
&our_upfront_shutdown_script))
1064+
/* We don't allocate off tmpctx, because that's freed inside
1065+
* opening_negotiate_msg */
1066+
if (!fromwire_opening_got_offer_reply(state, msg, &err_reason,
1067+
&state->upfront_shutdown_script[LOCAL]))
10691068
master_badmsg(WIRE_OPENING_GOT_OFFER_REPLY, msg);
10701069

10711070
/* If they give us a reason to reject, do so. */
10721071
if (err_reason) {
10731072
u8 *errmsg = towire_errorfmt(NULL, &state->channel_id,
10741073
"%s", err_reason);
10751074
sync_crypto_write(state->pps, take(errmsg));
1075+
tal_free(err_reason);
10761076
return NULL;
10771077
}
10781078

1079-
if (!our_upfront_shutdown_script)
1080-
our_upfront_shutdown_script = dev_upfront_shutdown_script(state);
1079+
if (!state->upfront_shutdown_script[LOCAL])
1080+
state->upfront_shutdown_script[LOCAL] = dev_upfront_shutdown_script(state);
10811081

10821082
/* OK, we accept! */
10831083
msg = towire_accept_channel_option_upfront_shutdown_script(NULL, &state->channel_id,
@@ -1094,7 +1094,7 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
10941094
&state->our_points.delayed_payment,
10951095
&state->our_points.htlc,
10961096
&state->first_per_commitment_point[LOCAL],
1097-
our_upfront_shutdown_script);
1097+
state->upfront_shutdown_script[LOCAL]);
10981098

10991099
sync_crypto_write(state->pps, take(msg));
11001100

@@ -1262,8 +1262,8 @@ static u8 *fundee_channel(struct state *state, const u8 *open_channel_msg)
12621262
state->feerate_per_kw,
12631263
msg,
12641264
state->localconf.channel_reserve,
1265-
our_upfront_shutdown_script,
1266-
state->remote_upfront_shutdown_script);
1265+
state->upfront_shutdown_script[LOCAL],
1266+
state->upfront_shutdown_script[REMOTE]);
12671267
}
12681268

12691269
/*~ Standard "peer sent a message, handle it" demuxer. Though it really only
@@ -1359,20 +1359,19 @@ static u8 *handle_master_in(struct state *state)
13591359
{
13601360
u8 *msg = wire_sync_read(tmpctx, REQ_FD);
13611361
enum opening_wire_type t = fromwire_peektype(msg);
1362-
u8 channel_flags, *upfront_shutdown_script;
1362+
u8 channel_flags;
13631363
struct bitcoin_txid funding_txid;
13641364
u16 funding_txout;
13651365

13661366
switch (t) {
13671367
case WIRE_OPENING_FUNDER_START:
1368-
if (!fromwire_opening_funder_start(tmpctx, msg, &state->funding,
1368+
if (!fromwire_opening_funder_start(state, msg, &state->funding,
13691369
&state->push_msat,
1370-
&upfront_shutdown_script,
1370+
&state->upfront_shutdown_script[LOCAL],
13711371
&state->feerate_per_kw,
13721372
&channel_flags))
13731373
master_badmsg(WIRE_OPENING_FUNDER_START, msg);
1374-
msg = funder_channel_start(state, upfront_shutdown_script,
1375-
channel_flags);
1374+
msg = funder_channel_start(state, channel_flags);
13761375

13771376
/* We want to keep openingd alive, since we're not done yet */
13781377
if (msg)
@@ -1478,8 +1477,10 @@ int main(int argc, char *argv[])
14781477
memset(&state->channel_id, 0, sizeof(state->channel_id));
14791478
state->channel = NULL;
14801479

1481-
/*~ We set this to NULL, meaning no requirements on shutdown */
1482-
state->remote_upfront_shutdown_script = NULL;
1480+
/*~ We set these to NULL, meaning no requirements on shutdown */
1481+
state->upfront_shutdown_script[LOCAL]
1482+
= state->upfront_shutdown_script[REMOTE]
1483+
= NULL;
14831484

14841485
/*~ We need an initial per-commitment point whether we're funding or
14851486
* they are, and lightningd has reserved a unique dbid for us already,

0 commit comments

Comments
 (0)