Skip to content

Commit c28f5e7

Browse files
whitslackrustyrussell
authored andcommitted
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
1 parent 4031860 commit c28f5e7

File tree

4 files changed

+8
-20
lines changed

4 files changed

+8
-20
lines changed

common/json_param.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -444,11 +444,9 @@ struct command_result *param_escaped_string(struct command *cmd,
444444
const char **str)
445445
{
446446
if (tok->type == JSMN_STRING) {
447-
struct json_escape *esc;
448447
/* jsmn always gives us ~ well-formed strings. */
449-
esc = json_escape_string_(cmd, buffer + tok->start,
450-
tok->end - tok->start);
451-
*str = json_escape_unescape(cmd, esc);
448+
*str = json_escape_unescape_len(cmd, buffer + tok->start,
449+
tok->end - tok->start);
452450
if (*str)
453451
return NULL;
454452
}

common/test/run-bolt12_decode.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,11 @@ int main(int argc, char *argv[])
190190
char *fail;
191191
const char *str;
192192
size_t dlen;
193-
struct json_escape *esc;
194193

195194
assert(json_to_bool(json, json_get_member(json, t, "valid"), &valid));
196195
strtok = json_get_member(json, t, "string");
197-
esc = json_escape_string_(tmpctx, json + strtok->start,
198-
strtok->end - strtok->start);
199-
str = json_escape_unescape(tmpctx, esc);
196+
str = json_escape_unescape_len(tmpctx, json + strtok->start,
197+
strtok->end - strtok->start);
200198
actual = (string_to_data(tmpctx, str, strlen(str),
201199
"lno", &dlen, &fail) != NULL);
202200
assert(actual == valid);

lightningd/jsonrpc.c

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,20 +1393,14 @@ static void setup_command_usage(struct lightningd *ld,
13931393
bool jsonrpc_command_add(struct jsonrpc *rpc, struct json_command *command,
13941394
const char *usage TAKES)
13951395
{
1396-
struct json_escape *esc;
13971396
const char *unescaped;
13981397

13991398
if (!command_add(rpc, command))
14001399
return false;
14011400

1402-
esc = json_escape_string_(tmpctx, usage, strlen(usage));
1403-
unescaped = json_escape_unescape(command, esc);
1401+
unescaped = json_escape_unescape_len(command, usage, strlen(usage));
14041402
if (!unescaped)
1405-
unescaped = tal_strdup(command, usage);
1406-
else {
1407-
if (taken(usage))
1408-
tal_free(usage);
1409-
}
1403+
return false;
14101404

14111405
strmap_add(&rpc->usagemap, command->name, unescaped);
14121406
tal_add_destructor2(command, destroy_json_command, rpc);

lightningd/runes.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,8 @@ static struct rune_altern *rune_altern_from_json(const tal_t *ctx,
484484
/* We still need to unescape here, for \\ -> \. JSON doesn't
485485
* allow unnecessary \ */
486486
const char *unescape;
487-
struct json_escape *e = json_escape_string_(tmpctx,
488-
buffer + tok->start,
489-
tok->end - tok->start);
490-
unescape = json_escape_unescape(tmpctx, e);
487+
unescape = json_escape_unescape_len(tmpctx, buffer + tok->start,
488+
tok->end - tok->start);
491489
if (!unescape)
492490
return NULL;
493491

0 commit comments

Comments
 (0)