From 919d371fe81a8a53c7d893419bef9256b73b41dd Mon Sep 17 00:00:00 2001 From: ZmnSCPxj jxPCSnmZ Date: Wed, 8 Apr 2020 02:01:36 +0800 Subject: [PATCH] common/json_stream.c: Implement a `json_add_jsonstr` to add already-JSON strings to `json_stream` objects. Also incidentally fixes #3613 ChangeLog-none --- common/json_stream.c | 12 ++++++++++++ common/json_stream.h | 12 ++++++++++++ plugins/fundchannel.c | 14 +------------- plugins/pay.c | 22 ++++++++++++---------- 4 files changed, 37 insertions(+), 23 deletions(-) diff --git a/common/json_stream.c b/common/json_stream.c index 582f3a2b8..6e5d96c7e 100644 --- a/common/json_stream.c +++ b/common/json_stream.c @@ -162,6 +162,18 @@ void json_add_member(struct json_stream *js, va_end(ap); } +void json_add_jsonstr(struct json_stream *js, + const char *fieldname, + const char *jsonstr) +{ + char *p; + size_t len = strlen(jsonstr); + + p = json_member_direct(js, fieldname, len); + + memcpy(p, jsonstr, len); +} + /* This is where we read the json_stream and write it to conn */ static struct io_plan *json_stream_output_write(struct io_conn *conn, struct json_stream *js) diff --git a/common/json_stream.h b/common/json_stream.h index 902f1a0dc..0446d9fb4 100644 --- a/common/json_stream.h +++ b/common/json_stream.h @@ -114,6 +114,18 @@ void json_add_member(struct json_stream *js, bool quote, const char *fmt, ...) PRINTF_FMT(4,5); +/** + * json_add_jsonstr - add a JSON entity in a string that is already + * 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. + */ +void json_add_jsonstr(struct json_stream *js, + const char *fieldname, + const char *jsonstr); + /** * json_member_direct - start a generic member. * @js: the json_stream. diff --git a/plugins/fundchannel.c b/plugins/fundchannel.c index 39a4b0215..9bf570613 100644 --- a/plugins/fundchannel.c +++ b/plugins/fundchannel.c @@ -44,17 +44,6 @@ struct funding_req { const char *error; }; -/* Helper to copy JSON object directly into a json_out */ -static void json_out_add_raw_len(struct json_out *jout, - const char *fieldname, - const char *jsonstr, size_t len) -{ - char *p; - - p = json_out_member_direct(jout, fieldname, len); - memcpy(p, jsonstr, len); -} - static struct command_result *send_prior(struct command *cmd, const char *buf, const jsmntok_t *error, @@ -220,8 +209,7 @@ static void txprepare(struct json_stream *js, if (fr->minconf) json_add_u32(js, "minconf", *fr->minconf); if (fr->utxo_str) - json_out_add_raw_len(js->jout, "utxos", fr->utxo_str, - strlen(fr->utxo_str)); + json_add_jsonstr(js, "utxos", fr->utxo_str); } static struct command_result *prepare_actual(struct command *cmd, diff --git a/plugins/pay.c b/plugins/pay.c index 8d15edd54..deee3ec8b 100644 --- a/plugins/pay.c +++ b/plugins/pay.c @@ -147,13 +147,6 @@ static void json_out_add_raw_len(struct json_out *jout, memcpy(p, jsonstr, len); } -static void json_out_add_raw(struct json_out *jout, - const char *fieldname, - const char *jsonstr) -{ - json_out_add_raw_len(jout, fieldname, jsonstr, strlen(jsonstr)); -} - static struct json_out *failed_start(struct pay_command *pc) { struct pay_attempt *attempt = current_attempt(pc); @@ -179,6 +172,15 @@ static const jsmntok_t *copy_member(struct json_out *ret, if (!m) return NULL; + /* FIXME: The fact it is a string is probably the wrong thing + * to handle: if it *is* a string we should probably copy + * the quote marks, but json_tok_full/json_tok_full_len + * specifically remove those. + * It works *now* because it is only used in "code" and + * "data": "code" is always numeric, and "data" is usually + * a JSON object/key-value table, but pure stromgs will + * probably result in invalid JSON. + */ /* Literal copy: it's already JSON escaped, and may be a string. */ json_out_add_raw_len(ret, membername, json_tok_full(buf, m), json_tok_full_len(m)); @@ -262,7 +264,7 @@ static struct command_result *waitsendpay_expired(struct command *cmd, for (size_t i = 0; i < tal_count(pc->ps->attempts); i++) { json_object_start(data, NULL); if (pc->ps->attempts[i].route) - json_add_member(data, "route", true, "%s", + json_add_jsonstr(data, "route", pc->ps->attempts[i].route); json_out_add_splice(data->jout, "failure", pc->ps->attempts[i].failure); @@ -835,7 +837,7 @@ static struct command_result *getroute_done(struct command *cmd, attempt->sendpay = true; req = jsonrpc_request_start(cmd->plugin, cmd, "sendpay", sendpay_done, sendpay_error, pc); - json_out_add_raw(req->js->jout, "route", attempt->route); + json_add_jsonstr(req->js, "route", attempt->route); json_add_string(req->js, "payment_hash", pc->payment_hash); json_add_string(req->js, "bolt11", pc->ps->bolt11); if (pc->label) @@ -1420,7 +1422,7 @@ static void add_attempt(struct json_stream *ret, } if (attempt->route) - json_add_member(ret, "route", true, "%s", attempt->route); + json_add_jsonstr(ret, "route", attempt->route); if (attempt->failure) json_out_add_splice(ret->jout, "failure", attempt->failure);