Skip to content

Commit e5318ee

Browse files
committed
common: add json_dup_contents() to duplicate toks and buffer.
We do this in several places, might as well make it common code. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
1 parent 7c9e016 commit e5318ee

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

common/json_parse.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,3 +667,13 @@ json_tok_channel_id(const char *buffer, const jsmntok_t *tok,
667667
return hex_decode(buffer + tok->start, tok->end - tok->start,
668668
cid, sizeof(*cid));
669669
}
670+
671+
void json_dup_contents(const tal_t *ctx,
672+
const char *buffer,
673+
const jsmntok_t *tok,
674+
const char **new_buffer,
675+
const jsmntok_t **new_toks)
676+
{
677+
*new_buffer = tal_dup_arr(ctx, char, buffer, tok->end, 0);
678+
*new_toks = tal_dup_arr(ctx, jsmntok_t, tok, json_next(tok) - tok, 0);
679+
}

common/json_parse.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ const char *json_scan(const tal_t *ctx,
135135
const char *guide,
136136
...);
137137

138+
/* Duplicate the tok(s) and buffer required (don't assume they're tal objects!) */
139+
void json_dup_contents(const tal_t *ctx,
140+
const char *buffer,
141+
const jsmntok_t *tok,
142+
const char **new_buffer,
143+
const jsmntok_t **new_toks);
144+
138145
/* eg. JSON_SCAN(json_to_bool, &boolvar) */
139146
#define JSON_SCAN(fmt, var) \
140147
json_scan, \

common/test/run-features.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "config.h"
22
#include "../features.c"
33
#include "../memleak.c"
4+
#include <bitcoin/script.h>
45
#include <ccan/mem/mem.h>
56
#include <common/setup.h>
67

lightningd/jsonrpc.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -991,8 +991,8 @@ rpc_command_hook_callback(struct rpc_command_hook_payload *p,
991991
if (tok) {
992992
/* We need to make copies here, as buffer and tokens
993993
* can be reused. */
994-
p->custom_replace = json_tok_copy(p, tok);
995-
p->custom_buffer = tal_dup_talarr(p, char, buffer);
994+
json_dup_contents(p, buffer, tok,
995+
&p->custom_buffer, &p->custom_replace);
996996
return true;
997997
}
998998

@@ -1139,8 +1139,9 @@ parse_request(struct json_connection *jcon, const jsmntok_t tok[])
11391139
rpc_hook = tal(c, struct rpc_command_hook_payload);
11401140
rpc_hook->cmd = c;
11411141
/* Duplicate since we might outlive the connection */
1142-
rpc_hook->buffer = tal_dup_talarr(rpc_hook, char, jcon->buffer);
1143-
rpc_hook->request = tal_dup_talarr(rpc_hook, jsmntok_t, tok);
1142+
json_dup_contents(rpc_hook, jcon->buffer, tok,
1143+
&rpc_hook->buffer,
1144+
&rpc_hook->request);
11441145

11451146
/* NULL the custom_ values for the hooks */
11461147
rpc_hook->custom_result = NULL;

0 commit comments

Comments
 (0)