From da0b651803f47b3e45eb5be55d728d5e6398e298 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Wed, 3 Aug 2022 16:56:55 +0200 Subject: [PATCH] pay: Use safe list traversal when completing suspended payments When traversing the list we call `command_finished` which modifies the list we are traversing. This ensures we don't end up advancing in the list iteration. Reported-by: Rusty Russell <@rustyrussell> --- plugins/pay.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/plugins/pay.c b/plugins/pay.c index 3c25c7063..ed0994182 100644 --- a/plugins/pay.c +++ b/plugins/pay.c @@ -574,7 +574,7 @@ static const char *init(struct plugin *p, static void on_payment_success(struct payment *payment) { - struct payment *p; + struct payment *p, *nxt; struct payment_tree_result result = payment_collect_result(payment); struct json_stream *ret; struct command *cmd; @@ -585,7 +585,7 @@ static void on_payment_success(struct payment *payment) /* Iterate through any pending payments we suspended and * terminate them. */ - list_for_each(&payments, p, list) { + list_for_each_safe(&payments, p, nxt, list) { /* The result for the active payment is returned in * `payment_finished`. */ if (payment == p) @@ -672,9 +672,9 @@ static void payment_json_add_attempts(struct json_stream *s, static void on_payment_failure(struct payment *payment) { - struct payment *p; + struct payment *p, *nxt; struct payment_tree_result result = payment_collect_result(payment); - list_for_each(&payments, p, list) + list_for_each_safe(&payments, p, nxt, list) { struct json_stream *ret; struct command *cmd;