common/utils: add tal_strdup_or_null helper.

It's not that uncommon to want to pass NULL through, for optional strings.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-07-22 16:42:57 +09:30
parent b6d347a6d3
commit 981d82c406
7 changed files with 22 additions and 34 deletions

View file

@ -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); struct chain_coin_mvt *mvt = tal(ctx, struct chain_coin_mvt);
if (account_name) mvt->account_name = tal_strdup_or_null(mvt, account_name);
mvt->account_name = tal_strdup(mvt, account_name);
else
mvt->account_name = NULL;
mvt->tx_txid = tx_txid; mvt->tx_txid = tx_txid;
mvt->outpoint = outpoint; mvt->outpoint = outpoint;
mvt->originating_acct = NULL; 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); struct coin_mvt *mvt = tal(ctx, struct coin_mvt);
mvt->account_id = tal_strdup(mvt, chain_mvt->account_name); mvt->account_id = tal_strdup(mvt, chain_mvt->account_name);
if (chain_mvt->originating_acct) mvt->originating_acct =
mvt->originating_acct = tal_strdup_or_null(mvt, chain_mvt->originating_acct);
tal_strdup(mvt, chain_mvt->originating_acct);
else
mvt->originating_acct = NULL;
mvt->hrp_name = tal_strdup(mvt, hrp_name); mvt->hrp_name = tal_strdup(mvt, hrp_name);
mvt->type = CHAIN_MVT; mvt->type = CHAIN_MVT;

View file

@ -13,10 +13,7 @@ struct configvar *configvar_new(const tal_t *ctx,
const char *configline) const char *configline)
{ {
struct configvar *cv = tal(ctx, struct configvar); struct configvar *cv = tal(ctx, struct configvar);
if (file) cv->file = tal_strdup_or_null(cv, file);
cv->file = tal_strdup(cv, file);
else
cv->file = NULL;
cv->src = src; cv->src = src;
cv->linenum = linenum; cv->linenum = linenum;
cv->configline = tal_strdup(cv, configline); cv->configline = tal_strdup(cv, configline);

View file

@ -148,6 +148,16 @@ char *utf8_str(const tal_t *ctx, const u8 *buf TAKES, size_t buflen)
return ret; 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) int tmpdir_mkstemp(const tal_t *ctx, const char *template TAKES, char **created)
{ {
char *tmpdir = getenv("TMPDIR"); char *tmpdir = getenv("TMPDIR");

View file

@ -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. */ /* 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); 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. */ /* Use the POSIX C locale. */
void setup_locale(void); void setup_locale(void);

View file

@ -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->txs, fmt_bitcoin_tx(txs->txs, otx->tx));
tal_arr_expand(&txs->allowhighfees, otx->allowhighfees); tal_arr_expand(&txs->allowhighfees, otx->allowhighfees);
tal_arr_expand(&txs->cmd_id, tal_arr_expand(&txs->cmd_id, tal_strdup_or_null(txs, otx->cmd_id));
otx->cmd_id ? tal_strdup(txs, otx->cmd_id) : NULL);
} }
tal_free(cleanup_ctx); tal_free(cleanup_ctx);
@ -277,10 +276,7 @@ void broadcast_tx_(struct chain_topology *topo,
otx->cbarg = cbarg; otx->cbarg = cbarg;
if (taken(otx->cbarg)) if (taken(otx->cbarg))
tal_steal(otx, otx->cbarg); tal_steal(otx, otx->cbarg);
if (cmd_id) otx->cmd_id = tal_strdup_or_null(otx, cmd_id);
otx->cmd_id = tal_strdup(otx, cmd_id);
else
otx->cmd_id = NULL;
/* Note that if the minimum block is N, we broadcast it when /* Note that if the minimum block is N, we broadcast it when
* we have block N-1! */ * we have block N-1! */

View file

@ -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->cb_arg = tal_steal(ph_req, cb_arg);
ph_req->db = ld->wallet->db; ph_req->db = ld->wallet->db;
ph_req->ld = ld; ph_req->ld = ld;
if (cmd_id) ph_req->cmd_id = tal_strdup_or_null(ph_req, cmd_id);
ph_req->cmd_id = tal_strdup(ph_req, cmd_id);
else
ph_req->cmd_id = NULL;
list_head_init(&ph_req->call_chain); list_head_init(&ph_req->call_chain);
for (size_t i=0; i<tal_count(hook->hooks); i++) { for (size_t i=0; i<tal_count(hook->hooks); i++) {

View file

@ -47,12 +47,7 @@ static struct income_event *chain_to_income(const tal_t *ctx,
inc->currency = tal_strdup(inc, ev->currency); inc->currency = tal_strdup(inc, ev->currency);
inc->timestamp = ev->timestamp; inc->timestamp = ev->timestamp;
inc->outpoint = tal_dup(inc, struct bitcoin_outpoint, &ev->outpoint); inc->outpoint = tal_dup(inc, struct bitcoin_outpoint, &ev->outpoint);
inc->desc = tal_strdup_or_null(inc, ev->desc);
if (ev->desc)
inc->desc = tal_strdup(inc, ev->desc);
else
inc->desc = NULL;
inc->txid = tal_dup_or_null(inc, struct bitcoin_txid, ev->spending_txid); 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); 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->timestamp = ev->timestamp;
inc->outpoint = NULL; inc->outpoint = NULL;
inc->txid = NULL; inc->txid = NULL;
if (ev->desc) inc->desc = tal_strdup_or_null(inc, ev->desc);
inc->desc = tal_strdup(inc, ev->desc);
else
inc->desc = NULL;
inc->payment_id = tal_dup_or_null(inc, struct sha256, ev->payment_id); inc->payment_id = tal_dup_or_null(inc, struct sha256, ev->payment_id);
return inc; return inc;