Change 18072 by rgs@rgs-home on 2002/10/29 20:37:31
Partial fix of bug [perl #17589] : prevent the parser to segfault when encountering the erroneous construct "sub;". Affected files ... .... //depot/perl/pod/perldiag.pod#318 edit .... //depot/perl/t/op/anonsub.t#7 edit .... //depot/perl/toke.c#450 edit Differences ... ==== //depot/perl/pod/perldiag.pod#318 (text) ==== Index: perl/pod/perldiag.pod --- perl/pod/perldiag.pod#317~18026~ Thu Oct 17 07:29:41 2002 +++ perl/pod/perldiag.pod Tue Oct 29 12:37:31 2002 @@ -1714,6 +1714,11 @@ (W syntax) An illegal character was found in a prototype declaration. Legal characters in prototypes are $, @, %, *, ;, [, ], &, and \. +=item Illegal declaration of anonymous subroutine + +(F) When using the C<sub> keyword to construct an anonymous subroutine, +you must always specify a block of code. See L<perlsub>. + =item Illegal division by zero (F) You tried to divide a number by 0. Either something was wrong in ==== //depot/perl/t/op/anonsub.t#7 (xtext) ==== Index: perl/t/op/anonsub.t --- perl/t/op/anonsub.t#6~17942~ Sun Sep 29 15:26:37 2002 +++ perl/t/op/anonsub.t Tue Oct 29 12:37:31 2002 @@ -1,5 +1,9 @@ #!./perl +# Note : we're not using t/test.pl here, because we would need +# fresh_perl_is, and fresh_perl_is uses a closure -- a special +# case of what this program tests for. + chdir 't' if -d 't'; @INC = '../lib'; $Is_VMS = $^O eq 'VMS'; @@ -12,7 +16,7 @@ undef $/; @prgs = split "\n########\n", <DATA>; -print "1..", scalar @prgs, "\n"; +print "1..", 6 + scalar @prgs, "\n"; $tmpfile = "asubtmp000"; 1 while -f ++$tmpfile; @@ -49,6 +53,30 @@ print "not "; } print "ok ", ++$i, "\n"; +} + +sub test_invalid_decl { + my ($code,$todo) = @_; + $todo //= ''; + eval $code; + if ($@ =~ /^Illegal declaration of anonymous subroutine at/) { + print "ok ", ++$i, " - '$code' is illegal$todo\n"; + } else { + print "not ok ", ++$i, " - '$code' is illegal$todo\n# GOT: $@"; + } +} + +test_invalid_decl('sub;'); +test_invalid_decl('sub ($) ;'); +test_invalid_decl('{ $x = sub }'); +test_invalid_decl('sub ($) && 1'); +test_invalid_decl('sub ($) : lvalue;',' # TODO'); + +eval "sub #foo\n{print 1}"; +if ($@ eq '') { + print "ok ", ++$i, "\n"; +} else { + print "not ok ", ++$i, "\n# GOT: $@"; } __END__ ==== //depot/perl/toke.c#450 (text) ==== Index: perl/toke.c --- perl/toke.c#449~18058~ Thu Oct 24 16:36:48 2002 +++ perl/toke.c Tue Oct 29 12:37:31 2002 @@ -5057,6 +5057,8 @@ if (*s == ':' && s[1] != ':') PL_expect = attrful; + else if (!have_name && *s != '{' && key == KEY_sub) + Perl_croak(aTHX_ "Illegal declaration of anonymous subroutine"); if (have_proto) { PL_nextval[PL_nexttoke].opval = End of Patch.