Skip to content

Commit

Permalink
plugins/chanbackup: make get_file_data take ctx.
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
rustyrussell authored and endothermicdev committed Feb 8, 2023
1 parent 17c3581 commit c60ea5b
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions plugins/chanbackup.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;

Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit c60ea5b

Please sign in to comment.