From 80b2e1e29875c974203a3087c69adb24e4be1480 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Thu, 20 Aug 2020 09:46:55 +0930 Subject: [PATCH] common: add simple json parse wrapper for the complete cases. We're going to change the API on the more complete JSON parser, so make and use a simple API for the easy cases. Signed-off-by: Rusty Russell --- common/json.c | 11 +++++++++++ common/json.h | 4 ++++ common/test/run-json.c | 13 ++++++------- common/test/run-json_remove.c | 5 ++--- devtools/onion.c | 5 ++--- plugins/libplugin.c | 18 ++++++------------ 6 files changed, 31 insertions(+), 25 deletions(-) diff --git a/common/json.c b/common/json.c index e21038efc..d16cc811f 100644 --- a/common/json.c +++ b/common/json.c @@ -585,6 +585,17 @@ again: return toks; } +jsmntok_t *json_parse_simple(const tal_t *ctx, const char *input, int len) +{ + bool valid; + jsmntok_t *toks; + + toks = json_parse_input(ctx, input, len, &valid); + if (toks && !valid) + toks = tal_free(toks); + return toks; +} + const char *jsmntype_to_string(jsmntype_t t) { switch (t) { diff --git a/common/json.h b/common/json.h index a051cf2ed..7a7d98e25 100644 --- a/common/json.h +++ b/common/json.h @@ -89,6 +89,10 @@ const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index); jsmntok_t *json_parse_input(const tal_t *ctx, const char *input, int len, bool *valid); +/* Simplified version of above which parses only a complete, valid + * JSON string */ +jsmntok_t *json_parse_simple(const tal_t *ctx, const char *input, int len); + /* 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.c b/common/test/run-json.c index adb88610b..ff05bbf2f 100644 --- a/common/test/run-json.c +++ b/common/test/run-json.c @@ -144,19 +144,18 @@ static void test_json_tok_size(void) /* This should *not* parse! (used to give toks[0]->size == 2!) */ buf = "{ 'satoshi', '546' }"; - toks = json_parse_input(tmpctx, buf, strlen(buf), &ok); - assert(!ok); + toks = json_parse_simple(tmpctx, buf, strlen(buf)); + assert(!toks); } static void test_json_delve(void) { const jsmntok_t *toks, *t; char *buf; - bool ok; buf = "{\"1\":\"one\", \"2\":\"two\", \"3\":[\"three\", {\"deeper\": 17}]}"; - toks = json_parse_input(tmpctx, buf, strlen(buf), &ok); - assert(ok); + toks = json_parse_simple(tmpctx, buf, strlen(buf)); + assert(toks); assert(toks->size == 3); t = json_delve(buf, toks, ".1"); @@ -227,8 +226,8 @@ static void test_json_delve(void) " }\n" "}\n" "\n"; - toks = json_parse_input(tmpctx, buf, strlen(buf), &ok); - assert(ok); + toks = json_parse_simple(tmpctx, buf, strlen(buf)); + assert(toks); assert(toks->size == 4); t = json_delve(buf, toks, ".rpcfile"); diff --git a/common/test/run-json_remove.c b/common/test/run-json_remove.c index cb36d56ee..2408cf7fc 100644 --- a/common/test/run-json_remove.c +++ b/common/test/run-json_remove.c @@ -148,9 +148,8 @@ static struct json *json_parse(const tal_t * ctx, const char *str) struct json *j = tal(ctx, struct json); j->buffer = tal_strdup(j, str); convert_quotes(j->buffer); - bool ok; - j->toks = json_parse_input(j, j->buffer, strlen(j->buffer), &ok); - assert(ok); + j->toks = json_parse_simple(j, j->buffer, strlen(j->buffer)); + assert(j->toks); j->toks = json_tok_copy(j, j->toks); return j; } diff --git a/devtools/onion.c b/devtools/onion.c index b3d242f5c..cad17c62f 100644 --- a/devtools/onion.c +++ b/devtools/onion.c @@ -204,7 +204,6 @@ static char *opt_set_node_id(const char *arg, struct node_id *node_id) static void runtest(const char *filename) { const tal_t *ctx = tal(NULL, u8); - bool valid; char *buffer = grab_file(ctx, filename); const jsmntok_t *toks, *session_key_tok, *associated_data_tok, *gentok, *hopstok, *hop, *payloadtok, *pubkeytok, *typetok, *oniontok, *decodetok; @@ -217,8 +216,8 @@ static void runtest(const char *filename) struct route_step *step; char *hexprivkey; - toks = json_parse_input(ctx, buffer, strlen(buffer), &valid); - if (!valid) + toks = json_parse_simple(ctx, buffer, strlen(buffer)); + if (!toks) errx(1, "File is not a valid JSON file."); gentok = json_get_member(buffer, toks, "generate"); diff --git a/plugins/libplugin.c b/plugins/libplugin.c index fedcec319..1fef9c6bf 100644 --- a/plugins/libplugin.c +++ b/plugins/libplugin.c @@ -442,12 +442,12 @@ static const jsmntok_t *read_rpc_reply(const tal_t *ctx, int *reqlen) { const jsmntok_t *toks; - bool valid; *reqlen = read_json_from_rpc(plugin); - toks = json_parse_input(ctx, membuf_elems(&plugin->rpc_conn->mb), *reqlen, &valid); - if (!valid) + toks = json_parse_simple(ctx, + membuf_elems(&plugin->rpc_conn->mb), *reqlen); + if (!toks) plugin_err(plugin, "Malformed JSON reply '%.*s'", *reqlen, membuf_elems(&plugin->rpc_conn->mb)); @@ -648,7 +648,6 @@ static void rpc_conn_finished(struct io_conn *conn, static bool rpc_read_response_one(struct plugin *plugin) { - bool valid; const jsmntok_t *toks, *jrtok; /* For our convenience, lightningd always ends JSON requests with @@ -656,15 +655,10 @@ static bool rpc_read_response_one(struct plugin *plugin) if (!memmem(plugin->rpc_buffer, plugin->rpc_used, "\n\n", 2)) return false; - toks = json_parse_input(NULL, plugin->rpc_buffer, plugin->rpc_used, - &valid); + toks = json_parse_simple(NULL, plugin->rpc_buffer, plugin->rpc_used); if (!toks) { - if (!valid) { - plugin_err(plugin, "Failed to parse RPC JSON response '%.*s'", - (int)plugin->rpc_used, plugin->rpc_buffer); - return false; - } - /* We need more. */ + plugin_err(plugin, "Failed to parse RPC JSON response '%.*s'", + (int)plugin->rpc_used, plugin->rpc_buffer); return false; }