From d0a55a62b36fc6f318ee6d4f5274747d7ecc091d Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 16 Jul 2022 14:28:58 +0930 Subject: [PATCH] common/json_stream: make json_add_jsonstr take a length. This is useful when have have a jsmntok_t. Signed-off-by: Rusty Russell --- common/json_stream.c | 8 ++++---- common/json_stream.h | 7 ++++--- lightningd/jsonrpc.c | 8 +++++--- plugins/spender/multifundchannel.c | 9 ++++++--- plugins/spender/multiwithdraw.c | 2 +- plugins/spender/openchannel.c | 4 +++- 6 files changed, 23 insertions(+), 15 deletions(-) diff --git a/common/json_stream.c b/common/json_stream.c index f55603352..53b374e24 100644 --- a/common/json_stream.c +++ b/common/json_stream.c @@ -199,13 +199,13 @@ static char *json_member_direct(struct json_stream *js, void json_add_jsonstr(struct json_stream *js, const char *fieldname, - const char *jsonstr) + const char *jsonstr, + size_t jsonstrlen) { char *p; - size_t len = strlen(jsonstr); - p = json_member_direct(js, fieldname, len); - memcpy(p, jsonstr, len); + p = json_member_direct(js, fieldname, jsonstrlen); + memcpy(p, jsonstr, jsonstrlen); } /* This is where we read the json_stream and write it to conn */ diff --git a/common/json_stream.h b/common/json_stream.h index 7743a29a8..316da33f4 100644 --- a/common/json_stream.h +++ b/common/json_stream.h @@ -157,12 +157,13 @@ void json_add_escaped_string(struct json_stream *result, * JSON-formatted. * @js: the json_stream. * @fieldname: fieldname (if in object), otherwise must be NULL. - * @jsonstr: the JSON entity, must be non-NULL, a null-terminated - * string that is already formatted in JSON. + * @jsonstr: the JSON entity + * @jsonstrlen: the length of @jsonstr */ void json_add_jsonstr(struct json_stream *js, const char *fieldname, - const char *jsonstr); + const char *jsonstr, + size_t jsonstrlen); /** * json_stream_output - start writing out a json_stream to this conn. diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 317346ca1..357297f2f 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -578,7 +578,7 @@ static struct json_stream *json_start(struct command *cmd) json_object_start(js, NULL); json_add_string(js, "jsonrpc", "2.0"); - json_add_jsonstr(js, "id", cmd->id); + json_add_jsonstr(js, "id", cmd->id, strlen(cmd->id)); return js; } @@ -742,13 +742,15 @@ static void rpc_command_hook_final(struct rpc_command_hook_payload *p STEALS) if (p->custom_result != NULL) { struct json_stream *s = json_start(p->cmd); - json_add_jsonstr(s, "result", p->custom_result); + json_add_jsonstr(s, "result", + p->custom_result, strlen(p->custom_result)); json_object_end(s); return was_pending(command_raw_complete(p->cmd, s)); } if (p->custom_error != NULL) { struct json_stream *s = json_start(p->cmd); - json_add_jsonstr(s, "error", p->custom_error); + json_add_jsonstr(s, "error", + p->custom_error, strlen(p->custom_error)); json_object_end(s); return was_pending(command_raw_complete(p->cmd, s)); } diff --git a/plugins/spender/multifundchannel.c b/plugins/spender/multifundchannel.c index b721d73c4..2c8a02ce5 100644 --- a/plugins/spender/multifundchannel.c +++ b/plugins/spender/multifundchannel.c @@ -499,7 +499,8 @@ multifundchannel_finished(struct multifundchannel_command *mfc) mfc->removeds[i].error_message); if (mfc->removeds[i].error_data) json_add_jsonstr(out, "data", - mfc->removeds[i].error_data); + mfc->removeds[i].error_data, + strlen(mfc->removeds[i].error_data)); json_object_end(out); /* End error object */ json_object_end(out); } @@ -1382,7 +1383,8 @@ perform_fundpsbt(struct multifundchannel_command *mfc, u32 feerate) &after_fundpsbt, &mfc_forward_error, mfc); - json_add_jsonstr(req->js, "utxos", mfc->utxos_str); + json_add_jsonstr(req->js, "utxos", + mfc->utxos_str, strlen(mfc->utxos_str)); json_add_bool(req->js, "reservedok", false); } else { plugin_log(mfc->cmd->plugin, LOG_DBG, @@ -1814,7 +1816,8 @@ post_cleanup_redo_multifundchannel(struct multifundchannel_redo *redo) json_add_string(out, "method", failing_method); if (mfc->removeds[i].error_data) json_add_jsonstr(out, "data", - mfc->removeds[i].error_data); + mfc->removeds[i].error_data, + strlen(mfc->removeds[i].error_data)); /* Close 'data'. */ json_object_end(out); diff --git a/plugins/spender/multiwithdraw.c b/plugins/spender/multiwithdraw.c index 4bd7605f0..9ab29bb9e 100644 --- a/plugins/spender/multiwithdraw.c +++ b/plugins/spender/multiwithdraw.c @@ -356,7 +356,7 @@ static struct command_result *start_mw(struct multiwithdraw_command *mw) &mw_forward_error, mw); json_add_bool(req->js, "reservedok", false); - json_add_jsonstr(req->js, "utxos", mw->utxos); + json_add_jsonstr(req->js, "utxos", mw->utxos, strlen(mw->utxos)); } else { plugin_log(mw->cmd->plugin, LOG_DBG, "multiwithdraw %"PRIu64": fundpsbt.", diff --git a/plugins/spender/openchannel.c b/plugins/spender/openchannel.c index 7973bd0be..0143c7070 100644 --- a/plugins/spender/openchannel.c +++ b/plugins/spender/openchannel.c @@ -346,7 +346,9 @@ openchannel_finished(struct multifundchannel_command *mfc) json_add_node_id(out, "id", &dest->id); json_add_string(out, "method", "openchannel_signed"); if (dest->error_data) - json_add_jsonstr(out, "data", dest->error_data); + json_add_jsonstr(out, "data", + dest->error_data, + strlen(dest->error_data)); json_object_end(out); return mfc_finished(mfc, out);