From d8eacd2fd21ed5b6670333ad3a2d4c685634680f Mon Sep 17 00:00:00 2001 From: Jeff Davis Date: Wed, 17 May 2017 14:16:26 -0700 Subject: [PATCH] LP#1691563: Prevent "Use of freed value in iteration" error during adjust to zero Signed-off-by: Jeff Davis Signed-off-by: Mike Rylander --- .../src/perlmods/lib/OpenILS/Application/Circ/CircCommon.pm | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/CircCommon.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/CircCommon.pm index 167571243c..b22fee458f 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/CircCommon.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Circ/CircCommon.pm @@ -804,7 +804,11 @@ sub bill_payment_map_for_xact { } # Try to map payments to bills by amounts starting with the - # largest payments: + # largest payments. + # To avoid modifying the array we're iterating over (which can result in a + # "Use of freed value in iteration" error), we create a copy of the + # payments array and remove handled payments from that instead. + my @handled_payments = @$payments; foreach my $payment (sort {$b->amount() <=> $a->amount()} @$payments) { my @bills2pay = grep {$_->{bill}->amount() == $payment->amount()} @entries; if (@bills2pay) { @@ -812,10 +816,13 @@ sub bill_payment_map_for_xact { $entry->{bill}->amount(0); push @{$entry->{payments}}, $payment; # Remove the payment from the master list. - my @p = grep {$_->id() != $payment->id()} @$payments; - $payments = \@p; + my @p = grep {$_->id() != $payment->id()} @handled_payments; + @handled_payments = @p; } } + # Now, update our list of payments so that it only includes unhandled + # (unmapped) payments. + $payments = \@handled_payments; # Map remaining bills to payments in whatever order. foreach my $entry (grep {$_->{bill}->amount() > 0} @entries) { -- 2.11.0