common: add json_dup_contents() to duplicate toks and buffer.
What changed, and why it matters
This commit is a routine code cleanup. It introduces a shared helper function that copies a JSON buffer and its associated tokens, then replaces two existing hand-rolled copy operations in the JSON-RPC code with that helper. There is no indication of a security fix or behavior change.
No security action required. Treat as normal maintenance/refactoring commit.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch adds json_dup_contents() to common/json_parse.c/.h, which duplicates a JSON buffer and a jsmntok_t array using tal_dup_arr(). It then refactors two call sites in lightningd/jsonrpc.c (rpc_command_hook_callback and parse_request) to use this helper instead of separate json_tok_copy/tal_dup_talarr calls. A test file also gains an unrelated include. The change is purely refactorization with no functional difference visible in the diff.
Changed components
common/json_parse.ccommon/json_parse.hlightningd/jsonrpc.cInspect captured patch +23 / −4
diff --git a/common/json_parse.c b/common/json_parse.c
index dc491b2a..7841e082 100644
--- a/common/json_parse.c
+++ b/common/json_parse.c
@@ -667,3 +667,13 @@ json_tok_channel_id(const char *buffer, const jsmntok_t *tok,
return hex_decode(buffer + tok->start, tok->end - tok->start,
cid, sizeof(*cid));
}
+
+void json_dup_contents(const tal_t *ctx,
+ const char *buffer,
+ const jsmntok_t *tok,
+ const char **new_buffer,
+ const jsmntok_t **new_toks)
+{
+ *new_buffer = tal_dup_arr(ctx, char, buffer, tok->end, 0);
+ *new_toks = tal_dup_arr(ctx, jsmntok_t, tok, json_next(tok) - tok, 0);
+}
diff --git a/common/json_parse.h b/common/json_parse.h
index 8f3bcfc3..4706c377 100644
--- a/common/json_parse.h
+++ b/common/json_parse.h
@@ -135,6 +135,13 @@ const char *json_scan(const tal_t *ctx,
const char *guide,
...);
+/* Duplicate the tok(s) and buffer required (don't assume they're tal objects!) */
+void json_dup_contents(const tal_t *ctx,
+ const char *buffer,
+ const jsmntok_t *tok,
+ const char **new_buffer,
+ const jsmntok_t **new_toks);
+
/* eg. JSON_SCAN(json_to_bool, &boolvar) */
#define JSON_SCAN(fmt, var) \
json_scan, \
diff --git a/common/test/run-features.c b/common/test/run-features.c
index e6936d5a..9d2d37d4 100644
--- a/common/test/run-features.c
+++ b/common/test/run-features.c
@@ -1,6 +1,7 @@
#include "config.h"
#include "../features.c"
#include "../memleak.c"
+#include <bitcoin/script.h>
#include <ccan/mem/mem.h>
#include <common/setup.h>
diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c
index 95d32cb1..8670bc3c 100644
--- a/lightningd/jsonrpc.c
+++ b/lightningd/jsonrpc.c
@@ -991,8 +991,8 @@ rpc_command_hook_callback(struct rpc_command_hook_payload *p,
if (tok) {
/* We need to make copies here, as buffer and tokens
* can be reused. */
- p->custom_replace = json_tok_copy(p, tok);
- p->custom_buffer = tal_dup_talarr(p, char, buffer);
+ json_dup_contents(p, buffer, tok,
+ &p->custom_buffer, &p->custom_replace);
return true;
}
@@ -1139,8 +1139,9 @@ parse_request(struct json_connection *jcon, const jsmntok_t tok[])
rpc_hook = tal(c, struct rpc_command_hook_payload);
rpc_hook->cmd = c;
/* Duplicate since we might outlive the connection */
- rpc_hook->buffer = tal_dup_talarr(rpc_hook, char, jcon->buffer);
- rpc_hook->request = tal_dup_talarr(rpc_hook, jsmntok_t, tok);
+ json_dup_contents(rpc_hook, jcon->buffer, tok,
+ &rpc_hook->buffer,
+ &rpc_hook->request);
/* NULL the custom_ values for the hooks */
rpc_hook->custom_result = NULL;
Why this scored 12/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.