This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Always copy return values when exiting scope
authorDavid Mitchell <davem@iabyn.com>
Wed, 21 Oct 2015 12:10:44 +0000 (13:10 +0100)
committerDavid Mitchell <davem@iabyn.com>
Wed, 3 Feb 2016 09:18:32 +0000 (09:18 +0000)
commit5e267fb87fe17363909b2ed49f916dccf9939c3b
tree748a68b9d0ccd5068579bb668bc8d78af18ddf43
parenta45346a418e0832215edf55c717fee24b0045c52
Always copy return values when exiting scope

v5.14.0-642-g3ed94dc fixed certain instances where returning from a sub
incorrectly returned the actual value rather than a copy, e.g.

    sub f { delete $foo{bar} }

This was because if the value(s) being returned had SvTEMP set, copying
was skipped. That commit added an extra condition to the skip test,
SvREFCNT(sv) == 1.

However, this applies equally well to other scope exits, for example

    do { ...; delete $foo{bar} }

So this commits adds the RC==1 test to S_leave_common() too, which handles
all the non-sub scope exits. As well as adding a test to do.t, it adds an
additional test to sub.t, since the original tests, although they
*detected* a non-copied return, didn't actually demonstrate a case where
it was actually harmful.

Note that S_leave_common() also sometimes skips on PADTMPs as well as
TEMPs, so this commit as a side-effect also makes it copy PADTMPs unless
their RC ==1. But since their RC should in fact almost always be 1 anyway,
in practice it makes no difference.
pp_ctl.c
t/op/do.t
t/op/sub.t