diff --git a/common/json.c b/common/json.c index 08ce199a1..50f444fb8 100644 --- a/common/json.c +++ b/common/json.c @@ -35,6 +35,11 @@ bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str) return strncmp(buffer + tok->start, str, tok->end - tok->start) == 0; } +char *json_strdup(const tal_t *ctx, const char *buffer, const jsmntok_t *tok) +{ + return tal_strndup(ctx, buffer + tok->start, tok->end - tok->start); +} + bool json_to_u64(const char *buffer, const jsmntok_t *tok, uint64_t *num) { diff --git a/common/json.h b/common/json.h index 2882cf185..6ba711e8c 100644 --- a/common/json.h +++ b/common/json.h @@ -23,6 +23,9 @@ int json_tok_len(const jsmntok_t *t); /* Is this a string equal to str? */ bool json_tok_streq(const char *buffer, const jsmntok_t *tok, const char *str); +/* Allocate a tal string copy */ +char *json_strdup(const tal_t *ctx, const char *buffer, const jsmntok_t *tok); + /* Extract number from this (may be a string, or a number literal) */ bool json_to_number(const char *buffer, const jsmntok_t *tok, unsigned int *num); diff --git a/lightningd/connect_control.c b/lightningd/connect_control.c index b488f7225..127d88e34 100644 --- a/lightningd/connect_control.c +++ b/lightningd/connect_control.c @@ -99,8 +99,7 @@ static void json_connect(struct command *cmd, return; /* Check for id@addrport form */ - id_str = tal_strndup(cmd, buffer + idtok->start, - idtok->end - idtok->start); + id_str = json_strdup(cmd, buffer, idtok); atptr = strchr(id_str, '@'); if (atptr) { int atidx = atptr - id_str; diff --git a/lightningd/plugin.c b/lightningd/plugin.c index 9589d37e4..86d80c4fa 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -551,14 +551,12 @@ static bool plugin_opt_add(struct plugin *plugin, const char *buffer, buffer + nametok->start); popt->value = NULL; if (defaulttok) { - popt->value = tal_strndup(popt, buffer + defaulttok->start, - defaulttok->end - defaulttok->start); + popt->value = json_strdup(popt, buffer, defaulttok); popt->description = tal_fmt( popt, "%.*s (default: %s)", desctok->end - desctok->start, buffer + desctok->start, popt->value); } else { - popt->description = tal_strndup(popt, buffer + desctok->start, - desctok->end - desctok->start); + popt->description = json_strdup(popt, buffer, desctok); } list_add_tail(&plugin->plugin_opts, &popt->list); @@ -722,14 +720,10 @@ static bool plugin_rpcmethod_add(struct plugin *plugin, } cmd = notleak(tal(plugin, struct json_command)); - cmd->name = tal_strndup(cmd, buffer + nametok->start, - nametok->end - nametok->start); - cmd->description = tal_strndup(cmd, buffer + desctok->start, - desctok->end - desctok->start); + cmd->name = json_strdup(cmd, buffer, nametok); + cmd->description = json_strdup(cmd, buffer, desctok); if (longdesctok) - cmd->verbose = - tal_strndup(cmd, buffer + longdesctok->start, - longdesctok->end - longdesctok->start); + cmd->verbose = json_strdup(cmd, buffer, longdesctok); else cmd->verbose = cmd->description;