use json_escape_unescape_len()

This avoids making an extra copy of the escaped string.

Note that jsonrpc_command_add() no longer accepts usage strings
containing invalid escape sequences. (Previously, it would quietly
accept such a string without unescaping anything.)

Changelog-None
This commit is contained in:
Matt Whitlock
2025-08-15 10:25:29 +09:30
committed by Rusty Russell
parent 4031860630
commit c28f5e70a8
4 changed files with 8 additions and 20 deletions

View File

@@ -1393,20 +1393,14 @@ static void setup_command_usage(struct lightningd *ld,
bool jsonrpc_command_add(struct jsonrpc *rpc, struct json_command *command,
const char *usage TAKES)
{
struct json_escape *esc;
const char *unescaped;
if (!command_add(rpc, command))
return false;
esc = json_escape_string_(tmpctx, usage, strlen(usage));
unescaped = json_escape_unescape(command, esc);
unescaped = json_escape_unescape_len(command, usage, strlen(usage));
if (!unescaped)
unescaped = tal_strdup(command, usage);
else {
if (taken(usage))
tal_free(usage);
}
return false;
strmap_add(&rpc->usagemap, command->name, unescaped);
tal_add_destructor2(command, destroy_json_command, rpc);

View File

@@ -484,10 +484,8 @@ static struct rune_altern *rune_altern_from_json(const tal_t *ctx,
/* We still need to unescape here, for \\ -> \. JSON doesn't
* allow unnecessary \ */
const char *unescape;
struct json_escape *e = json_escape_string_(tmpctx,
buffer + tok->start,
tok->end - tok->start);
unescape = json_escape_unescape(tmpctx, e);
unescape = json_escape_unescape_len(tmpctx, buffer + tok->start,
tok->end - tok->start);
if (!unescape)
return NULL;