From 28340534571ab1b61fd0118dd6a9fc633a76e1ad Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Mon, 10 Dec 2018 15:27:37 +0100 Subject: [PATCH] plugin: Fix an issue with string IDs ending up quoted twice The transparent passthrough that was recently introduced would end up causing phantom quotes to appear around IDs when one of them was a string. This happened for example when using `lightning-cli`, the code would copy the quotes from the original request, insert our u64 ID, and then re-add them on the way back as well. Signed-off-by: Christian Decker --- lightningd/plugin.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lightningd/plugin.c b/lightningd/plugin.c index 91962aa3d..b0c6f7508 100644 --- a/lightningd/plugin.c +++ b/lightningd/plugin.c @@ -484,13 +484,18 @@ static void json_stream_forward_change_id(struct json_stream *stream, const jsmntok_t *idtok, const char *new_id) { - /* We copy everything, but replace the id (maybe inside a string, we - * don't care) */ + /* We copy everything, but replace the id. Special care has to + * be taken when the id that is being replaced is a string. If + * we don't crop the quotes off we'll transform a numeric + * new_id into a string, or even worse, quote a string id + * twice. */ + size_t offset = idtok->type==JSMN_STRING?1:0; json_stream_append_part(stream, buffer + toks->start, - idtok->start - toks->start); + idtok->start - toks->start - offset); + json_stream_append(stream, new_id); - json_stream_append_part(stream, buffer + idtok->end, - toks->end - idtok->end); + json_stream_append_part(stream, buffer + idtok->end + offset, + toks->end - idtok->end - offset); /* We promise it will end in '\n\n' */ /* It's an object (with an id!): definitely can't be less that "{}" */