This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: Segmentation fault at undeclared for loop variable
authorRobin Houston <robin@cpan.org>
Tue, 13 Feb 2007 16:50:08 +0000 (16:50 +0000)
committerH.Merijn Brand <h.m.brand@xs4all.nl>
Tue, 13 Feb 2007 20:11:08 +0000 (20:11 +0000)
Message-Id: <C8B8E41C-C6FE-4822-918B-C62C916194DF@cpan.org>

p4raw-id: //depot/perl@30267

op.c

diff --git a/op.c b/op.c
index 92d0cfc..0191844 100644 (file)
--- a/op.c
+++ b/op.c
@@ -4521,7 +4521,15 @@ Perl_newFOROP(pTHX_ I32 flags, char *label, line_t forline, OP *sv, OP *expr, OP
            iterpflags = sv->op_private & OPpOUR_INTRO; /* for our $x () */
            sv->op_type = OP_RV2GV;
            sv->op_ppaddr = PL_ppaddr[OP_RV2GV];
-           if (cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv)
+
+           /* The op_type check is needed to prevent a possible segfault
+            * if the loop variable is undeclared and 'strict vars' is in
+            * effect. This is illegal but is nonetheless parsed, so we
+            * may reach this point with an OP_CONST where we're expecting
+            * an OP_GV.
+            */
+           if (cUNOPx(sv)->op_first->op_type == OP_GV
+            && cGVOPx_gv(cUNOPx(sv)->op_first) == PL_defgv)
                iterpflags |= OPpITER_DEF;
        }
        else if (sv->op_type == OP_PADSV) { /* private variable */