This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: [perl #47195] $1 suddenly tainted after regexp on utf-8 string
[perl5.git] / t / op / taint.t
CommitLineData
1e422769
PP
1#!./perl -T
2#
3# Taint tests by Tom Phoenix <rootbeer@teleport.com>.
4#
5# I don't claim to know all about tainting. If anyone sees
9607fc9c 6# tests that I've missed here, please add them. But this is
1e422769
PP
7# better than having no tests at all, right?
8#
9
10BEGIN {
11 chdir 't' if -d 't';
20822f61 12 @INC = '../lib';
1e422769
PP
13}
14
15use strict;
16use Config;
dc459aad 17use File::Spec::Functions;
1e422769 18
09f04786 19BEGIN { require './test.pl'; }
d3706118 20plan tests => 267;
7c36658b 21
0ecd3ba2
MG
22$| = 1;
23
c9f931b8
JH
24use vars qw($ipcsysv); # did we manage to load IPC::SysV?
25
3eeba6fb
CB
26BEGIN {
27 if ($^O eq 'VMS' && !defined($Config{d_setenv})) {
28 $ENV{PATH} = $ENV{PATH};
29 $ENV{TERM} = $ENV{TERM} ne ''? $ENV{TERM} : 'dummy';
30 }
be3174d2
GS
31 if ($Config{'extensions'} =~ /\bIPC\/SysV\b/
32 && ($Config{d_shm} || $Config{d_msg})) {
c9f931b8
JH
33 eval { require IPC::SysV };
34 unless ($@) {
35 $ipcsysv++;
ddc3217d 36 IPC::SysV->import(qw(IPC_PRIVATE IPC_RMID IPC_CREAT S_IRWXU IPC_NOWAIT));
c9f931b8 37 }
b9d1c439 38 }
3eeba6fb
CB
39}
40
09f04786
MS
41my $Is_MacOS = $^O eq 'MacOS';
42my $Is_VMS = $^O eq 'VMS';
43my $Is_MSWin32 = $^O eq 'MSWin32';
44my $Is_NetWare = $^O eq 'NetWare';
45my $Is_Dos = $^O eq 'dos';
46my $Is_Cygwin = $^O eq 'cygwin';
8c2c7bd1 47my $Is_OpenBSD = $^O eq 'openbsd';
08322f8f 48my $Invoke_Perl = $Is_VMS ? 'MCR Sys$Disk:[]Perl.exe' :
09f04786
MS
49 $Is_MSWin32 ? '.\perl' :
50 $Is_MacOS ? ':perl' :
51 $Is_NetWare ? 'perl' :
52 './perl' ;
c90c0ff4 53my @MoreEnv = qw/IFS CDPATH ENV BASH_ENV/;
7bac28a0 54
1e422769 55if ($Is_VMS) {
7bac28a0
PP
56 my (%old, $x);
57 for $x ('DCL$PATH', @MoreEnv) {
58 ($old{$x}) = $ENV{$x} =~ /^(.*)$/ if exists $ENV{$x};
59 }
1e422769
PP
60 eval <<EndOfCleanup;
61 END {
3eeba6fb 62 \$ENV{PATH} = '' if $Config{d_setenv};
1e422769 63 warn "# Note: logical name 'PATH' may have been deleted\n";
562a7b0c 64 \@ENV{keys %old} = values %old;
1e422769
PP
65 }
66EndOfCleanup
67}
68
69# Sources of taint:
70# The empty tainted value, for tainting strings
71my $TAINT = substr($^X, 0, 0);
72# A tainted zero, useful for tainting numbers
09f04786
MS
73my $TAINT0;
74{
75 no warnings;
76 $TAINT0 = 0 + $TAINT;
77}
1e422769
PP
78
79# This taints each argument passed. All must be lvalues.
80# Side effect: It also stringifies them. :-(
81sub taint_these (@) {
82 for (@_) { $_ .= $TAINT }
83}
84
85# How to identify taint when you see it
86sub any_tainted (@) {
87 not eval { join("",@_), kill 0; 1 };
88}
89sub tainted ($) {
90 any_tainted @_;
91}
92sub all_tainted (@) {
93 for (@_) { return 0 unless tainted $_ }
94 1;
95}
96
09f04786
MS
97
98sub test ($;$) {
99 my($ok, $diag) = @_;
100
101 my $curr_test = curr_test();
102
103 if ($ok) {
104 print "ok $curr_test\n";
1e422769 105 } else {
09f04786
MS
106 print "not ok $curr_test\n";
107 printf "# Failed test at line %d\n", (caller)[2];
1e422769
PP
108 for (split m/^/m, $diag) {
109 print "# $_";
110 }
9607fc9c 111 print "\n" unless
1e422769
PP
112 $diag eq ''
113 or substr($diag, -1) eq "\n";
114 }
09f04786
MS
115
116 next_test();
117
118 return $ok;
1e422769
PP
119}
120
121# We need an external program to call.
dc459aad 122my $ECHO = ($Is_MSWin32 ? ".\\echo$$" : $Is_MacOS ? ":echo$$" : ($Is_NetWare ? "echo$$" : "./echo$$"));
1e422769
PP
123END { unlink $ECHO }
124open PROG, "> $ECHO" or die "Can't create $ECHO: $!";
125print PROG 'print "@ARGV\n"', "\n";
126close PROG;
127my $echo = "$Invoke_Perl $ECHO";
128
dc459aad
JH
129my $TEST = catfile(curdir(), 'TEST');
130
1e422769
PP
131# First, let's make sure that Perl is checking the dangerous
132# environment variables. Maybe they aren't set yet, so we'll
133# taint them ourselves.
134{
135 $ENV{'DCL$PATH'} = '' if $Is_VMS;
136
f68313a1
SH
137 if ($Is_MSWin32 && $Config{ccname} =~ /bcc32/ && ! -f 'cc3250mt.dll') {
138 my $bcc_dir;
139 foreach my $dir (split /$Config{path_sep}/, $ENV{PATH}) {
140 if (-f "$dir/cc3250mt.dll") {
141 $bcc_dir = $dir and last;
142 }
143 }
144 if (defined $bcc_dir) {
145 require File::Copy;
146 File::Copy::copy("$bcc_dir/cc3250mt.dll", '.') or
147 die "$0: failed to copy cc3250mt.dll: $!\n";
148 eval q{
149 END { unlink "cc3250mt.dll" }
150 };
151 }
152 }
e59c8b07 153 $ENV{PATH} = ($Is_Cygwin) ? '/usr/bin' : '';
c90c0ff4 154 delete @ENV{@MoreEnv};
7bac28a0
PP
155 $ENV{TERM} = 'dumb';
156
09f04786
MS
157 test eval { `$echo 1` } eq "1\n";
158
159 SKIP: {
160 skip "Environment tainting tests skipped", 4
161 if $Is_MSWin32 || $Is_NetWare || $Is_VMS || $Is_Dos || $Is_MacOS;
7bac28a0 162
7bac28a0
PP
163 my @vars = ('PATH', @MoreEnv);
164 while (my $v = $vars[0]) {
165 local $ENV{$v} = $TAINT;
166 last if eval { `$echo 1` };
167 last unless $@ =~ /^Insecure \$ENV{$v}/;
168 shift @vars;
169 }
09f04786 170 test !@vars, "@vars";
c90c0ff4
PP
171
172 # tainted $TERM is unsafe only if it contains metachars
173 local $ENV{TERM};
174 $ENV{TERM} = 'e=mc2';
09f04786 175 test eval { `$echo 1` } eq "1\n";
c90c0ff4 176 $ENV{TERM} = 'e=mc2' . $TAINT;
09f04786
MS
177 test !eval { `$echo 1` };
178 test $@ =~ /^Insecure \$ENV{TERM}/, $@;
5aabfad6 179 }
7bac28a0 180
9607fc9c 181 my $tmp;
2986a63f 182 if ($^O eq 'os2' || $^O eq 'amigaos' || $Is_MSWin32 || $Is_NetWare || $Is_Dos) {
48c036b1
GS
183 print "# all directories are writeable\n";
184 }
185 else {
9607fc9c 186 $tmp = (grep { defined and -d and (stat _)[2] & 2 }
099f76bb 187 qw(sys$scratch /tmp /var/tmp /usr/tmp),
9607fc9c
PP
188 @ENV{qw(TMP TEMP)})[0]
189 or print "# can't find world-writeable directory to test PATH\n";
190 }
191
09f04786
MS
192 SKIP: {
193 skip "all directories are writeable", 2 unless $tmp;
194
7bac28a0 195 local $ENV{PATH} = $tmp;
09f04786
MS
196 test !eval { `$echo 1` };
197 test $@ =~ /^Insecure directory in \$ENV{PATH}/, $@;
1e422769
PP
198 }
199
09f04786
MS
200 SKIP: {
201 skip "This is not VMS", 4 unless $Is_VMS;
202
1e422769 203 $ENV{'DCL$PATH'} = $TAINT;
09f04786
MS
204 test eval { `$echo 1` } eq '';
205 test $@ =~ /^Insecure \$ENV{DCL\$PATH}/, $@;
206 SKIP: {
207 skip q[can't find world-writeable directory to test DCL$PATH], 2
25fb98c0 208 unless $tmp;
09f04786 209
9607fc9c 210 $ENV{'DCL$PATH'} = $tmp;
09f04786
MS
211 test eval { `$echo 1` } eq '';
212 test $@ =~ /^Insecure directory in \$ENV{DCL\$PATH}/, $@;
9607fc9c 213 }
1e422769
PP
214 $ENV{'DCL$PATH'} = '';
215 }
1e422769
PP
216}
217
218# Let's see that we can taint and untaint as needed.
219{
220 my $foo = $TAINT;
09f04786 221 test tainted $foo;
9607fc9c
PP
222
223 # That was a sanity check. If it failed, stop the insanity!
224 die "Taint checks don't seem to be enabled" unless tainted $foo;
1e422769
PP
225
226 $foo = "foo";
09f04786 227 test not tainted $foo;
1e422769
PP
228
229 taint_these($foo);
09f04786 230 test tainted $foo;
1e422769
PP
231
232 my @list = 1..10;
09f04786 233 test not any_tainted @list;
1e422769 234 taint_these @list[1,3,5,7,9];
09f04786
MS
235 test any_tainted @list;
236 test all_tainted @list[1,3,5,7,9];
237 test not any_tainted @list[0,2,4,6,8];
1e422769
PP
238
239 ($foo) = $foo =~ /(.+)/;
09f04786 240 test not tainted $foo;
1e422769
PP
241
242 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
09f04786
MS
243 test not tainted $foo;
244 test $foo eq 'bar';
1e422769 245
b3eb6a9b
GS
246 {
247 use re 'taint';
248
249 ($foo) = ('bar' . $TAINT) =~ /(.+)/;
09f04786
MS
250 test tainted $foo;
251 test $foo eq 'bar';
b3eb6a9b
GS
252
253 $foo = $1 if ('bar' . $TAINT) =~ /(.+)/;
09f04786
MS
254 test tainted $foo;
255 test $foo eq 'bar';
b3eb6a9b
GS
256 }
257
258 $foo = $1 if 'bar' =~ /(.+)$TAINT/;
09f04786
MS
259 test tainted $foo;
260 test $foo eq 'bar';
48c036b1 261
1e422769 262 my $pi = 4 * atan2(1,1) + $TAINT0;
09f04786 263 test tainted $pi;
1e422769
PP
264
265 ($pi) = $pi =~ /(\d+\.\d+)/;
09f04786
MS
266 test not tainted $pi;
267 test sprintf("%.5f", $pi) eq '3.14159';
1e422769
PP
268}
269
270# How about command-line arguments? The problem is that we don't
271# always get some, so we'll run another process with some.
dc459aad
JH
272SKIP: {
273 my $arg = catfile(curdir(), "arg$$");
1e422769
PP
274 open PROG, "> $arg" or die "Can't create $arg: $!";
275 print PROG q{
276 eval { join('', @ARGV), kill 0 };
277 exit 0 if $@ =~ /^Insecure dependency/;
278 print "# Oops: \$@ was [$@]\n";
279 exit 1;
280 };
281 close PROG;
282 print `$Invoke_Perl "-T" $arg and some suspect arguments`;
09f04786 283 test !$?, "Exited with status $?";
1e422769
PP
284 unlink $arg;
285}
286
287# Reading from a file should be tainted
288{
09f04786 289 test open(FILE, $TEST), "Couldn't open '$TEST': $!";
1e422769
PP
290
291 my $block;
292 sysread(FILE, $block, 100);
9607fc9c 293 my $line = <FILE>;
1e422769 294 close FILE;
09f04786
MS
295 test tainted $block;
296 test tainted $line;
1e422769
PP
297}
298
c90c0ff4
PP
299# Globs should be forbidden, except under VMS,
300# which doesn't spawn an external program.
09f04786
MS
301SKIP: {
302 skip "globs should be forbidden", 2 if 1 or $Is_VMS;
303
7bac28a0 304 my @globs = eval { <*> };
09f04786 305 test @globs == 0 && $@ =~ /^Insecure dependency/;
1e422769 306
7bac28a0 307 @globs = eval { glob '*' };
09f04786 308 test @globs == 0 && $@ =~ /^Insecure dependency/;
1e422769
PP
309}
310
311# Output of commands should be tainted
312{
313 my $foo = `$echo abc`;
09f04786 314 test tainted $foo;
1e422769
PP
315}
316
317# Certain system variables should be tainted
318{
09f04786 319 test all_tainted $^X, $0;
1e422769
PP
320}
321
322# Results of matching should all be untainted
323{
324 my $foo = "abcdefghi" . $TAINT;
09f04786 325 test tainted $foo;
1e422769
PP
326
327 $foo =~ /def/;
09f04786 328 test not any_tainted $`, $&, $';
1e422769
PP
329
330 $foo =~ /(...)(...)(...)/;
09f04786 331 test not any_tainted $1, $2, $3, $+;
1e422769
PP
332
333 my @bar = $foo =~ /(...)(...)(...)/;
09f04786 334 test not any_tainted @bar;
1e422769 335
09f04786
MS
336 test tainted $foo; # $foo should still be tainted!
337 test $foo eq "abcdefghi";
1e422769
PP
338}
339
340# Operations which affect files can't use tainted data.
341{
09f04786
MS
342 test !eval { chmod 0, $TAINT }, 'chmod';
343 test $@ =~ /^Insecure dependency/, $@;
1e422769 344
9607fc9c
PP
345 # There is no feature test in $Config{} for truncate,
346 # so we allow for the possibility that it's missing.
09f04786
MS
347 test !eval { truncate 'NoSuChFiLe', $TAINT0 }, 'truncate';
348 test $@ =~ /^(?:Insecure dependency|truncate not implemented)/, $@;
1e422769 349
09f04786
MS
350 test !eval { rename '', $TAINT }, 'rename';
351 test $@ =~ /^Insecure dependency/, $@;
1e422769 352
09f04786
MS
353 test !eval { unlink $TAINT }, 'unlink';
354 test $@ =~ /^Insecure dependency/, $@;
9607fc9c 355
09f04786
MS
356 test !eval { utime $TAINT }, 'utime';
357 test $@ =~ /^Insecure dependency/, $@;
48c036b1 358
09f04786
MS
359 SKIP: {
360 skip "chown() is not available", 2 unless $Config{d_chown};
1e422769 361
09f04786
MS
362 test !eval { chown -1, -1, $TAINT }, 'chown';
363 test $@ =~ /^Insecure dependency/, $@;
1e422769
PP
364 }
365
09f04786
MS
366 SKIP: {
367 skip "link() is not available", 2 unless $Config{d_link};
368
369 test !eval { link $TAINT, '' }, 'link';
370 test $@ =~ /^Insecure dependency/, $@;
1e422769 371 }
09f04786
MS
372
373 SKIP: {
374 skip "symlink() is not available", 2 unless $Config{d_symlink};
375
376 test !eval { symlink $TAINT, '' }, 'symlink';
377 test $@ =~ /^Insecure dependency/, $@;
1e422769
PP
378 }
379}
380
381# Operations which affect directories can't use tainted data.
382{
09f04786
MS
383 test !eval { mkdir "foo".$TAINT, 0755.$TAINT0 }, 'mkdir';
384 test $@ =~ /^Insecure dependency/, $@;
1e422769 385
09f04786
MS
386 test !eval { rmdir $TAINT }, 'rmdir';
387 test $@ =~ /^Insecure dependency/, $@;
9607fc9c 388
09f04786
MS
389 test !eval { chdir "foo".$TAINT }, 'chdir';
390 test $@ =~ /^Insecure dependency/, $@;
48c036b1 391
09f04786
MS
392 SKIP: {
393 skip "chroot() is not available", 2 unless $Config{d_chroot};
394
395 test !eval { chroot $TAINT }, 'chroot';
396 test $@ =~ /^Insecure dependency/, $@;
1e422769
PP
397 }
398}
399
400# Some operations using files can't use tainted data.
401{
402 my $foo = "imaginary library" . $TAINT;
09f04786
MS
403 test !eval { require $foo }, 'require';
404 test $@ =~ /^Insecure dependency/, $@;
1e422769
PP
405
406 my $filename = "./taintB$$"; # NB: $filename isn't tainted!
407 END { unlink $filename if defined $filename }
408 $foo = $filename . $TAINT;
409 unlink $filename; # in any case
410
09f04786
MS
411 test !eval { open FOO, $foo }, 'open for read';
412 test $@ eq '', $@; # NB: This should be allowed
9d116dd7
JH
413
414 # Try first new style but allow also old style.
327ccce1
YST
415 # We do not want the whole taint.t to fail
416 # just because Errno possibly failing.
09f04786 417 test eval('$!{ENOENT}') ||
61ae2fbf
JH
418 $! == 2 || # File not found
419 ($Is_Dos && $! == 22) ||
420 ($^O eq 'mint' && $! == 33);
1e422769 421
09f04786
MS
422 test !eval { open FOO, "> $foo" }, 'open for write';
423 test $@ =~ /^Insecure dependency/, $@;
1e422769
PP
424}
425
426# Commands to the system can't use tainted data
427{
428 my $foo = $TAINT;
429
09f04786
MS
430 SKIP: {
431 skip "open('|') is not available", 4 if $^O eq 'amigaos';
432
433 test !eval { open FOO, "| x$foo" }, 'popen to';
434 test $@ =~ /^Insecure dependency/, $@;
1e422769 435
09f04786
MS
436 test !eval { open FOO, "x$foo |" }, 'popen from';
437 test $@ =~ /^Insecure dependency/, $@;
48c036b1 438 }
1e422769 439
09f04786
MS
440 test !eval { exec $TAINT }, 'exec';
441 test $@ =~ /^Insecure dependency/, $@;
9607fc9c 442
09f04786
MS
443 test !eval { system $TAINT }, 'system';
444 test $@ =~ /^Insecure dependency/, $@;
48c036b1 445
1e422769
PP
446 $foo = "*";
447 taint_these $foo;
448
09f04786
MS
449 test !eval { `$echo 1$foo` }, 'backticks';
450 test $@ =~ /^Insecure dependency/, $@;
1e422769 451
09f04786
MS
452 SKIP: {
453 # wildcard expansion doesn't invoke shell on VMS, so is safe
454 skip "This is not VMS", 2 unless $Is_VMS;
455
456 test join('', eval { glob $foo } ) ne '', 'globbing';
457 test $@ eq '', $@;
1e422769
PP
458 }
459}
460
461# Operations which affect processes can't use tainted data.
462{
09f04786
MS
463 test !eval { kill 0, $TAINT }, 'kill';
464 test $@ =~ /^Insecure dependency/, $@;
1e422769 465
09f04786
MS
466 SKIP: {
467 skip "setpgrp() is not available", 2 unless $Config{d_setpgrp};
1e422769 468
09f04786
MS
469 test !eval { setpgrp 0, $TAINT0 }, 'setpgrp';
470 test $@ =~ /^Insecure dependency/, $@;
1e422769 471 }
09f04786
MS
472
473 SKIP: {
474 skip "setpriority() is not available", 2 unless $Config{d_setprior};
475
476 test !eval { setpriority 0, $TAINT0, $TAINT0 }, 'setpriority';
477 test $@ =~ /^Insecure dependency/, $@;
1e422769
PP
478 }
479}
480
481# Some miscellaneous operations can't use tainted data.
482{
09f04786
MS
483 SKIP: {
484 skip "syscall() is not available", 2 unless $Config{d_syscall};
485
486 test !eval { syscall $TAINT }, 'syscall';
487 test $@ =~ /^Insecure dependency/, $@;
1e422769
PP
488 }
489
490 {
491 my $foo = "x" x 979;
492 taint_these $foo;
493 local *FOO;
494 my $temp = "./taintC$$";
495 END { unlink $temp }
09f04786 496 test open(FOO, "> $temp"), "Couldn't open $temp for write: $!";
1e422769 497
09f04786
MS
498 test !eval { ioctl FOO, $TAINT0, $foo }, 'ioctl';
499 test $@ =~ /^Insecure dependency/, $@;
1e422769 500
09f04786
MS
501 SKIP: {
502 skip "fcntl() is not available", 2 unless $Config{d_fcntl};
503
504 test !eval { fcntl FOO, $TAINT0, $foo }, 'fcntl';
505 test $@ =~ /^Insecure dependency/, $@;
1e422769
PP
506 }
507
508 close FOO;
509 }
510}
511
9607fc9c 512# Some tests involving references
1e422769
PP
513{
514 my $foo = 'abc' . $TAINT;
515 my $fooref = \$foo;
09f04786
MS
516 test not tainted $fooref;
517 test tainted $$fooref;
518 test tainted $foo;
1e422769 519}
54310121
PP
520
521# Some tests involving assignment
522{
523 my $foo = $TAINT0;
524 my $bar = $foo;
09f04786
MS
525 test all_tainted $foo, $bar;
526 test tainted($foo = $bar);
527 test tainted($bar = $bar);
528 test tainted($bar += $bar);
529 test tainted($bar -= $bar);
530 test tainted($bar *= $bar);
531 test tainted($bar++);
532 test tainted($bar /= $bar);
533 test tainted($bar += 0);
534 test tainted($bar -= 2);
535 test tainted($bar *= -1);
536 test tainted($bar /= 1);
537 test tainted($bar--);
538 test $bar == 0;
54310121 539}
a1f49e72
CS
540
541# Test assignment and return of lists
542{
543 my @foo = ("A", "tainted" . $TAINT, "B");
09f04786
MS
544 test not tainted $foo[0];
545 test tainted $foo[1];
546 test not tainted $foo[2];
a1f49e72 547 my @bar = @foo;
09f04786
MS
548 test not tainted $bar[0];
549 test tainted $bar[1];
550 test not tainted $bar[2];
a1f49e72 551 my @baz = eval { "A", "tainted" . $TAINT, "B" };
09f04786
MS
552 test not tainted $baz[0];
553 test tainted $baz[1];
554 test not tainted $baz[2];
a1f49e72 555 my @plugh = eval q[ "A", "tainted" . $TAINT, "B" ];
09f04786
MS
556 test not tainted $plugh[0];
557 test tainted $plugh[1];
558 test not tainted $plugh[2];
a1f49e72 559 my $nautilus = sub { "A", "tainted" . $TAINT, "B" };
09f04786
MS
560 test not tainted ((&$nautilus)[0]);
561 test tainted ((&$nautilus)[1]);
562 test not tainted ((&$nautilus)[2]);
a1f49e72 563 my @xyzzy = &$nautilus;
09f04786
MS
564 test not tainted $xyzzy[0];
565 test tainted $xyzzy[1];
566 test not tainted $xyzzy[2];
a1f49e72 567 my $red_october = sub { return "A", "tainted" . $TAINT, "B" };
09f04786
MS
568 test not tainted ((&$red_october)[0]);
569 test tainted ((&$red_october)[1]);
570 test not tainted ((&$red_october)[2]);
a1f49e72 571 my @corge = &$red_october;
09f04786
MS
572 test not tainted $corge[0];
573 test tainted $corge[1];
574 test not tainted $corge[2];
a1f49e72 575}
fb73857a
PP
576
577# Test for system/library calls returning string data of dubious origin.
578{
579 # No reliable %Config check for getpw*
09f04786
MS
580 SKIP: {
581 skip "getpwent() is not available", 1 unless
582 eval { setpwent(); getpwent() };
583
fb73857a
PP
584 setpwent();
585 my @getpwent = getpwent();
586 die "getpwent: $!\n" unless (@getpwent);
09f04786 587 test ( not tainted $getpwent[0]
2959b6e3 588 and tainted $getpwent[1]
fb73857a
PP
589 and not tainted $getpwent[2]
590 and not tainted $getpwent[3]
591 and not tainted $getpwent[4]
592 and not tainted $getpwent[5]
3972a534 593 and tainted $getpwent[6] # ge?cos
fb73857a 594 and not tainted $getpwent[7]
3972a534 595 and tainted $getpwent[8]); # shell
fb73857a 596 endpwent();
fb73857a
PP
597 }
598
09f04786
MS
599 SKIP: {
600 # pretty hard to imagine not
601 skip "readdir() is not available", 1 unless $Config{d_readdir};
602
fb73857a
PP
603 local(*D);
604 opendir(D, "op") or die "opendir: $!\n";
605 my $readdir = readdir(D);
09f04786
MS
606 test tainted $readdir;
607 closedir(D);
fb73857a
PP
608 }
609
09f04786
MS
610 SKIP: {
611 skip "readlink() or symlink() is not available" unless
612 $Config{d_readlink} && $Config{d_symlink};
613
fb73857a
PP
614 my $symlink = "sl$$";
615 unlink($symlink);
dc459aad
JH
616 my $sl = "/something/naughty";
617 # it has to be a real path on Mac OS
618 $sl = MacPerl::MakePath((MacPerl::Volumes())[0]) if $Is_MacOS;
619 symlink($sl, $symlink) or die "symlink: $!\n";
fb73857a 620 my $readlink = readlink($symlink);
09f04786 621 test tainted $readlink;
fb73857a 622 unlink($symlink);
fb73857a
PP
623 }
624}
625
626# test bitwise ops (regression bug)
627{
628 my $why = "y";
629 my $j = "x" | $why;
09f04786 630 test not tainted $j;
fb73857a
PP
631 $why = $TAINT."y";
632 $j = "x" | $why;
09f04786 633 test tainted $j;
fb73857a
PP
634}
635
48c036b1
GS
636# test target of substitution (regression bug)
637{
638 my $why = $TAINT."y";
639 $why =~ s/y/z/;
09f04786 640 test tainted $why;
48c036b1
GS
641
642 my $z = "[z]";
643 $why =~ s/$z/zee/;
09f04786 644 test tainted $why;
48c036b1
GS
645
646 $why =~ s/e/'-'.$$/ge;
09f04786 647 test tainted $why;
48c036b1 648}
d929ce6f 649
09f04786
MS
650
651SKIP: {
652 skip "no IPC::SysV", 2 unless $ipcsysv;
653
654 # test shmread
655 SKIP: {
656 skip "shm*() not available", 1 unless $Config{d_shm};
657
658 no strict 'subs';
659 my $sent = "foobar";
660 my $rcvd;
661 my $size = 2000;
662 my $id = shmget(IPC_PRIVATE, $size, S_IRWXU);
663
664 if (defined $id) {
665 if (shmwrite($id, $sent, 0, 60)) {
666 if (shmread($id, $rcvd, 0, 60)) {
667 substr($rcvd, index($rcvd, "\0")) = '';
668 } else {
669 warn "# shmread failed: $!\n";
670 }
671 } else {
672 warn "# shmwrite failed: $!\n";
673 }
674 shmctl($id, IPC_RMID, 0) or warn "# shmctl failed: $!\n";
675 } else {
676 warn "# shmget failed: $!\n";
677 }
678
679 skip "SysV shared memory operation failed", 1 unless
680 $rcvd eq $sent;
681
682 test tainted $rcvd;
c9f931b8 683 }
c2e66d9e 684
d929ce6f 685
09f04786
MS
686 # test msgrcv
687 SKIP: {
688 skip "msg*() not available", 1 unless $Config{d_msg};
41d6edb2 689
b9d1c439 690 no strict 'subs';
41d6edb2
JH
691 my $id = msgget(IPC_PRIVATE, IPC_CREAT | S_IRWXU);
692
693 my $sent = "message";
694 my $type_sent = 1234;
695 my $rcvd;
696 my $type_rcvd;
697
698 if (defined $id) {
ddc3217d
JH
699 if (msgsnd($id, pack("l! a*", $type_sent, $sent), IPC_NOWAIT)) {
700 if (msgrcv($id, $rcvd, 60, 0, IPC_NOWAIT)) {
41d6edb2
JH
701 ($type_rcvd, $rcvd) = unpack("l! a*", $rcvd);
702 } else {
ddc3217d 703 warn "# msgrcv failed: $!\n";
41d6edb2
JH
704 }
705 } else {
ddc3217d 706 warn "# msgsnd failed: $!\n";
41d6edb2 707 }
c2e66d9e 708 msgctl($id, IPC_RMID, 0) or warn "# msgctl failed: $!\n";
41d6edb2
JH
709 } else {
710 warn "# msgget failed\n";
711 }
712
09f04786
MS
713 SKIP: {
714 skip "SysV message queue operation failed", 1
715 unless $rcvd eq $sent && $type_sent == $type_rcvd;
716
717 test tainted $rcvd;
41d6edb2 718 }
41d6edb2
JH
719 }
720}
721
3887d568
AP
722{
723 # bug id 20001004.006
724
dc459aad 725 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
3887d568
AP
726 local $/;
727 my $a = <IN>;
728 my $b = <IN>;
09f04786
MS
729
730 ok tainted($a) && tainted($b) && !defined($b);
731
27c9684d 732 close IN;
3887d568 733}
27c9684d
AP
734
735{
736 # bug id 20001004.007
737
dc459aad 738 open IN, $TEST or warn "$0: cannot read $TEST: $!" ;
27c9684d
AP
739 my $a = <IN>;
740
741 my $c = { a => 42,
742 b => $a };
09f04786
MS
743
744 ok !tainted($c->{a}) && tainted($c->{b});
745
27c9684d
AP
746
747 my $d = { a => $a,
748 b => 42 };
09f04786
MS
749 ok tainted($d->{a}) && !tainted($d->{b});
750
27c9684d
AP
751
752 my $e = { a => 42,
753 b => { c => $a, d => 42 } };
09f04786
MS
754 ok !tainted($e->{a}) &&
755 !tainted($e->{b}) &&
756 tainted($e->{b}->{c}) &&
757 !tainted($e->{b}->{d});
27c9684d
AP
758
759 close IN;
760}
761
b94c04ac
JH
762{
763 # bug id 20010519.003
764
248c32c0
JH
765 BEGIN {
766 use vars qw($has_fcntl);
767 eval { require Fcntl; import Fcntl; };
768 unless ($@) {
769 $has_fcntl = 1;
770 }
771 }
b94c04ac 772
09f04786
MS
773 SKIP: {
774 skip "no Fcntl", 18 unless $has_fcntl;
775
248c32c0
JH
776 my $evil = "foo" . $TAINT;
777
778 eval { sysopen(my $ro, $evil, &O_RDONLY) };
09f04786 779 test $@ !~ /^Insecure dependency/, $@;
248c32c0
JH
780
781 eval { sysopen(my $wo, $evil, &O_WRONLY) };
09f04786 782 test $@ =~ /^Insecure dependency/, $@;
248c32c0
JH
783
784 eval { sysopen(my $rw, $evil, &O_RDWR) };
09f04786 785 test $@ =~ /^Insecure dependency/, $@;
248c32c0
JH
786
787 eval { sysopen(my $ap, $evil, &O_APPEND) };
09f04786 788 test $@ =~ /^Insecure dependency/, $@;
248c32c0
JH
789
790 eval { sysopen(my $cr, $evil, &O_CREAT) };
09f04786 791 test $@ =~ /^Insecure dependency/, $@;
248c32c0
JH
792
793 eval { sysopen(my $tr, $evil, &O_TRUNC) };
09f04786 794 test $@ =~ /^Insecure dependency/, $@;
248c32c0 795
09f04786
MS
796 eval { sysopen(my $ro, "foo", &O_RDONLY | $TAINT0) };
797 test $@ !~ /^Insecure dependency/, $@;
248c32c0 798
09f04786
MS
799 eval { sysopen(my $wo, "foo", &O_WRONLY | $TAINT0) };
800 test $@ =~ /^Insecure dependency/, $@;
248c32c0 801
09f04786
MS
802 eval { sysopen(my $rw, "foo", &O_RDWR | $TAINT0) };
803 test $@ =~ /^Insecure dependency/, $@;
248c32c0 804
09f04786
MS
805 eval { sysopen(my $ap, "foo", &O_APPEND | $TAINT0) };
806 test $@ =~ /^Insecure dependency/, $@;
248c32c0 807
09f04786
MS
808 eval { sysopen(my $cr, "foo", &O_CREAT | $TAINT0) };
809 test $@ =~ /^Insecure dependency/, $@;
248c32c0 810
09f04786
MS
811 eval { sysopen(my $tr, "foo", &O_TRUNC | $TAINT0) };
812 test $@ =~ /^Insecure dependency/, $@;
248c32c0 813
09f04786
MS
814 eval { sysopen(my $ro, "foo", &O_RDONLY, $TAINT0) };
815 test $@ !~ /^Insecure dependency/, $@;
248c32c0 816
09f04786
MS
817 eval { sysopen(my $wo, "foo", &O_WRONLY, $TAINT0) };
818 test $@ =~ /^Insecure dependency/, $@;
248c32c0 819
09f04786
MS
820 eval { sysopen(my $rw, "foo", &O_RDWR, $TAINT0) };
821 test $@ =~ /^Insecure dependency/, $@;
248c32c0 822
09f04786
MS
823 eval { sysopen(my $ap, "foo", &O_APPEND, $TAINT0) };
824 test $@ =~ /^Insecure dependency/, $@;
248c32c0 825
09f04786
MS
826 eval { sysopen(my $cr, "foo", &O_CREAT, $TAINT0) };
827 test $@ =~ /^Insecure dependency/, $@;
248c32c0 828
09f04786
MS
829 eval { sysopen(my $tr, "foo", &O_TRUNC, $TAINT0) };
830 test $@ =~ /^Insecure dependency/, $@;
248c32c0
JH
831
832 unlink("foo"); # not unlink($evil), because that would fail...
833 }
b94c04ac
JH
834}
835
65811bc3
JH
836{
837 # bug 20010526.004
838
839 use warnings;
840
09f04786
MS
841 my $saw_warning = 0;
842 local $SIG{__WARN__} = sub { $saw_warning = 1 };
65811bc3
JH
843
844 sub fmi {
845 my $divnum = shift()/1;
846 sprintf("%1.1f\n", $divnum);
847 }
848
849 fmi(21 . $TAINT);
850 fmi(37);
851 fmi(248);
852
09f04786 853 test !$saw_warning;
65811bc3
JH
854}
855
9e1b5a4e
A
856
857{
858 # Bug ID 20010730.010
859
860 my $i = 0;
861
862 sub Tie::TIESCALAR {
863 my $class = shift;
864 my $arg = shift;
865
866 bless \$arg => $class;
867 }
868
869 sub Tie::FETCH {
870 $i ++;
871 ${$_ [0]}
872 }
873
874
875 package main;
876
877 my $bar = "The Big Bright Green Pleasure Machine";
878 taint_these $bar;
879 tie my ($foo), Tie => $bar;
880
881 my $baz = $foo;
882
09f04786 883 ok $i == 1;
9e1b5a4e
A
884}
885
8852b6d2
JH
886{
887 # Check that all environment variables are tainted.
888 my @untainted;
889 while (my ($k, $v) = each %ENV) {
890 if (!tainted($v) &&
eb25aaf6
HS
891 # These we have explicitly untainted or set earlier.
892 $k !~ /^(BASH_ENV|CDPATH|ENV|IFS|PATH|PERL_CORE|TEMP|TERM|TMP)$/) {
8852b6d2
JH
893 push @untainted, "# '$k' = '$v'\n";
894 }
895 }
09f04786 896 test @untainted == 0, "untainted:\n @untainted";
8852b6d2 897}
9e1b5a4e
A
898
899
9aa05f58 900ok( ${^TAINT} == 1, '$^TAINT is on' );
7c36658b
MS
901
902eval { ${^TAINT} = 0 };
903ok( ${^TAINT}, '$^TAINT is not assignable' );
904ok( $@ =~ /^Modification of a read-only value attempted/,
c212f17f 905 'Assigning to ${^TAINT} fails' );
7c36658b 906
e08e52cf
AMS
907{
908 # bug 20011111.105
909
910 my $re1 = qr/x$TAINT/;
09f04786 911 test tainted $re1;
e08e52cf
AMS
912
913 my $re2 = qr/^$re1\z/;
09f04786 914 test tainted $re2;
e08e52cf
AMS
915
916 my $re3 = "$re2";
09f04786 917 test tainted $re3;
e08e52cf 918}
52a55424 919
09f04786
MS
920SKIP: {
921 skip "system {} has different semantics on Win32", 1 if $Is_MSWin32;
922
52a55424
RG
923 # bug 20010221.005
924 local $ENV{PATH} .= $TAINT;
925 eval { system { "echo" } "/arg0", "arg1" };
09f04786 926 test $@ =~ /^Insecure \$ENV/;
52a55424 927}
09f04786
MS
928
929TODO: {
930 todo_skip 'tainted %ENV warning occludes tainted arguments warning', 22
931 if $Is_VMS;
932
933 # bug 20020208.005 plus some single arg exec/system extras
06bd1802 934 my $err = qr/^Insecure dependency/ ;
09f04786
MS
935 test !eval { exec $TAINT, $TAINT }, 'exec';
936 test $@ =~ $err, $@;
937 test !eval { exec $TAINT $TAINT }, 'exec';
938 test $@ =~ $err, $@;
939 test !eval { exec $TAINT $TAINT, $TAINT }, 'exec';
940 test $@ =~ $err, $@;
941 test !eval { exec $TAINT 'notaint' }, 'exec';
942 test $@ =~ $err, $@;
943 test !eval { exec {'notaint'} $TAINT }, 'exec';
944 test $@ =~ $err, $@;
945
946 test !eval { system $TAINT, $TAINT }, 'system';
947 test $@ =~ $err, $@;
948 test !eval { system $TAINT $TAINT }, 'system';
949 test $@ =~ $err, $@;
950 test !eval { system $TAINT $TAINT, $TAINT }, 'system';
951 test $@ =~ $err, $@;
952 test !eval { system $TAINT 'notaint' }, 'system';
953 test $@ =~ $err, $@;
954 test !eval { system {'notaint'} $TAINT }, 'system';
955 test $@ =~ $err, $@;
956
957 eval {
958 no warnings;
959 system("lskdfj does not exist","with","args");
960 };
961 test !$@;
962
963 SKIP: {
964 skip "no exec() on MacOS Classic" if $Is_MacOS;
965
966 eval {
967 no warnings;
968 exec("lskdfj does not exist","with","args");
969 };
970 test !$@;
63c6dcc1 971 }
6c8794e1
JH
972
973 # If you add tests here update also the above skip block for VMS.
bbd7eb8a 974}
a8c7c11a
HS
975
976{
977 # [ID 20020704.001] taint propagation failure
978 use re 'taint';
979 $TAINT =~ /(.*)/;
09f04786 980 test tainted(my $foo = $1);
a8c7c11a 981}
7b756e0a
RGS
982
983{
c038024b
RGS
984 # [perl #24291] this used to dump core
985 our %nonmagicalenv = ( PATH => "util" );
7b756e0a
RGS
986 local *ENV = \%nonmagicalenv;
987 eval { system("lskdfj"); };
09f04786 988 test $@ =~ /^%ENV is aliased to another variable while running with -T switch/;
c038024b 989 local *ENV = *nonmagicalenv;
7b756e0a 990 eval { system("lskdfj"); };
09f04786 991 test $@ =~ /^%ENV is aliased to %nonmagicalenv while running with -T switch/;
7b756e0a 992}
0b4182de
RD
993{
994 # [perl #24248]
995 $TAINT =~ /(.*)/;
09f04786 996 test !tainted($1);
0b4182de 997 my $notaint = $1;
09f04786 998 test !tainted($notaint);
0b4182de
RD
999
1000 my $l;
1001 $notaint =~ /($notaint)/;
1002 $l = $1;
09f04786
MS
1003 test !tainted($1);
1004 test !tainted($l);
0b4182de
RD
1005 $notaint =~ /($TAINT)/;
1006 $l = $1;
09f04786
MS
1007 test tainted($1);
1008 test tainted($l);
0b4182de
RD
1009
1010 $TAINT =~ /($notaint)/;
1011 $l = $1;
09f04786
MS
1012 test !tainted($1);
1013 test !tainted($l);
0b4182de
RD
1014 $TAINT =~ /($TAINT)/;
1015 $l = $1;
09f04786
MS
1016 test tainted($1);
1017 test tainted($l);
0b4182de
RD
1018
1019 my $r;
1020 ($r = $TAINT) =~ /($notaint)/;
09f04786 1021 test !tainted($1);
0b4182de 1022 ($r = $TAINT) =~ /($TAINT)/;
09f04786 1023 test tainted($1);
3511154c
DM
1024
1025 # [perl #24674]
1026 # accessing $^O shoudn't taint it as a side-effect;
1027 # assigning tainted data to it is now an error
1028
09f04786 1029 test !tainted($^O);
3511154c 1030 if (!$^X) { } elsif ($^O eq 'bar') { }
09f04786 1031 test !tainted($^O);
3511154c 1032 eval '$^O = $^X';
09f04786 1033 test $@ =~ /Insecure dependency in/;
0b4182de 1034}
23634c10
AL
1035
1036EFFECTIVELY_CONSTANTS: {
1037 my $tainted_number = 12 + $TAINT0;
09f04786 1038 test tainted( $tainted_number );
23634c10
AL
1039
1040 # Even though it's always 0, it's still tainted
1041 my $tainted_product = $tainted_number * 0;
09f04786
MS
1042 test tainted( $tainted_product );
1043 test $tainted_product == 0;
23634c10
AL
1044}
1045
1046TERNARY_CONDITIONALS: {
1047 my $tainted_true = $TAINT . "blah blah blah";
1048 my $tainted_false = $TAINT0;
09f04786
MS
1049 test tainted( $tainted_true );
1050 test tainted( $tainted_false );
23634c10
AL
1051
1052 my $result = $tainted_true ? "True" : "False";
09f04786
MS
1053 test $result eq "True";
1054 test !tainted( $result );
23634c10
AL
1055
1056 $result = $tainted_false ? "True" : "False";
09f04786
MS
1057 test $result eq "False";
1058 test !tainted( $result );
23634c10
AL
1059
1060 my $untainted_whatever = "The Fabulous Johnny Cash";
1061 my $tainted_whatever = "Soft Cell" . $TAINT;
1062
1063 $result = $tainted_true ? $tainted_whatever : $untainted_whatever;
09f04786
MS
1064 test $result eq "Soft Cell";
1065 test tainted( $result );
23634c10
AL
1066
1067 $result = $tainted_false ? $tainted_whatever : $untainted_whatever;
09f04786
MS
1068 test $result eq "The Fabulous Johnny Cash";
1069 test !tainted( $result );
23634c10 1070}
65814f21
MS
1071
1072{
1073 # rt.perl.org 5900 $1 remains tainted if...
1074 # 1) The regular expression contains a scalar variable AND
1075 # 2) The regular expression appears in an elsif clause
1076
1077 my $foo = "abcdefghi" . $TAINT;
1078
1079 my $valid_chars = 'a-z';
1080 if ( $foo eq '' ) {
1081 }
1082 elsif ( $foo =~ /([$valid_chars]+)/o ) {
1083 test not tainted $1;
1084 }
1085
1086 if ( $foo eq '' ) {
1087 }
1088 elsif ( my @bar = $foo =~ /([$valid_chars]+)/o ) {
1089 test not any_tainted @bar;
1090 }
1091}
0a9c116b
DM
1092
1093# at scope exit, a restored localised value should have its old
1094# taint status, not the taint status of the current statement
1095
1096{
1097 our $x99 = $^X;
1098 test tainted $x99;
1099
1100 $x99 = '';
1101 test not tainted $x99;
1102
1103 my $c = do { local $x99; $^X };
1104 test not tainted $x99;
1105}
1106{
1107 our $x99 = $^X;
1108 test tainted $x99;
1109
1110 my $c = do { local $x99; '' };
1111 test tainted $x99;
1112}
1113
27cc343c
DM
1114# an mg_get of a tainted value during localization shouldn't taint the
1115# statement
1116
1117{
1118 eval { local $0, eval '1' };
1119 test $@ eq '';
1120}
e26a4975
DM
1121
1122# [perl #8262] //g loops infinitely on tainted data
1123
1124{
1125 my @a;
c7dd0d64 1126 local $::TODO = 1;
e26a4975
DM
1127 $a[0] = $^X;
1128 my $i = 0;
1129 while($a[0]=~ m/(.)/g ) {
1130 last if $i++ > 10000;
1131 }
86f12da2 1132 cmp_ok $i, '<', 10000, "infinite m//g";
e26a4975 1133}
0aa395f8
NC
1134
1135SKIP:
1136{
1137 my $got_dualvar;
1138 eval 'use Scalar::Util "dualvar"; $got_dualvar++';
1139 skip "No Scalar::Util::dualvar" unless $got_dualvar;
1140 my $a = Scalar::Util::dualvar(3, $^X);
1141 my $b = $a + 5;
1142 is ($b, 8, "Arithmetic on tainted dualvars works");
1143}
f27977c3
MH
1144
1145# opening '|-' should not trigger $ENV{PATH} check
1146
1147{
1148 SKIP: {
1149 skip "fork() is not available", 3 unless $Config{'d_fork'};
b4fda7a3
SP
1150 skip "opening |- is not stable on threaded OpenBSD with taint", 3
1151 if $Config{useithreads} && $Is_OpenBSD;
f27977c3
MH
1152
1153 $ENV{'PATH'} = $TAINT;
1154 local $SIG{'PIPE'} = 'IGNORE';
1155 eval {
1156 my $pid = open my $pipe, '|-';
1157 if (!defined $pid) {
1158 die "open failed: $!";
1159 }
1160 if (!$pid) {
1161 kill 'KILL', $$; # child suicide
1162 }
1163 close $pipe;
1164 };
1165 test $@ !~ /Insecure \$ENV/, 'fork triggers %ENV check';
1166 test $@ eq '', 'pipe/fork/open/close failed';
1167 eval {
1168 open my $pipe, "|$Invoke_Perl -e 1";
1169 close $pipe;
1170 };
1171 test $@ =~ /Insecure \$ENV/, 'popen neglects %ENV check';
1172 }
1173}
5d121f7f
RD
1174
1175{
1176 package AUTOLOAD_TAINT;
1177 sub AUTOLOAD {
1178 our $AUTOLOAD;
1179 return if $AUTOLOAD =~ /DESTROY/;
1180 if ($AUTOLOAD =~ /untainted/) {
1181 main::ok(!main::tainted($AUTOLOAD), '$AUTOLOAD can be untainted');
1182 } else {
1183 main::ok(main::tainted($AUTOLOAD), '$AUTOLOAD can be tainted');
1184 }
1185 }
1186
1187 package main;
1188 my $o = bless [], 'AUTOLOAD_TAINT';
1189 $o->$TAINT;
1190 $o->untainted;
1191}
1192
20ee07fb
RGS
1193{
1194 # tests for tainted format in s?printf
1195 eval { printf($TAINT . "# %s\n", "foo") };
1196 like($@, qr/^Insecure dependency in printf/, q/printf doesn't like tainted formats/);
1197 eval { printf("# %s\n", $TAINT . "foo") };
1198 ok(!$@, q/printf accepts other tainted args/);
1199 eval { sprintf($TAINT . "# %s\n", "foo") };
1200 like($@, qr/^Insecure dependency in sprintf/, q/sprintf doesn't like tainted formats/);
1201 eval { sprintf("# %s\n", $TAINT . "foo") };
1202 ok(!$@, q/sprintf accepts other tainted args/);
1203}
085bde85
NC
1204
1205{
1206 # 40708
1207 my $n = 7e9;
1208 8e9 - $n;
1209
1210 my $val = $n;
1211 is ($val, '7000000000', 'Assignment to untainted variable');
1212 $val = $TAINT;
1213 $val = $n;
1214 is ($val, '7000000000', 'Assignment to tainted variable');
1215}
5e500fc8
NC
1216
1217{
5e500fc8
NC
1218 my $val = 0;
1219 my $tainted = '1' . $TAINT;
1220 eval '$val = eval $tainted;';
1221 is ($val, 0, "eval doesn't like tainted strings");
1222 like ($@, qr/^Insecure dependency in eval/);
1223
abb7fb96
NC
1224 # Rather nice code to get a tainted undef by from Rick Delaney
1225 open FH, "test.pl" or die $!;
af2d3def
RD
1226 seek FH, 0, 2 or die $!;
1227 $tainted = <FH>;
5e500fc8
NC
1228
1229 eval 'eval $tainted';
1230 like ($@, qr/^Insecure dependency in eval/);
1231}
beeaa6fd 1232
d3706118
NC
1233foreach my $ord (78, 163, 256) {
1234 # 47195
1235 my $line = 'A1' . $TAINT . chr $ord;
1236 chop $line;
1237 is($line, 'A1');
1238 $line =~ /(A\S*)/;
d3706118
NC
1239 ok(!tainted($1), "\\S match with chr $ord");
1240}
1241
beeaa6fd
NC
1242# This may bomb out with the alarm signal so keep it last
1243SKIP: {
1244 skip "No alarm()" unless $Config{d_alarm};
1245 # Test from RT #41831]
1246 # [PATCH] Bug & fix: hang when using study + taint mode (perl 5.6.1, 5.8.x)
1247
1248 my $DATA = <<'END' . $TAINT;
1249line1 is here
1250line2 is here
1251line3 is here
1252line4 is here
1253
1254END
1255
1256 #study $DATA;
1257
1258 ## don't set $SIG{ALRM}, since we'd never get to a user-level handler as
1259 ## perl is stuck in a regexp infinite loop!
1260
1261 alarm(10);
1262
1263 if ($DATA =~ /^line2.*line4/m) {
1264 fail("Should not be a match")
1265 } else {
1266 pass("Match on tainted multiline data should fail promptly");
1267 }
1268
1269 alarm(0);
1270}
1271__END__
1272# Keep the previous test last