Skip to content

Commit 6e4bc1d

Browse files
committed
askrene: neated flow array handling, by freeing flows we discard.
Pointed out by @Lagrang3; he's right, while it's a temporary leak the way we use flows, it's still a trap. Signed-off-by: Rusty Russell <[email protected]>
1 parent 69adf38 commit 6e4bc1d

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

plugins/askrene/refine.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,13 @@ static struct amount_msat sum_all_deliver(struct flow **flows)
312312
return all_deliver;
313313
}
314314

315+
/* Remove and free the flow */
316+
static void del_flow_from_arr(struct flow ***flows, size_t i)
317+
{
318+
tal_free((*flows)[i]);
319+
tal_arr_remove(flows, i);
320+
}
321+
315322
/* It reduces the amount of the flows and/or removes some flows in order to
316323
* deliver no more than max_deliver. It will leave at least one flow.
317324
* Returns the total delivery amount. */
@@ -340,7 +347,7 @@ static struct amount_msat remove_excess(struct flow ***flows,
340347
if (!amount_msat_deduct(&all_deliver,
341348
(*flows)[i]->delivers))
342349
abort();
343-
tal_arr_remove(flows, i);
350+
del_flow_from_arr(flows, i);
344351
}
345352

346353
/* If we still have some excess, remove it from the
@@ -511,15 +518,15 @@ const char *refine_flows(const tal_t *ctx, struct route_query *rq,
511518
if (error_message)
512519
goto fail;
513520
/* htlc_min is not met for this flow */
514-
tal_arr_remove(flows, i);
521+
del_flow_from_arr(flows, i);
515522
}
516523

517524
/* remove 0 amount flows if any */
518525
asort(*flows, tal_count(*flows), revcmp_flows, NULL);
519526
for (int i = tal_count(*flows) - 1; i >= 0; i--) {
520527
if (!amount_msat_is_zero((*flows)[i]->delivers))
521528
break;
522-
tal_arr_remove(flows, i);
529+
del_flow_from_arr(flows, i);
523530
}
524531

525532
tal_free(working_ctx);
@@ -568,7 +575,7 @@ void squash_flows(const tal_t *ctx, struct route_query *rq,
568575
if (amount_msat_greater(combined, max))
569576
break;
570577
flow->delivers = combined;
571-
tal_arr_remove(flows, i+1);
578+
del_flow_from_arr(flows, i+1);
572579
}
573580
}
574581
}
@@ -619,7 +626,8 @@ const char *reduce_num_flows(const tal_t *ctx,
619626
*/
620627
size_t orig_num_flows = tal_count(*flows);
621628
asort(*flows, orig_num_flows, revcmp_flows, NULL);
622-
tal_resize(flows, num_parts);
629+
while (tal_count(*flows) > num_parts)
630+
del_flow_from_arr(flows, tal_count(*flows) - 1);
623631

624632
if (!increase_flows(rq, *flows, deliver, -1.0))
625633
return rq_log(ctx, rq, LOG_INFORM,

0 commit comments

Comments
 (0)