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