Browse Source

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 <rusty@rustcorp.com.au>
plugin-6
Rusty Russell 6 years ago
parent
commit
86c517ac9b
  1. 5
      common/json.c
  2. 3
      common/json.h
  3. 2
      common/test/run-json_remove.c
  4. 8
      lightningd/bitcoind.c
  5. 2
      lightningd/jsonrpc.c
  6. 3
      lightningd/plugin.c
  7. 6
      lightningd/test/run-jsonrpc.c

5
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);

3
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);

2
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;

8
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 */

2
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,

3
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'",

6
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);

Loading…
Cancel
Save