diff --git a/common/coin_mvt.c b/common/coin_mvt.c index 4b087a73a..f7d82b128 100644 --- a/common/coin_mvt.c +++ b/common/coin_mvt.c @@ -98,11 +98,7 @@ static struct chain_coin_mvt *new_chain_coin_mvt(const tal_t *ctx, { struct chain_coin_mvt *mvt = tal(ctx, struct chain_coin_mvt); - if (account_name) - mvt->account_name = tal_strdup(mvt, account_name); - else - mvt->account_name = NULL; - + mvt->account_name = tal_strdup_or_null(mvt, account_name); mvt->tx_txid = tx_txid; mvt->outpoint = outpoint; mvt->originating_acct = NULL; @@ -393,11 +389,8 @@ struct coin_mvt *finalize_chain_mvt(const tal_t *ctx, struct coin_mvt *mvt = tal(ctx, struct coin_mvt); mvt->account_id = tal_strdup(mvt, chain_mvt->account_name); - if (chain_mvt->originating_acct) - mvt->originating_acct = - tal_strdup(mvt, chain_mvt->originating_acct); - else - mvt->originating_acct = NULL; + mvt->originating_acct = + tal_strdup_or_null(mvt, chain_mvt->originating_acct); mvt->hrp_name = tal_strdup(mvt, hrp_name); mvt->type = CHAIN_MVT; diff --git a/common/configvar.c b/common/configvar.c index 3aae4b0ba..84ddb1646 100644 --- a/common/configvar.c +++ b/common/configvar.c @@ -13,10 +13,7 @@ struct configvar *configvar_new(const tal_t *ctx, const char *configline) { struct configvar *cv = tal(ctx, struct configvar); - if (file) - cv->file = tal_strdup(cv, file); - else - cv->file = NULL; + cv->file = tal_strdup_or_null(cv, file); cv->src = src; cv->linenum = linenum; cv->configline = tal_strdup(cv, configline); diff --git a/common/utils.c b/common/utils.c index 165a1d556..0c9ecc5ae 100644 --- a/common/utils.c +++ b/common/utils.c @@ -148,6 +148,16 @@ char *utf8_str(const tal_t *ctx, const u8 *buf TAKES, size_t buflen) return ret; } +char *tal_strdup_or_null(const tal_t *ctx, const char *str) +{ + if (!str) { + /* You might have taken NULL; that's legal! Release now. */ + taken(str); + return NULL; + } + return tal_strdup(ctx, str); +} + int tmpdir_mkstemp(const tal_t *ctx, const char *template TAKES, char **created) { char *tmpdir = getenv("TMPDIR"); diff --git a/common/utils.h b/common/utils.h index e1793bdea..cc49aa3b5 100644 --- a/common/utils.h +++ b/common/utils.h @@ -88,6 +88,9 @@ bool utf8_check(const void *buf, size_t buflen); /* Check it's UTF-8, return copy (or same if TAKES), or NULL if not valid. */ char *utf8_str(const tal_t *ctx, const u8 *buf TAKES, size_t buflen); +/* Strdup, or pass through NULL */ +char *tal_strdup_or_null(const tal_t *ctx, const char *str); + /* Use the POSIX C locale. */ void setup_locale(void); diff --git a/lightningd/chaintopology.c b/lightningd/chaintopology.c index c7ffa5bb3..3c72da362 100644 --- a/lightningd/chaintopology.c +++ b/lightningd/chaintopology.c @@ -186,8 +186,7 @@ static void rebroadcast_txs(struct chain_topology *topo) tal_arr_expand(&txs->txs, fmt_bitcoin_tx(txs->txs, otx->tx)); tal_arr_expand(&txs->allowhighfees, otx->allowhighfees); - tal_arr_expand(&txs->cmd_id, - otx->cmd_id ? tal_strdup(txs, otx->cmd_id) : NULL); + tal_arr_expand(&txs->cmd_id, tal_strdup_or_null(txs, otx->cmd_id)); } tal_free(cleanup_ctx); @@ -277,10 +276,7 @@ void broadcast_tx_(struct chain_topology *topo, otx->cbarg = cbarg; if (taken(otx->cbarg)) tal_steal(otx, otx->cbarg); - if (cmd_id) - otx->cmd_id = tal_strdup(otx, cmd_id); - else - otx->cmd_id = NULL; + otx->cmd_id = tal_strdup_or_null(otx, cmd_id); /* Note that if the minimum block is N, we broadcast it when * we have block N-1! */ diff --git a/lightningd/plugin_hook.c b/lightningd/plugin_hook.c index 2456aec48..fe392f486 100644 --- a/lightningd/plugin_hook.c +++ b/lightningd/plugin_hook.c @@ -262,10 +262,7 @@ bool plugin_hook_call_(struct lightningd *ld, const struct plugin_hook *hook, ph_req->cb_arg = tal_steal(ph_req, cb_arg); ph_req->db = ld->wallet->db; ph_req->ld = ld; - if (cmd_id) - ph_req->cmd_id = tal_strdup(ph_req, cmd_id); - else - ph_req->cmd_id = NULL; + ph_req->cmd_id = tal_strdup_or_null(ph_req, cmd_id); list_head_init(&ph_req->call_chain); for (size_t i=0; ihooks); i++) { diff --git a/plugins/bkpr/incomestmt.c b/plugins/bkpr/incomestmt.c index 51dda2456..ee357997e 100644 --- a/plugins/bkpr/incomestmt.c +++ b/plugins/bkpr/incomestmt.c @@ -47,12 +47,7 @@ static struct income_event *chain_to_income(const tal_t *ctx, inc->currency = tal_strdup(inc, ev->currency); inc->timestamp = ev->timestamp; inc->outpoint = tal_dup(inc, struct bitcoin_outpoint, &ev->outpoint); - - if (ev->desc) - inc->desc = tal_strdup(inc, ev->desc); - else - inc->desc = NULL; - + inc->desc = tal_strdup_or_null(inc, ev->desc); inc->txid = tal_dup_or_null(inc, struct bitcoin_txid, ev->spending_txid); inc->payment_id = tal_dup_or_null(inc, struct sha256, ev->payment_id); @@ -75,10 +70,7 @@ static struct income_event *channel_to_income(const tal_t *ctx, inc->timestamp = ev->timestamp; inc->outpoint = NULL; inc->txid = NULL; - if (ev->desc) - inc->desc = tal_strdup(inc, ev->desc); - else - inc->desc = NULL; + inc->desc = tal_strdup_or_null(inc, ev->desc); inc->payment_id = tal_dup_or_null(inc, struct sha256, ev->payment_id); return inc;