Patch for Object subroutines
Ilya Zakharevich [Tue, 7 Jan 1997 12:56:02 +0000 (07:56 -0500)]
The following script segfaults with _17 ():

#!./perl
my $x;

BEGIN {
  $x = sub {print "in sub.\n"; undef $x};
  sub X::DESTROY { print "Destroying.\n"}
  bless $x, 'X';
}

# At this moment refcount of $x and &$x are 1 (we need a closure for this
# because of a sub leak).

&$x();
print "x: `$x'.\n";

cop.h

diff --git a/cop.h b/cop.h
index 543c039..20301ff 100644 (file)
--- a/cop.h
+++ b/cop.h
@@ -46,6 +46,8 @@ struct block_sub {
        cx->blk_sub.dfoutgv = defoutgv;                                 \
        (void)SvREFCNT_inc(cx->blk_sub.dfoutgv)
 
+/* We muck with cxstack_ix since _dec may call a DESTROY, overwriting cx. */
+
 #define POPSUB(cx)                                                     \
        if (cx->blk_sub.hasargs) {   /* put back old @_ */              \
            GvAV(defgv) = cx->blk_sub.savearray;                        \
@@ -55,7 +57,9 @@ struct block_sub {
                if (cx->blk_sub.hasargs) {                              \
                    SvREFCNT_inc((SV*)cx->blk_sub.argarray);            \
                }                                                       \
+               cxstack_ix++;                                           \
                SvREFCNT_dec((SV*)cx->blk_sub.cv);                      \
+               cxstack_ix--;                                           \
            }                                                           \
        }