From 7335777595bb94c81eee442c41cc4c51c71373ae Mon Sep 17 00:00:00 2001 From: Lagrang3 Date: Mon, 6 May 2024 16:14:10 +0100 Subject: [PATCH] renepay: fix some memory leaks --- plugins/renepay/main.c | 8 ++++---- plugins/renepay/mods.c | 10 +++++++++- plugins/renepay/payment.c | 5 ++++- plugins/renepay/payment.h | 2 +- plugins/renepay/routetracker.c | 2 +- plugins/renepay/uncertainty.c | 1 + 6 files changed, 20 insertions(+), 8 deletions(-) diff --git a/plugins/renepay/main.c b/plugins/renepay/main.c index bb18dc29f..b7e83cdf3 100644 --- a/plugins/renepay/main.c +++ b/plugins/renepay/main.c @@ -29,10 +29,10 @@ struct pay_plugin *pay_plugin; static void memleak_mark(struct plugin *p, struct htable *memtable) { memleak_scan_obj(memtable, pay_plugin); - - // TODO is this necessary? - // memleak_scan_htable(memtable, &pay_plugin->chan_extra_map->raw); - // memleak_scan_htable(memtable, &pay_plugin->payment_map->raw); + memleak_scan_htable(memtable, + &pay_plugin->uncertainty->chan_extra_map->raw); + memleak_scan_htable(memtable, &pay_plugin->payment_map->raw); + memleak_scan_htable(memtable, &pay_plugin->route_map->raw); } static const char *init(struct plugin *p, diff --git a/plugins/renepay/mods.c b/plugins/renepay/mods.c index 5f5423559..1520c0597 100644 --- a/plugins/renepay/mods.c +++ b/plugins/renepay/mods.c @@ -335,6 +335,7 @@ static struct command_result *selfpay_success(struct command *cmd, const jsmntok_t *tok, struct route *route) { + tal_steal(tmpctx, route); // discard this route when tmpctx clears struct payment *payment = payment_map_get(pay_plugin->payment_map, route->key.payment_hash); assert(payment); @@ -357,6 +358,7 @@ static struct command_result *selfpay_failure(struct command *cmd, const jsmntok_t *tok, struct route *route) { + tal_steal(tmpctx, route); // discard this route when tmpctx clears struct payment *payment = payment_map_get(pay_plugin->payment_map, route->key.payment_hash); assert(payment); @@ -382,6 +384,8 @@ static struct command_result *selfpay_cb(struct payment *payment) "Selfpay: cannot get a valid cmd."); struct payment_info *pinfo = &payment->payment_info; + /* Self-payment routes are not part of the routetracker, we build them + * on-the-fly here and release them on success or failure. */ struct route *route = new_route(payment, payment->groupid, /*partid=*/0, pinfo->payment_hash, @@ -748,7 +752,7 @@ compute_routes_done(struct command *cmd UNUSED, const char *buf UNUSED, // in the local gossmap. enum jsonrpc_errcode errcode; - const char *err_msg; + const char *err_msg = NULL; gossmap_apply_localmods(pay_plugin->gossmap, payment->local_gossmods); // TODO: add an algorithm selector here @@ -775,13 +779,17 @@ compute_routes_done(struct command *cmd UNUSED, const char *buf UNUSED, &errcode, &err_msg); + err_msg = tal_steal(tmpctx, err_msg); gossmap_remove_localmods(pay_plugin->gossmap, payment->local_gossmods); /* Couldn't feasible route, we stop. */ if (!payment->routes_computed) { + if(err_msg==NULL) + err_msg = tal_fmt(tmpctx, "get_routes returned NULL error message"); return payment_fail(payment, errcode, "%s", err_msg); } + return payment_continue(payment); } diff --git a/plugins/renepay/payment.c b/plugins/renepay/payment.c index b06ce44de..6be264f54 100644 --- a/plugins/renepay/payment.c +++ b/plugins/renepay/payment.c @@ -280,7 +280,10 @@ struct command_result *payment_success(struct payment *payment, assert(preimage); payment->status = PAYMENT_SUCCESS; payment->preimage = tal_free(payment->preimage); - payment->preimage = tal_dup(payment, struct preimage, preimage); + if(taken(preimage)) + payment->preimage = tal_steal(payment, preimage); + else + payment->preimage = tal_dup(payment, struct preimage, preimage); return payment_finish(payment); } diff --git a/plugins/renepay/payment.h b/plugins/renepay/payment.h index 3d76aa93d..8c6ac6ba0 100644 --- a/plugins/renepay/payment.h +++ b/plugins/renepay/payment.h @@ -27,7 +27,7 @@ struct payment { enum payment_status status; /* Payment preimage, in case of success. */ - struct preimage *preimage; + const struct preimage *preimage; /* Final error code and message, in case of failure. */ enum jsonrpc_errcode error_code; diff --git a/plugins/renepay/routetracker.c b/plugins/renepay/routetracker.c index 165654a3c..4dc5fded8 100644 --- a/plugins/renepay/routetracker.c +++ b/plugins/renepay/routetracker.c @@ -238,7 +238,7 @@ void payment_collect_results(struct payment *payment, if (r->result->status == SENDPAY_COMPLETE && payment_preimage) { assert(r->result->payment_preimage); *payment_preimage = - tal_dup(payment, struct preimage, + tal_dup(tmpctx, struct preimage, r->result->payment_preimage); } diff --git a/plugins/renepay/uncertainty.c b/plugins/renepay/uncertainty.c index 37529431a..c2c437970 100644 --- a/plugins/renepay/uncertainty.c +++ b/plugins/renepay/uncertainty.c @@ -89,6 +89,7 @@ int uncertainty_update(struct uncertainty *uncertainty, struct gossmap *gossmap) } for(size_t i=0;i