summaryrefslogtreecommitdiff
path: root/av.c
diff options
context:
space:
mode:
authorFather Chrysostomos <sprout@cpan.org>2012-01-06 23:36:38 -0800
committerFather Chrysostomos <sprout@cpan.org>2012-01-06 23:36:38 -0800
commit70ce9249c4e5e892ce6ec830baedb9e3aed67ded (patch)
tree302a9e575fbd47a6671efba850f96838a296248e /av.c
parent9f71cfe6ef2a57e26394d4caf1bf2894802f4777 (diff)
downloadperl-70ce9249c4e5e892ce6ec830baedb9e3aed67ded.tar.gz
[perl #85670] Copy magic to ary elems properly
On Tue Mar 08 07:26:35 2011, thospel wrote: > #!/usr/bin/perl -l > use Data::Dumper; > use Scalar::Util qw(weaken); > our @ISA; > > for (1..2) { > @ISA = qw(Foo); > weaken($a = \@ISA); > weaken($a = \$ISA[0]); > print STDERR Dumper(\@ISA); > } > > This prints: > $VAR1 = [ > 'Foo' > ]; > $VAR1 = [ > 'Foo', > \$VAR1->[0] > ]; > > So the first time it's the expected @ISA, but the second time round it > automagically added a reference to to the first ISA element > > (bug also exists in blead) Shorter: #!/usr/bin/perl -l use Scalar::Util qw(weaken); weaken($a = \@ISA); @ISA = qw(Foo); use Devel::Peek; Dump \@ISA; weaken($a = \$ISA[0]); print scalar @ISA; # prints 2 The dump shows the problem. backref magic is being copied to the ele- ment. Put the magic in a different order, and everything is fine: #!/usr/bin/perl -l use Scalar::Util qw(weaken); weaken($a = $b = []); *ISA = $a; @ISA = qw(Foo); use Devel::Peek; Dump \@ISA; weaken($a = \$ISA[0]); print scalar @ISA; # prints 2 This code in av_store is so wrong: if (SvSMAGICAL(av)) { const MAGIC* const mg = SvMAGIC(av); if (val != &PL_sv_undef) { sv_magic(val, MUTABLE_SV(av), toLOWER(mg->mg_type), 0, key); } if (PL_delaymagic && mg->mg_type == PERL_MAGIC_isa) PL_delaymagic |= DM_ARRAY_ISA; else mg_set(MUTABLE_SV(av)); } It doesn’t follow the magic chain at all. So anything magic could get attached to the @ISA array, and that will be copied to the element instead of isa magic. Notice that MUTABLE_SV(av) is the second argument to sv_magic, so mg->mg_obj for the element always points back to the array. Since backref magic’s mg->mg_obj points to the backrefs array, @ISA ends up being used as this element’s backrefs array. What if arylen_p gets copied instead? Let’s see: $#ISA = -1; @ISA = qw(Foo); $ISA[0] = "Bar"; main->ber; sub Bar::ber { warn "shave" } __END__ Can't locate object method "ber" via package "main" at - line 7. I’ve fixed this by making av_store walk the magic chain, copying any magic for which toLOWER(mg->mg_type) != mg->mg_type.
Diffstat (limited to 'av.c')
-rw-r--r--av.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/av.c b/av.c
index 733bbd4c5b..01b565f670 100644
--- a/av.c
+++ b/av.c
@@ -362,13 +362,20 @@ Perl_av_store(pTHX_ register AV *av, I32 key, SV *val)
SvREFCNT_dec(ary[key]);
ary[key] = val;
if (SvSMAGICAL(av)) {
- const MAGIC* const mg = SvMAGIC(av);
- if (val != &PL_sv_undef) {
+ const MAGIC *mg = SvMAGIC(av);
+ bool set = TRUE;
+ for (; mg; mg = mg->mg_moremagic) {
+ const int eletype = toLOWER(mg->mg_type);
+ if (eletype == mg->mg_type) continue;
+ if (val != &PL_sv_undef) {
sv_magic(val, MUTABLE_SV(av), toLOWER(mg->mg_type), 0, key);
- }
- if (PL_delaymagic && mg->mg_type == PERL_MAGIC_isa)
+ }
+ if (PL_delaymagic && mg->mg_type == PERL_MAGIC_isa) {
PL_delaymagic |= DM_ARRAY_ISA;
- else
+ set = FALSE;
+ }
+ }
+ if (set)
mg_set(MUTABLE_SV(av));
}
return &ary[key];