STRLEN != int.
[p5sagit/p5-mst-13.2.git] / t / run / kill_perl.t
1 #!./perl
2
3 # This is for tests that will normally cause segfaults, and other nasty
4 # errors that might kill the interpreter and for some reason you can't
5 # use an eval().
6 #
7 # New tests are added to the bottom.  For example.
8 #
9 #       ######## perlbug ID 20020831.001
10 #       ($a, b) = (1,2)
11 #       EXPECT
12 #       Can't modify constant item in list assignment - at line 1
13 #
14 # to test that the code "($a, b) = (1,2)" causes the appropriate syntax
15 # error, rather than just segfaulting as reported in perlbug ID
16 # 20020831.001
17 #
18 #
19 # NOTE: Please don't add tests to this file unless they *need* to be
20 # run in separate executable and can't simply use eval.
21
22 BEGIN {
23     chdir 't' if -d 't';
24     @INC = '../lib';
25 }
26
27 use strict;
28
29 $|=1;
30
31 my @prgs = ();
32 while(<DATA>) { 
33     if(m/^#{8,}\s*(.*)/) { 
34         push @prgs, ['', $1];
35     }
36     else { 
37         $prgs[-1][0] .= $_;
38     }
39 }
40 print "1..", scalar @prgs, "\n";
41
42 my $tmpfile = "misctmp000";
43 1 while -f ++$tmpfile;
44 END { while($tmpfile && unlink $tmpfile){} }
45
46 my $test = 1;
47 foreach my $prog (@prgs) {
48     my($raw_prog, $name) = @$prog;
49
50     my $switch;
51     if ($raw_prog =~ s/^\s*(-\w.*)//){
52         $switch = $1;
53     }
54
55     my($prog,$expected) = split(/\nEXPECT\n/, $raw_prog);
56
57     open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
58
59     # VMS adjustments
60     if( $^O eq 'VMS' ) {
61         $prog =~ s#/dev/null#NL:#;
62
63         # VMS file locking 
64         $prog =~ s{if \(-e _ and -f _ and -r _\)}
65                   {if (-e _ and -f _)}
66     }
67
68     print TEST $prog, "\n";
69     close TEST or die "Cannot close $tmpfile: $!";
70
71     my $results;
72     if ($^O eq 'MSWin32') {
73         $results = `.\\perl -I../lib $switch $tmpfile 2>&1`;
74     }
75     elsif ($^O eq 'NetWare') {
76         $results = `perl -I../lib $switch $tmpfile 2>&1`;
77     }
78     elsif ($^O eq 'MacOS') {
79         $results = `$^X -I::lib -MMac::err=unix $switch $tmpfile`;
80     }
81     else {
82       $results = `./perl "-I../lib" $switch $tmpfile 2>&1`;
83     }
84     my $status = $?;
85
86     # Clean up the results into something a bit more predictable.
87     $results =~ s/\n+$//;
88     $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
89     $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
90
91     # bison says 'parse error' instead of 'syntax error',
92     # various yaccs may or may not capitalize 'syntax'.
93     $results =~ s/^(syntax|parse) error/syntax error/mig;
94
95     if ($^O eq 'VMS') {
96         # some tests will trigger VMS messages that won't be expected
97         $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
98
99         # pipes double these sometimes
100         $results =~ s/\n\n/\n/g;
101     }
102
103     $expected =~ s/\n+$//;
104     my $ok = $results eq $expected;
105
106     unless( $ok ) {
107         print STDERR "# PROG: $switch\n$prog\n";
108         print STDERR "# EXPECTED:\n$expected\n";
109         print STDERR "# GOT:\n$results\n";
110     }
111     printf "%sok %d%s\n", ($ok ? '' : "not "), $test, 
112                           length $name ? " - $name" : $name;
113     $test++;
114 }
115
116 __END__
117 ########
118 $a = ":="; split /($a)/o, "a:=b:=c"; print "@_"
119 EXPECT
120 a := b := c
121 ########
122 $cusp = ~0 ^ (~0 >> 1);
123 use integer;
124 $, = " ";
125 print +($cusp - 1) % 8, $cusp % 8, -$cusp % 8, 8 | (($cusp + 1) % 8 + 7), "!\n";
126 EXPECT
127 7 0 0 8 !
128 ########
129 $foo=undef; $foo->go;
130 EXPECT
131 Can't call method "go" on an undefined value at - line 1.
132 ########
133 BEGIN
134         {
135             "foo";
136         }
137 ########
138 $array[128]=1
139 ########
140 $x=0x0eabcd; print $x->ref;
141 EXPECT
142 Can't call method "ref" without a package or object reference at - line 1.
143 ########
144 chop ($str .= <DATA>);
145 ########
146 close ($banana);
147 ########
148 $x=2;$y=3;$x<$y ? $x : $y += 23;print $x;
149 EXPECT
150 25
151 ########
152 eval {sub bar {print "In bar";}}
153 ########
154 system './perl -ne "print if eof" /dev/null'
155 ########
156 chop($file = <DATA>);
157 ########
158 package N;
159 sub new {my ($obj,$n)=@_; bless \$n}  
160 $aa=new N 1;
161 $aa=12345;
162 print $aa;
163 EXPECT
164 12345
165 ########
166 %@x=0;
167 EXPECT
168 Can't modify hash dereference in repeat (x) at - line 1, near "0;"
169 Execution of - aborted due to compilation errors.
170 ########
171 $_="foo";
172 printf(STDOUT "%s\n", $_);
173 EXPECT
174 foo
175 ########
176 push(@a, 1, 2, 3,)
177 ########
178 quotemeta ""
179 ########
180 for ("ABCDE") {
181  &sub;
182 s/./&sub($&)/eg;
183 print;}
184 sub sub {local($_) = @_;
185 $_ x 4;}
186 EXPECT
187 Modification of a read-only value attempted at - line 3.
188 ########
189 package FOO;sub new {bless {FOO => BAR}};
190 package main;
191 use strict vars;   
192 my $self = new FOO;
193 print $$self{FOO};
194 EXPECT
195 BAR
196 ########
197 $_="foo";
198 s/.{1}//s;
199 print;
200 EXPECT
201 oo
202 ########
203 print scalar ("foo","bar")
204 EXPECT
205 bar
206 ########
207 sub by_number { $a <=> $b; };# inline function for sort below
208 $as_ary{0}="a0";
209 @ordered_array=sort by_number keys(%as_ary);
210 ########
211 sub NewShell
212 {
213   local($Host) = @_;
214   my($m2) = $#Shells++;
215   $Shells[$m2]{HOST} = $Host;
216   return $m2;
217 }
218  
219 sub ShowShell
220 {
221   local($i) = @_;
222 }
223  
224 &ShowShell(&NewShell(beach,Work,"+0+0"));
225 &ShowShell(&NewShell(beach,Work,"+0+0"));
226 &ShowShell(&NewShell(beach,Work,"+0+0"));
227 ########
228    {
229        package FAKEARRAY;
230    
231        sub TIEARRAY
232        { print "TIEARRAY @_\n"; 
233          die "bomb out\n" unless $count ++ ;
234          bless ['foo'] 
235        }
236        sub FETCH { print "fetch @_\n"; $_[0]->[$_[1]] }
237        sub STORE { print "store @_\n"; $_[0]->[$_[1]] = $_[2] }
238        sub DESTROY { print "DESTROY \n"; undef @{$_[0]}; }
239    }
240    
241 eval 'tie @h, FAKEARRAY, fred' ;
242 tie @h, FAKEARRAY, fred ;
243 EXPECT
244 TIEARRAY FAKEARRAY fred
245 TIEARRAY FAKEARRAY fred
246 DESTROY 
247 ########
248 BEGIN { die "phooey\n" }
249 EXPECT
250 phooey
251 BEGIN failed--compilation aborted at - line 1.
252 ########
253 BEGIN { 1/$zero }
254 EXPECT
255 Illegal division by zero at - line 1.
256 BEGIN failed--compilation aborted at - line 1.
257 ########
258 BEGIN { undef = 0 }
259 EXPECT
260 Modification of a read-only value attempted at - line 1.
261 BEGIN failed--compilation aborted at - line 1.
262 ########
263 {
264     package foo;
265     sub PRINT {
266         shift;
267         print join(' ', reverse @_)."\n";
268     }
269     sub PRINTF {
270         shift;
271           my $fmt = shift;
272         print sprintf($fmt, @_)."\n";
273     }
274     sub TIEHANDLE {
275         bless {}, shift;
276     }
277     sub READLINE {
278         "Out of inspiration";
279     }
280     sub DESTROY {
281         print "and destroyed as well\n";
282   }
283   sub READ {
284       shift;
285       print STDOUT "foo->can(READ)(@_)\n";
286       return 100; 
287   }
288   sub GETC {
289       shift;
290       print STDOUT "Don't GETC, Get Perl\n";
291       return "a"; 
292   }    
293 }
294 {
295     local(*FOO);
296     tie(*FOO,'foo');
297     print FOO "sentence.", "reversed", "a", "is", "This";
298     print "-- ", <FOO>, " --\n";
299     my($buf,$len,$offset);
300     $buf = "string";
301     $len = 10; $offset = 1;
302     read(FOO, $buf, $len, $offset) == 100 or die "foo->READ failed";
303     getc(FOO) eq "a" or die "foo->GETC failed";
304     printf "%s is number %d\n", "Perl", 1;
305 }
306 EXPECT
307 This is a reversed sentence.
308 -- Out of inspiration --
309 foo->can(READ)(string 10 1)
310 Don't GETC, Get Perl
311 Perl is number 1
312 and destroyed as well
313 ########
314 my @a; $a[2] = 1; for (@a) { $_ = 2 } print "@a\n"
315 EXPECT
316 2 2 2
317 ########
318 # used to attach defelem magic to all immortal values,
319 # which made restore of local $_ fail.
320 foo(2>1);
321 sub foo { bar() for @_;  }
322 sub bar { local $_; }
323 print "ok\n";
324 EXPECT
325 ok
326 ########
327 @a = ($a, $b, $c, $d) = (5, 6);
328 print "ok\n"
329   if ($a[0] == 5 and $a[1] == 6 and !defined $a[2] and !defined $a[3]);
330 EXPECT
331 ok
332 ########
333 print "ok\n" if (1E2<<1 == 200 and 3E4<<3 == 240000);
334 EXPECT
335 ok
336 ########
337 print "ok\n" if ("\0" lt "\xFF");
338 EXPECT
339 ok
340 ########
341 open(H,'run/kill_perl.t'); # must be in the 't' directory
342 stat(H);
343 print "ok\n" if (-e _ and -f _ and -r _);
344 EXPECT
345 ok
346 ########
347 sub thing { 0 || return qw(now is the time) }
348 print thing(), "\n";
349 EXPECT
350 nowisthetime
351 ########
352 $ren = 'joy';
353 $stimpy = 'happy';
354 { local $main::{ren} = *stimpy; print $ren, ' ' }
355 print $ren, "\n";
356 EXPECT
357 happy joy
358 ########
359 $stimpy = 'happy';
360 { local $main::{ren} = *stimpy; print ${'ren'}, ' ' }
361 print +(defined(${'ren'}) ? 'oops' : 'joy'), "\n";
362 EXPECT
363 happy joy
364 ########
365 package p;
366 sub func { print 'really ' unless wantarray; 'p' }
367 sub groovy { 'groovy' }
368 package main;
369 print p::func()->groovy(), "\n"
370 EXPECT
371 really groovy
372 ########
373 @list = ([ 'one', 1 ], [ 'two', 2 ]);
374 sub func { $num = shift; (grep $_->[1] == $num, @list)[0] }
375 print scalar(map &func($_), 1 .. 3), " ",
376       scalar(map scalar &func($_), 1 .. 3), "\n";
377 EXPECT
378 2 3
379 ########
380 ($k, $s)  = qw(x 0);
381 @{$h{$k}} = qw(1 2 4);
382 for (@{$h{$k}}) { $s += $_; delete $h{$k} if ($_ == 2) }
383 print "bogus\n" unless $s == 7;
384 ########
385 my $a = 'outer';
386 eval q[ my $a = 'inner'; eval q[ print "$a " ] ];
387 eval { my $x = 'peace'; eval q[ print "$x\n" ] }
388 EXPECT
389 inner peace
390 ########
391 -w
392 $| = 1;
393 sub foo {
394     print "In foo1\n";
395     eval 'sub foo { print "In foo2\n" }';
396     print "Exiting foo1\n";
397 }
398 foo;
399 foo;
400 EXPECT
401 In foo1
402 Subroutine foo redefined at (eval 1) line 1.
403 Exiting foo1
404 In foo2
405 ########
406 $s = 0;
407 map {#this newline here tickles the bug
408 $s += $_} (1,2,4);
409 print "eat flaming death\n" unless ($s == 7);
410 ########
411 sub foo { local $_ = shift; split; @_ }
412 @x = foo(' x  y  z ');
413 print "you die joe!\n" unless "@x" eq 'x y z';
414 ########
415 /(?{"{"})/      # Check it outside of eval too
416 EXPECT
417 Sequence (?{...}) not terminated or not {}-balanced at - line 1, within pattern
418 Sequence (?{...}) not terminated or not {}-balanced in regex; marked by <-- HERE in m/(?{ <-- HERE "{"})/ at - line 1.
419 ########
420 /(?{"{"}})/     # Check it outside of eval too
421 EXPECT
422 Unmatched right curly bracket at (re_eval 1) line 1, at end of line
423 syntax error at (re_eval 1) line 1, near ""{"}"
424 Compilation failed in regexp at - line 1.
425 ########
426 BEGIN { @ARGV = qw(a b c d e) }
427 BEGIN { print "argv <@ARGV>\nbegin <",shift,">\n" }
428 END { print "end <",shift,">\nargv <@ARGV>\n" }
429 INIT { print "init <",shift,">\n" }
430 CHECK { print "check <",shift,">\n" }
431 EXPECT
432 argv <a b c d e>
433 begin <a>
434 check <b>
435 init <c>
436 end <d>
437 argv <e>
438 ########
439 -l
440 # fdopen from a system descriptor to a system descriptor used to close
441 # the former.
442 open STDERR, '>&=STDOUT' or die $!;
443 select STDOUT; $| = 1; print fileno STDOUT or die $!;
444 select STDERR; $| = 1; print fileno STDERR or die $!;
445 EXPECT
446 1
447 2
448 ########
449 -w
450 sub testme { my $a = "test"; { local $a = "new test"; print $a }}
451 EXPECT
452 Can't localize lexical variable $a at - line 2.
453 ########
454 package X;
455 sub ascalar { my $r; bless \$r }
456 sub DESTROY { print "destroyed\n" };
457 package main;
458 *s = ascalar X;
459 EXPECT
460 destroyed
461 ########
462 package X;
463 sub anarray { bless [] }
464 sub DESTROY { print "destroyed\n" };
465 package main;
466 *a = anarray X;
467 EXPECT
468 destroyed
469 ########
470 package X;
471 sub ahash { bless {} }
472 sub DESTROY { print "destroyed\n" };
473 package main;
474 *h = ahash X;
475 EXPECT
476 destroyed
477 ########
478 package X;
479 sub aclosure { my $x; bless sub { ++$x } }
480 sub DESTROY { print "destroyed\n" };
481 package main;
482 *c = aclosure X;
483 EXPECT
484 destroyed
485 ########
486 package X;
487 sub any { bless {} }
488 my $f = "FH000"; # just to thwart any future optimisations
489 sub afh { select select ++$f; my $r = *{$f}{IO}; delete $X::{$f}; bless $r }
490 sub DESTROY { print "destroyed\n" }
491 package main;
492 $x = any X; # to bump sv_objcount. IO objs aren't counted??
493 *f = afh X;
494 EXPECT
495 destroyed
496 destroyed
497 ########
498 BEGIN {
499   $| = 1;
500   $SIG{__WARN__} = sub {
501     eval { print $_[0] };
502     die "bar\n";
503   };
504   warn "foo\n";
505 }
506 EXPECT
507 foo
508 bar
509 BEGIN failed--compilation aborted at - line 8.
510 ########
511 package X;
512 @ISA='Y';
513 sub new {
514     my $class = shift;
515     my $self = { };
516     bless $self, $class;
517     my $init = shift;
518     $self->foo($init);
519     print "new", $init;
520     return $self;
521 }
522 sub DESTROY {
523     my $self = shift;
524     print "DESTROY", $self->foo;
525 }
526 package Y;
527 sub attribute {
528     my $self = shift;
529     my $var = shift;
530     if (@_ == 0) {
531         return $self->{$var};
532     } elsif (@_ == 1) {
533         $self->{$var} = shift;
534     }
535 }
536 sub AUTOLOAD {
537     $AUTOLOAD =~ /::([^:]+)$/;
538     my $method = $1;
539     splice @_, 1, 0, $method;
540     goto &attribute;
541 }
542 package main;
543 my $x = X->new(1);
544 for (2..3) {
545     my $y = X->new($_);
546     print $y->foo;
547 }
548 print $x->foo;
549 EXPECT
550 new1new22DESTROY2new33DESTROY31DESTROY1
551 ########
552 re();
553 sub re {
554     my $re = join '', eval 'qr/(??{ $obj->method })/';
555     $re;
556 }
557 EXPECT
558 ########
559 use strict;
560 my $foo = "ZZZ\n";
561 END { print $foo }
562 EXPECT
563 ZZZ
564 ########
565 eval '
566 use strict;
567 my $foo = "ZZZ\n";
568 END { print $foo }
569 ';
570 EXPECT
571 ZZZ
572 ########
573 -w
574 if (@ARGV) { print "" }
575 else {
576   if ($x == 0) { print "" } else { print $x }
577 }
578 EXPECT
579 Use of uninitialized value in numeric eq (==) at - line 4.
580 ########
581 $x = sub {};
582 foo();
583 sub foo { eval { return }; }
584 print "ok\n";
585 EXPECT
586 ok
587 ########
588 # moved to op/lc.t
589 EXPECT
590 ########
591 sub f { my $a = 1; my $b = 2; my $c = 3; my $d = 4; next }
592 my $x = "foo";
593 { f } continue { print $x, "\n" }
594 EXPECT
595 foo
596 ########
597 sub C () { 1 }
598 sub M { $_[0] = 2; }
599 eval "C";
600 M(C);
601 EXPECT
602 Modification of a read-only value attempted at - line 2.
603 ########
604 print qw(ab a\b a\\b);
605 EXPECT
606 aba\ba\b
607 ########
608 # lexicals declared after the myeval() definition should not be visible
609 # within it
610 sub myeval { eval $_[0] }
611 my $foo = "ok 2\n";
612 myeval('sub foo { local $foo = "ok 1\n"; print $foo; }');
613 die $@ if $@;
614 foo();
615 print $foo;
616 EXPECT
617 ok 1
618 ok 2
619 ########
620 # lexicals outside an eval"" should be visible inside subroutine definitions
621 # within it
622 eval <<'EOT'; die $@ if $@;
623 {
624     my $X = "ok\n";
625     eval 'sub Y { print $X }'; die $@ if $@;
626     Y();
627 }
628 EOT
629 EXPECT
630 ok
631 ########
632 # This test is here instead of pragma/locale.t because
633 # the bug depends on in the internal state of the locale
634 # settings and pragma/locale messes up that state pretty badly.
635 # We need a "fresh run".
636 BEGIN {
637     eval { require POSIX };
638     if ($@) {
639         exit(0); # running minitest?
640     }
641 }
642 use Config;
643 my $have_setlocale = $Config{d_setlocale} eq 'define';
644 $have_setlocale = 0 if $@;
645 # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1"
646 # and mingw32 uses said silly CRT
647 $have_setlocale = 0 if (($^O eq 'MSWin32' || $^O eq 'NetWare') && $Config{cc} =~ /^(cl|gcc)/i);
648 exit(0) unless $have_setlocale;
649 my @locales;
650 if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a|")) {
651     while(<LOCALES>) {
652         chomp;
653         push(@locales, $_);
654     }
655     close(LOCALES);
656 }
657 exit(0) unless @locales;
658 for (@locales) {
659     use POSIX qw(locale_h);
660     use locale;
661     setlocale(LC_NUMERIC, $_) or next;
662     my $s = sprintf "%g %g", 3.1, 3.1;
663     next if $s eq '3.1 3.1' || $s =~ /^(3.+1) \1$/;
664     print "$_ $s\n";
665 }
666 EXPECT
667 ########
668 die qr(x)
669 EXPECT
670 (?-xism:x) at - line 1.
671 ########
672 # 20001210.003 mjd@plover.com
673 format REMITOUT_TOP =
674 FOO
675 .
676
677 format REMITOUT =
678 BAR
679 .
680
681 # This loop causes a segv in 5.6.0
682 for $lineno (1..61) {
683    write REMITOUT;
684 }
685
686 print "It's OK!";
687 EXPECT
688 It's OK!
689 ########
690 # Inaba Hiroto
691 reset;
692 if (0) {
693   if ("" =~ //) {
694   }
695 }
696 ########
697 # Nicholas Clark
698 $ENV{TERM} = 0;
699 reset;
700 // if 0;
701 ########
702 # Vadim Konovalov
703 use strict;
704 sub new_pmop($) {
705     my $pm = shift;
706     return eval "sub {shift=~/$pm/}";
707 }
708 new_pmop "abcdef"; reset;
709 new_pmop "abcdef"; reset;
710 new_pmop "abcdef"; reset;
711 new_pmop "abcdef"; reset;
712 ########
713 # David Dyck
714 # coredump in 5.7.1
715 close STDERR; die;
716 EXPECT
717 ########
718 -w
719 "x" =~ /(\G?x)?/;       # core dump in 20000716.007
720 ########
721 # Bug 20010515.004
722 my @h = 1 .. 10;
723 bad(@h);
724 sub bad {
725    undef @h;
726    print "O";
727    print for @_;
728    print "K";
729 }
730 EXPECT
731 OK
732 ########
733 # Bug 20010506.041
734 "abcd\x{1234}" =~ /(a)(b[c])(d+)?/i and print "ok\n";
735 EXPECT
736 ok
737 ########
738 # Bug 20010422.005
739 {s//${}/; //}
740 EXPECT
741 syntax error at - line 2, near "${}"
742 Execution of - aborted due to compilation errors.
743 ########
744 # Bug 20010528.007
745 "\x{"
746 EXPECT
747 Missing right brace on \x{} at - line 2, within string
748 Execution of - aborted due to compilation errors.
749 ########
750 my $foo = Bar->new();
751 my @dst;
752 END {
753     ($_ = "@dst") =~ s/\(0x.+?\)/(0x...)/;
754     print $_, "\n";
755 }
756 package Bar;
757 sub new {
758     my Bar $self = bless [], Bar;
759     eval '$self';
760     return $self;
761 }
762 sub DESTROY { 
763     push @dst, "$_[0]";
764 }
765 EXPECT
766 Bar=ARRAY(0x...)
767 ########
768 # 20010407.008 sprintf removes utf8-ness
769 $a = sprintf "\x{1234}";
770 printf "%x %d\n", unpack("U*", $a), length($a);
771 $a = sprintf "%s", "\x{5678}";
772 printf "%x %d\n", unpack("U*", $a), length($a);
773 $a = sprintf "\x{1234}%s", "\x{5678}";
774 printf "%x %x %d\n", unpack("U*", $a), length($a);
775 EXPECT
776 1234 1
777 5678 1
778 1234 5678 2
779 ######## found by Markov chain stress testing
780 eval "a.b.c.d.e.f;sub"
781 EXPECT
782
783 ######## perlbug ID 20010831.001
784 ($a, b) = (1, 2);
785 EXPECT
786 Can't modify constant item in list assignment at - line 1, near ");"
787 Execution of - aborted due to compilation errors.
788 ######## tying a bareword causes a segfault in 5.6.1
789 tie FOO, "Foo";
790 EXPECT
791 Can't modify constant item in tie at - line 1, near ""Foo";"
792 Execution of - aborted due to compilation errors.
793 ######## undefing constant causes a segfault in 5.6.1 [ID 20010906.019]
794 undef foo;
795 EXPECT
796 Can't modify constant item in undef operator at - line 1, near "foo;"
797 Execution of - aborted due to compilation errors.
798 ######## (?{...}) compilation bounces on PL_rs
799 -0
800 {
801   /(?{ $x })/;
802   # {
803 }
804 BEGIN { print "ok\n" }
805 EXPECT
806 ok