Commit b883c96fa4 for perl
commit b883c96fa465105aece3f9460977d2a70ff0486a
Author: Paul Evans <leonerd@leonerd.org.uk>
Date: Thu Nov 12 20:25:24 2026 +0000
Remember to SvGETMAGIC for refaliased foreach values
The previous version when it was using PERL_MAGIC_lvref remembered to
SvGETMAGIC on each position, but this more direct logic forgot to do
that. There didn't seem to be a test for it previously, so that was
overlooked.
Fixes [GH#25056]
diff --git a/pp_hot.c b/pp_hot.c
index fb04903988..8c0383aae4 100644
--- a/pp_hot.c
+++ b/pp_hot.c
@@ -5202,6 +5202,8 @@ PP(pp_iter)
if (refalias_this) {
/* TODO(leonerd): pull out this behaviour into a shared place.
* This is mostly copypaste from mg.c's Perl_magic_setlvref */
+ if(sv)
+ SvGETMAGIC(sv);
if (!sv || !SvROK(sv)) croak("Assigned value is not a reference");
SV *rv = SvRV(sv);
diff --git a/t/op/lvref.t b/t/op/lvref.t
index 1435aa9f51..f31fa11d03 100644
--- a/t/op/lvref.t
+++ b/t/op/lvref.t
@@ -5,7 +5,7 @@ BEGIN {
set_up_inc("../lib");
}
-plan 203;
+plan 205;
eval '\$x = \$y';
like $@, qr/^Experimental aliasing via reference not enabled/,
@@ -768,3 +768,32 @@ pass("RT #123821");
is("$h{one} $h{two} $h{three}", "102 200 301", "rt #133538 %h still aliased");
}
+
+# GH#25056 refalias checks GETMAGIC
+{
+ package ElemsAreArrayrefs {
+ # Implements a tied array whose elements are all 1-element ARRAYrefs,
+ # containing their own index
+ sub TIEARRAY { bless [] }
+ sub FETCHSIZE { 5 }
+ sub FETCH { my ($self, $idx) = @_; return [$idx]; }
+ }
+
+ tie my @outer_a, "ElemsAreArrayrefs";
+
+ my $output;
+ foreach \my @inner_a (@outer_a) {
+ $output .= "|" if length $output;
+ $output .= "<@inner_a>";
+ }
+ is($output, "<0>|<1>|<2>|<3>|<4>",
+ 'foreach(ARRAY) on refalias invokes GETMAGIC on each element');
+
+ undef $output;
+ foreach \my @inner_a ($outer_a[0], $outer_a[1], $outer_a[2]) {
+ $output .= "|" if length $output;
+ $output .= "<@inner_a>";
+ }
+ is($output, "<0>|<1>|<2>",
+ 'foreach(LIST) on refalias invokes GETMAGIC on each element');
+}