mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-21 14:24:09 +01:00
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:
parent
b6d347a6d3
commit
981d82c406
7 changed files with 22 additions and 34 deletions
|
@ -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;
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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! */
|
||||
|
|
|
@ -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; i<tal_count(hook->hooks); i++) {
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue