From c60ea5bcbb268f6cc89c90961a683c21edaf5df4 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Sat, 4 Feb 2023 16:32:36 +1030 Subject: [PATCH] plugins/chanbackup: make get_file_data take ctx. When you return an allocated pointer, you should always hand in the context you want it allocated from. This is more explicit, because it may really matter to the caller! This also folds some simple operations, and avoids doing too much variable assignment in the declarations themselves: some coding styles prohibit such initializers, but that's a bit exteme. Signed-off-by: Rusty Russell --- plugins/chanbackup.c | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/plugins/chanbackup.c b/plugins/chanbackup.c index 79a758dd7..8f8ea93ea 100644 --- a/plugins/chanbackup.c +++ b/plugins/chanbackup.c @@ -168,9 +168,9 @@ static void maybe_create_new_scb(struct plugin *p, rename("scb.tmp", FILENAME); } -static u8* get_file_data(struct plugin *p) +static u8 *get_file_data(const tal_t *ctx, struct plugin *p) { - u8 *scb = grab_file(tmpctx, "emergency.recover"); + u8 *scb = grab_file(ctx, FILENAME); if (!scb) { plugin_err(p, "Cannot read emergency.recover: %s", strerror(errno)); } else { @@ -183,7 +183,7 @@ static u8* get_file_data(struct plugin *p) /* Returns decrypted SCB in form of a u8 array */ static u8 *decrypt_scb(struct plugin *p) { - u8 *filedata = get_file_data(p); + u8 *filedata = get_file_data(tmpctx, p); crypto_secretstream_xchacha20poly1305_state crypto_state; @@ -430,10 +430,10 @@ static struct command_result *after_listpeers(struct command *cmd, size_t i; struct info *info = tal(cmd, struct info); bool is_connected; + u8 *serialise_scb; - u8 *scb = get_file_data(cmd->plugin); - - u8 *serialise_scb = towire_peer_storage(cmd, scb); + serialise_scb = towire_peer_storage(cmd, + get_file_data(tmpctx, cmd->plugin)); peers = json_get_member(buf, params, "peers"); @@ -525,12 +525,14 @@ static struct command_result *peer_connected(struct command *cmd, const char *buf, const jsmntok_t *params) { - struct node_id *node_id = tal(cmd, struct node_id); + struct node_id *node_id; struct out_req *req; - u8 *scb = get_file_data(cmd->plugin); - u8 *serialise_scb = towire_peer_storage(cmd, scb); + u8 *serialise_scb; const char *err; + serialise_scb = towire_peer_storage(cmd, + get_file_data(tmpctx, cmd->plugin)); + node_id = tal(cmd, struct node_id); err = json_scan(cmd, buf, params, "{peer:{id:%}}", JSON_SCAN(json_to_node_id, node_id));