This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Stop regexp assignment from clobbering magic
authorFather Chrysostomos <sprout@cpan.org>
Sun, 28 Oct 2012 01:18:35 +0000 (18:18 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Sun, 28 Oct 2012 09:04:57 +0000 (02:04 -0700)
commit78a84e43f7c23daa5ea308f75bfa99ce0fd2a841
tree001ef7250e198ac14bd53a0ac879b3b830a5db11
parent12c45b2548283866d4ee5be5cea6c1cd072c3be9
Stop regexp assignment from clobbering magic

$ perl5.10.0 -le '$1 = ${qr||}; print "ok"'
Modification of a read-only value attempted at -e line 1.
$ perl5.12.0 -le '$1 = ${qr||}; print "ok"'
ok

Wonderful!

It can also cause blessings to be lost, or so I thought:

sub curse {
  for my $obj ( ${$_[0]} ) {
    my $save = $obj;
    $obj = ${qr||};
    $obj = $save;
  }
}
$y = bless \$x;
print $y, "\n"; # main=SCALAR(0x825b70)
curse $y;
print $y, "\n"; # Bus error

The OBJECT flag gets left on, but SvSTASH is null.

Commit b9ad13acb set SvSTASH to null after copying the regexp struct.
Commit 703c388dc did the same with SvMAGIC.  In both cases, this was
to avoid bugs involving magic and blessings being copied by = which
should not happen.  But both changes caused other bugs.

Three months later, 6e1287864cd changed the order of the struct, such
that SvMAGIC and SvSTASH are no longer copied from the parent regexp,
rendering the aforementioned changes no longer necessary.
regcomp.c
t/op/qr.t