From fd04fca7710ff89d074e70654b3a43d334552c5c Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 13 Feb 2018 09:24:29 +1030 Subject: [PATCH] jsonrpc: make lifetimes in error path clearer. Saving one allocation isn't worth the confusion. Reported-by: @ZmnSCPxj Signed-off-by: Rusty Russell --- lightningd/jsonrpc.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index fe80ed038..2990d1c44 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -265,18 +265,19 @@ static void connection_complete_error(struct json_connection *jcon, int code, const struct json_result *data) { + const tal_t *tmpctx = tal_tmpctx(jcon); /* Use this to escape errmsg. */ - struct json_result *errorres = new_json_result(jcon); + struct json_result *errorres = new_json_result(tmpctx); const char *data_str; json_add_string_escape(errorres, NULL, errmsg); if (data) - data_str = tal_fmt(errorres, ", \"data\" : %s", + data_str = tal_fmt(tmpctx, ", \"data\" : %s", json_result_string(data)); else data_str = ""; - json_done(jcon, take(tal_fmt(errorres, + json_done(jcon, take(tal_fmt(tmpctx, "{ \"jsonrpc\": \"2.0\", " " \"error\" : " "{ \"code\" : %d," @@ -286,7 +287,7 @@ static void connection_complete_error(struct json_connection *jcon, json_result_string(errorres), data_str, id))); - tal_free(errorres); + tal_free(tmpctx); } struct json_result *null_response(const tal_t *ctx)