From 86c517ac9b260ed868464d176654355e766784a8 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 8 Dec 2018 11:07:56 +1030 Subject: [PATCH] common/json: add context arg to json_parse_input. All callers currently just hand the same arg twice, but plugins might want this different. Signed-off-by: Rusty Russell --- common/json.c | 5 +++-- common/json.h | 3 ++- common/test/run-json_remove.c | 2 +- lightningd/bitcoind.c | 8 +++++--- lightningd/jsonrpc.c | 2 +- lightningd/plugin.c | 3 ++- lightningd/test/run-jsonrpc.c | 6 +++--- 7 files changed, 17 insertions(+), 12 deletions(-) diff --git a/common/json.c b/common/json.c index 549e5445f..0492e5d38 100644 --- a/common/json.c +++ b/common/json.c @@ -172,13 +172,14 @@ const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index) return NULL; } -jsmntok_t *json_parse_input(const char *input, int len, bool *valid) +jsmntok_t *json_parse_input(const tal_t *ctx, + const char *input, int len, bool *valid) { jsmn_parser parser; jsmntok_t *toks; int ret; - toks = tal_arr(input, jsmntok_t, 10); + toks = tal_arr(ctx, jsmntok_t, 10); toks[0].type = JSMN_UNDEFINED; jsmn_init(&parser); diff --git a/common/json.h b/common/json.h index 38120ea15..9c3328fd5 100644 --- a/common/json.h +++ b/common/json.h @@ -55,7 +55,8 @@ const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[], const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index); /* If input is complete and valid, return tokens. */ -jsmntok_t *json_parse_input(const char *input, int len, bool *valid); +jsmntok_t *json_parse_input(const tal_t *ctx, + const char *input, int len, bool *valid); /* Convert a jsmntype_t enum to a human readable string. */ const char *jsmntype_to_string(jsmntype_t t); diff --git a/common/test/run-json_remove.c b/common/test/run-json_remove.c index 5328e684a..f20df9654 100644 --- a/common/test/run-json_remove.c +++ b/common/test/run-json_remove.c @@ -25,7 +25,7 @@ static struct json *json_parse(const tal_t * ctx, const char *str) j->buffer = tal_strdup(j, str); convert_quotes(j->buffer); bool ok; - j->toks = json_parse_input(j->buffer, strlen(j->buffer), &ok); + j->toks = json_parse_input(j, j->buffer, strlen(j->buffer), &ok); assert(ok); j->toks = json_tok_copy(j, j->toks); return j; diff --git a/lightningd/bitcoind.c b/lightningd/bitcoind.c index 918e891d2..a9693667f 100644 --- a/lightningd/bitcoind.c +++ b/lightningd/bitcoind.c @@ -309,7 +309,7 @@ static bool extract_feerate(struct bitcoin_cli *bcli, const jsmntok_t *tokens, *feeratetok; bool valid; - tokens = json_parse_input(output, output_bytes, &valid); + tokens = json_parse_input(output, output, output_bytes, &valid); if (!tokens) fatal("%s: %s response", bcli_args(tmpctx, bcli), @@ -541,7 +541,8 @@ static bool process_gettxout(struct bitcoin_cli *bcli) return true; } - tokens = json_parse_input(bcli->output, bcli->output_bytes, &valid); + tokens = json_parse_input(bcli->output, bcli->output, bcli->output_bytes, + &valid); if (!tokens) fatal("%s: %s response", bcli_args(tmpctx, bcli), valid ? "partial" : "invalid"); @@ -601,7 +602,8 @@ static bool process_getblock(struct bitcoin_cli *bcli) struct bitcoin_txid txid; bool valid; - tokens = json_parse_input(bcli->output, bcli->output_bytes, &valid); + tokens = json_parse_input(bcli->output, bcli->output, bcli->output_bytes, + &valid); if (!tokens) { /* Most likely we are running on a pruned node, call * the callback with NULL to indicate failure */ diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index e969e64a9..213d5de9a 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -652,7 +652,7 @@ static struct io_plan *read_json(struct io_conn *conn, return io_wait(conn, conn, read_json, jcon); } - toks = json_parse_input(jcon->buffer, jcon->used, &valid); + toks = json_parse_input(jcon->buffer, jcon->buffer, jcon->used, &valid); if (!toks) { if (!valid) { log_unusual(jcon->log, diff --git a/lightningd/plugin.c b/lightningd/plugin.c index 5325b7031..0d7150bba 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -249,7 +249,8 @@ static bool plugin_read_json_one(struct plugin *plugin) /* FIXME: This could be done more efficiently by storing the * toks and doing an incremental parse, like lightning-cli * does. */ - toks = json_parse_input(plugin->buffer, plugin->used, &valid); + toks = json_parse_input(plugin->buffer, plugin->buffer, plugin->used, + &valid); if (!toks) { if (!valid) { plugin_kill(plugin, "Failed to parse JSON response '%.*s'", diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index 146ae5d5f..24f5dbee7 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -76,7 +76,7 @@ static int test_json_filter(void) str = tal_strndup(result, membuf_elems(&result->outbuf), membuf_num_elems(&result->outbuf)); - toks = json_parse_input(str, strlen(str), &valid); + toks = json_parse_input(str, str, strlen(str), &valid); assert(valid); assert(toks); @@ -174,7 +174,7 @@ static void test_json_stream(void) * timeout. */ input = "{\"x\":\"x\"}{\"y\":\"y\"}"; talstr = tal_strndup(NULL, input, strlen(input)); - toks = json_parse_input(talstr, strlen(talstr), &valid); + toks = json_parse_input(talstr, talstr, strlen(talstr), &valid); assert(toks); assert(tal_count(toks) == 4); assert(toks[0].start == 0 && toks[0].end == 9); @@ -185,7 +185,7 @@ static void test_json_stream(void) * accidentally getting the boundaries to match. */ input = "{\"x\":\"x\"}{\"y\":\"y\"}{\"z\":\"z"; talstr = tal_strndup(NULL, input, strlen(input)); - toks = json_parse_input(talstr, strlen(talstr), &valid); + toks = json_parse_input(talstr, talstr, strlen(talstr), &valid); assert(toks); assert(tal_count(toks) == 4); assert(toks[0].start == 0 && toks[0].end == 9);