diff --git a/channeld/full_channel.c b/channeld/full_channel.c index 6e1b39c87..70ddc15df 100644 --- a/channeld/full_channel.c +++ b/channeld/full_channel.c @@ -546,13 +546,7 @@ static enum channel_add_err add_htlc(struct channel *channel, htlc->fail_immediate = false; htlc->rhash = *payment_hash; - if (blinding) - htlc->blinding = tal_dup(htlc, struct pubkey, blinding); - else { - /* Can be taken, even if NULL. */ - taken(blinding); - htlc->blinding = NULL; - } + htlc->blinding = tal_dup_or_null(htlc, struct pubkey, blinding); htlc->failed = NULL; htlc->r = NULL; htlc->routing = tal_dup_arr(htlc, u8, routing, TOTAL_PACKET_SIZE(ROUTING_INFO_SIZE), 0); diff --git a/common/blockheight_states.c b/common/blockheight_states.c index b8f1843d7..d60602334 100644 --- a/common/blockheight_states.c +++ b/common/blockheight_states.c @@ -101,11 +101,9 @@ struct height_states *dup_height_states(const tal_t *ctx, tal_steal(ctx, states)); n = tal_dup(ctx, struct height_states, states); - for (size_t i = 0; i < ARRAY_SIZE(n->height); i++) { - if (n->height[i]) - n->height[i] = tal_dup(n, u32, n->height[i]); + for (size_t i = 0; i < ARRAY_SIZE(n->height); i++) + n->height[i] = tal_dup_or_null(n, u32, n->height[i]); - } return n; } diff --git a/common/coin_mvt.c b/common/coin_mvt.c index 9133c166b..6f981e7fe 100644 --- a/common/coin_mvt.c +++ b/common/coin_mvt.c @@ -105,10 +105,7 @@ static struct chain_coin_mvt *new_chain_coin_mvt(const tal_t *ctx, /* for htlc's that are filled onchain, we also have a * preimage, NULL otherwise */ - if (payment_hash) - mvt->payment_hash = tal_dup(mvt, struct sha256, payment_hash); - else - mvt->payment_hash = NULL; + mvt->payment_hash = tal_dup_or_null(mvt, struct sha256, payment_hash); mvt->blockheight = blockheight; mvt->tags = tal_steal(mvt, tags); diff --git a/common/fee_states.c b/common/fee_states.c index 4176972f8..adf6845d4 100644 --- a/common/fee_states.c +++ b/common/fee_states.c @@ -48,10 +48,9 @@ struct fee_states *dup_fee_states(const tal_t *ctx, return cast_const(struct fee_states *, tal_steal(ctx, fee_states)); n = tal_dup(ctx, struct fee_states, fee_states); - for (size_t i = 0; i < ARRAY_SIZE(n->feerate); i++) { - if (n->feerate[i]) - n->feerate[i] = tal_dup(n, u32, n->feerate[i]); - } + for (size_t i = 0; i < ARRAY_SIZE(n->feerate); i++) + n->feerate[i] = tal_dup_or_null(n, u32, n->feerate[i]); + return n; } diff --git a/common/htlc_wire.c b/common/htlc_wire.c index d0a024fe4..4385658b3 100644 --- a/common/htlc_wire.c +++ b/common/htlc_wire.c @@ -14,10 +14,8 @@ static struct failed_htlc *failed_htlc_dup(const tal_t *ctx, return cast_const(struct failed_htlc *, tal_steal(ctx, f)); newf = tal(ctx, struct failed_htlc); newf->id = f->id; - if (f->sha256_of_onion) - newf->sha256_of_onion = tal_dup(newf, struct sha256, f->sha256_of_onion); - else - newf->sha256_of_onion = NULL; + newf->sha256_of_onion = tal_dup_or_null(newf, struct sha256, + f->sha256_of_onion); newf->badonion = f->badonion; if (f->onion) newf->onion = dup_onionreply(newf, f->onion); @@ -46,15 +44,9 @@ struct existing_htlc *new_existing_htlc(const tal_t *ctx, existing->payment_hash = *payment_hash; memcpy(existing->onion_routing_packet, onion_routing_packet, sizeof(existing->onion_routing_packet)); - if (blinding) - existing->blinding = tal_dup(existing, struct pubkey, blinding); - else - existing->blinding = NULL; - if (preimage) - existing->payment_preimage - = tal_dup(existing, struct preimage, preimage); - else - existing->payment_preimage = NULL; + existing->blinding = tal_dup_or_null(existing, struct pubkey, blinding); + existing->payment_preimage + = tal_dup_or_null(existing, struct preimage, preimage); if (failed) existing->failed = failed_htlc_dup(existing, failed); else diff --git a/common/onion.c b/common/onion.c index 69e0e32db..f8b09511e 100644 --- a/common/onion.c +++ b/common/onion.c @@ -355,10 +355,7 @@ struct onion_payload *onion_decode(const tal_t *ctx, } p->payment_secret = NULL; - if (blinding) - p->blinding = tal_dup(p, struct pubkey, blinding); - else - p->blinding = NULL; + p->blinding = tal_dup_or_null(p, struct pubkey, blinding); #if EXPERIMENTAL_FEATURES if (!p->blinding) { diff --git a/lightningd/channel.c b/lightningd/channel.c index f8e7972dd..eeb8aba9c 100644 --- a/lightningd/channel.c +++ b/lightningd/channel.c @@ -183,12 +183,9 @@ new_inflight(struct channel *channel, /* Channel lease infos */ inflight->lease_blockheight_start = lease_blockheight_start; inflight->lease_expiry = lease_expiry; - if (lease_commit_sig) - inflight->lease_commit_sig - = tal_dup(inflight, secp256k1_ecdsa_signature, + inflight->lease_commit_sig + = tal_dup_or_null(inflight, secp256k1_ecdsa_signature, lease_commit_sig); - else - inflight->lease_commit_sig = NULL; inflight->lease_chan_max_msat = lease_chan_max_msat; inflight->lease_chan_max_ppt = lease_chan_max_ppt; diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index a01366d5a..3d51d0203 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -214,10 +214,7 @@ void delay_then_reconnect(struct channel *channel, u32 seconds_delay, d = tal(channel, struct delayed_reconnect); d->channel = channel; d->seconds_delayed = seconds_delay; - if (addrhint) - d->addrhint = tal_dup(d, struct wireaddr_internal, addrhint); - else - d->addrhint = NULL; + d->addrhint = tal_dup_or_null(d, struct wireaddr_internal, addrhint); log_debug(channel->log, "Will try reconnect in %u seconds", seconds_delay); diff --git a/lightningd/htlc_end.c b/lightningd/htlc_end.c index d2ad10257..129ba638c 100644 --- a/lightningd/htlc_end.c +++ b/lightningd/htlc_end.c @@ -144,10 +144,7 @@ struct htlc_in *new_htlc_in(const tal_t *ctx, hin->payment_hash = *payment_hash; hin->status = NULL; hin->fail_immediate = fail_immediate; - if (shared_secret) - hin->shared_secret = tal_dup(hin, struct secret, shared_secret); - else - hin->shared_secret = NULL; + hin->shared_secret = tal_dup_or_null(hin, struct secret, shared_secret); if (blinding) { hin->blinding = tal_dup(hin, struct pubkey, blinding); hin->blinding_ss = *blinding_ss; @@ -303,10 +300,7 @@ struct htlc_out *new_htlc_out(const tal_t *ctx, hout->preimage = NULL; hout->timeout = NULL; - if (blinding) - hout->blinding = tal_dup(hout, struct pubkey, blinding); - else - hout->blinding = NULL; + hout->blinding = tal_dup_or_null(hout, struct pubkey, blinding); hout->am_origin = am_origin; if (am_origin) { hout->partid = partid; diff --git a/lightningd/log.c b/lightningd/log.c index 292eba39c..0d975934c 100644 --- a/lightningd/log.c +++ b/lightningd/log.c @@ -306,11 +306,8 @@ new_log(const tal_t *ctx, struct log_book *record, * by log entries, too */ log->prefix = log_prefix_new(log->lr, take(tal_vfmt(NULL, fmt, ap))); va_end(ap); - if (default_node_id) - log->default_node_id = tal_dup(log, struct node_id, + log->default_node_id = tal_dup_or_null(log, struct node_id, default_node_id); - else - log->default_node_id = NULL; /* Initialized on first use */ log->print_level = NULL; diff --git a/lightningd/pay.c b/lightningd/pay.c index 00fdafdf8..346cc8ecc 100644 --- a/lightningd/pay.c +++ b/lightningd/pay.c @@ -495,11 +495,8 @@ remote_routing_failure(const tal_t *ctx, routing_failure->failcode = failcode; routing_failure->msg = tal_dup_talarr(routing_failure, u8, failuremsg); - if (erring_node != NULL) - routing_failure->erring_node = - tal_dup(routing_failure, struct node_id, erring_node); - else - routing_failure->erring_node = NULL; + routing_failure->erring_node = + tal_dup_or_null(routing_failure, struct node_id, erring_node); if (erring_channel != NULL) { routing_failure->erring_channel = tal_dup( @@ -1081,10 +1078,8 @@ send_payment_core(struct lightningd *ld, payment->payment_hash = *rhash; payment->partid = partid; payment->groupid = group; - if (destination) - payment->destination = tal_dup(payment, struct node_id, destination); - else - payment->destination = NULL; + payment->destination = tal_dup_or_null(payment, struct node_id, + destination); payment->status = PAYMENT_PENDING; payment->msatoshi = msat; payment->msatoshi_sent = first_hop->amount; @@ -1092,14 +1087,8 @@ send_payment_core(struct lightningd *ld, payment->timestamp = time_now().ts.tv_sec; payment->payment_preimage = NULL; payment->path_secrets = tal_steal(payment, path_secrets); - if (route_nodes) - payment->route_nodes = tal_steal(payment, route_nodes); - else - payment->route_nodes = NULL; - if (route_channels) - payment->route_channels = tal_steal(payment, route_channels); - else - payment->route_channels = NULL; + payment->route_nodes = tal_steal(payment, route_nodes); + payment->route_channels = tal_steal(payment, route_channels); payment->failonion = NULL; if (label != NULL) payment->label = tal_strdup(payment, label); @@ -1109,10 +1098,8 @@ send_payment_core(struct lightningd *ld, payment->invstring = tal_strdup(payment, invstring); else payment->invstring = NULL; - if (local_offer_id) - payment->local_offer_id = tal_dup(payment, struct sha256, local_offer_id); - else - payment->local_offer_id = NULL; + payment->local_offer_id = tal_dup_or_null(payment, struct sha256, + local_offer_id); /* We write this into db when HTLC is actually sent. */ wallet_payment_setup(ld->wallet, payment); diff --git a/lightningd/subd.c b/lightningd/subd.c index a42849b48..bbd04d40c 100644 --- a/lightningd/subd.c +++ b/lightningd/subd.c @@ -753,10 +753,7 @@ static struct subd *new_subd(struct lightningd *ld, list_head_init(&sd->reqs); sd->channel = channel; sd->rcvd_version = false; - if (node_id) - sd->node_id = tal_dup(sd, struct node_id, node_id); - else - sd->node_id = NULL; + sd->node_id = tal_dup_or_null(sd, struct node_id, node_id); /* conn actually owns daemon: we die when it does. */ sd->conn = io_new_conn(ld, msg_fd, msg_setup, sd); diff --git a/onchaind/onchaind.c b/onchaind/onchaind.c index 244971975..869c772ae 100644 --- a/onchaind/onchaind.c +++ b/onchaind/onchaind.c @@ -929,11 +929,8 @@ new_tracked_output(struct tracked_output ***outs, if (htlc) out->htlc = *htlc; out->wscript = tal_steal(out, wscript); - if (remote_htlc_sig) - out->remote_htlc_sig = tal_dup(out, struct bitcoin_signature, + out->remote_htlc_sig = tal_dup_or_null(out, struct bitcoin_signature, remote_htlc_sig); - else - out->remote_htlc_sig = NULL; tal_arr_expand(outs, out); diff --git a/plugins/pay.c b/plugins/pay.c index b454d7a8c..9c0b4dae7 100644 --- a/plugins/pay.c +++ b/plugins/pay.c @@ -2361,9 +2361,7 @@ static struct command_result *json_paymod(struct command *cmd, feature_offered(b11->features, OPT_VAR_ONION); p->payment_hash = tal_dup(p, struct sha256, &b11->payment_hash); p->payment_secret = - b11->payment_secret - ? tal_dup(p, struct secret, b11->payment_secret) - : NULL; + tal_dup_or_null(p, struct secret, b11->payment_secret); p->routes = tal_steal(p, b11->routes); p->min_final_cltv_expiry = b11->min_final_cltv_expiry; p->features = tal_steal(p, b11->features);