This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
op.c:newMYSUB: Pop scope after creating sub
authorFather Chrysostomos <sprout@cpan.org>
Tue, 10 Jul 2012 05:25:24 +0000 (22:25 -0700)
committerFather Chrysostomos <sprout@cpan.org>
Sun, 16 Sep 2012 05:45:02 +0000 (22:45 -0700)
commit2435fbd5b7d5a7aa6aaeba8f8b9ea14ff5878e88
treea0df4218ec9545868ceec13ca54ce2f6dee43617
parent3610c89f8ee6462cb194b0106d9bb4a362538267
op.c:newMYSUB: Pop scope after creating sub

I was popping the scope before creating the sub in order to expose the
parent pad, where the new sub is to be stored.

That can cause problems, since ops may still be created that get
attached to the new sub.  Those ops will end up using the parent sub’s
slab in that case.  If the parent sub does not finish compiling, due
to an error, it may clean out its slab, freeing ops that the inner sub
is using, so the inner sub, when freed, will try to free ops that are
no longer in allocated memory, as the slab is gone.  Most of the time,
the inner ops won’t have been reused for anything, so the op type will
still be OP_FREED, and op_free will do nothing (except a single bad
read).  But debugging builds detect that and fail an assertion.

Popping the scope afterwards actually does simplify things, surpris-
ingly enough.

I was able to produce this bug with a one-liner, but it did not fail
as part of the test suite.  So this fix includes no test.

Since the o variable in newMYSUB is a padop, it can only be freed when
its pad is active.  It is created before the sub, so it cannot be
freed until the scope has been popped, so it has to go at the bot-
tom.  If an error occurs during newMYSUB, opslab_force_free will take
care of it.
op.c