Don’t cache qr magic on references
authorFather Chrysostomos <sprout@cpan.org>
Mon, 25 Nov 2013 07:10:42 +0000 (23:10 -0800)
committerFather Chrysostomos <sprout@cpan.org>
Mon, 25 Nov 2013 07:17:56 +0000 (23:17 -0800)
commite66ad3503cde2847fa749cda083ec75dea5c7343
tree3f5b8432389626f9afe1c7932c4fcf9bebabf190
parent2685dc2d9a9865ac92971890d053145da562c094
Don’t cache qr magic on references

When a scalar is returned from (??{...}) inside a regexp, it gets com-
piled into a regexp if it is not one already.  Then the regexp is sup-
posed to be cached on that scalar (in magic), so that the same scalar
returned again will not require another compilation.

This has never worked correctly with references, because the value was
being cached against the returned scalar itself, whereas the *refer-
ent* of a returned reference was being checked for qr magic.

Commit 636209429 (recent) attempted to fix the resulting bug, but
ended up exposing another, older bug, that e4bfbed39b (5.18) acciden-
tally (?) fixed.  The stringification of a reference can easily change
without the reference itself being touched.  So set-magic (which
clears the qr cache) is never triggered:

{ package o; use overload '""'=>sub{"abc"} }
$x = bless \$y, "o";
sub foo { warn "abc" =~ /(??{$x})/ }
foo();
bless \$y;
warn "$x";
foo();
__END__

Output:
1 at - line 3.
main=SCALAR(0x7fcbc3027478) at - line 6.
1 at - line 3.

Blessing \$y into main causes it to stringify as
main=SCALAR(0x7fcbc3027478).  So how can "abc" match
/main=SCALAR(0x7fcbc3027478)/?

Skipping the cache for references obviously fixes this.  The cache was
only being stored on refs to overloaded objects, which don’t use the
cache.  The only case in which is was being used was when the over-
loaded object was blessed into a non-overloaded class, and then it
was incorrect.
regexec.c
t/re/pat_re_eval.t