Simple/IO.pm

filter: Statementstmt 94.9% 112 / 118 Branchbran 58.0% 36 / 62 Conditioncond 41.0% 16 / 39 MC/DCmcdc 4.0% 1 / 25 Subroutinesub 94.4% 17 / 18 Podpod 100% 7 / 7 totaltotal 70.2% 189 / 269 CC 47 SCAR 23.2
CC
47
Cov
75%
_handle_looks_safe
23.9
slurp
13.0
validate
9.2
CRAP 10.2
« Simple/Base/Compiler.pm ↑ Summary Simple/Compiler/Safe.pm »
1 package Text::Template::Simple::IO;
2 32 $Text::Template::Simple::IO::VERSION = '0.91';
3 32 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 32 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 32 ▶use constant MY_IO_LAYER => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 32 ▶use constant MY_INCLUDE_PATHS => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 32 ▶use constant MY_TAINT_MODE => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 32 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 32 ▶use Text::Template::Simple::Constants qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 32 ▶use Text::Template::Simple::Util qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
12 binary_mode
13 fatal
14 DEBUG
15 LOG
16 32 );
17  
18 45 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
19 45 my $class = shift;
20 45 my $layer = shift;
21 45 my $paths = shift;
22 45 my $tmode = shift;
23 45 my $self = [ undef, undef, undef ];
24 45 bless $self, $class;
25 45 ▶ $self->[MY_IO_LAYER] = $layer if defined $layer;
Branch: 1 branch
BranchTrueFalseTotal
if defined $layer 45 0 45
26 45 ▶ $self->[MY_INCLUDE_PATHS] = [ @{ $paths } ] if $paths; # copy
Branch: 1 branch
BranchTrueFalseTotal
if $paths 45 0 45
27 45 $self->[MY_TAINT_MODE] = $tmode;
28 45 return $self;
29 }
30  
31 2 ▶sub validate {
Subroutine validate: called CC=7 SCAR=22.2
Pod documented
32 2 my $self = shift;
33 2 ▶ my $type = shift || fatal('tts.io.validate.type');
Condition33%1/3
shift() || fatal('tts.io.validate.type')
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() || fatal('tts.io.validate.type')
shift() fatal('tts.io.validate.type')
Truth table33%1/3
A || B
A= shift()B= fatal('tts.io.validate.type')
ABresult
1X1
011
000
34 2 ▶ my $path = shift || fatal('tts.io.validate.path');
Condition33%1/3
shift() || fatal('tts.io.validate.path')
l!l&&r!l&&!r
200
MC/DC0%0/2
shift() || fatal('tts.io.validate.path')
shift() fatal('tts.io.validate.path')
Truth table33%1/3
A || B
A= shift()B= fatal('tts.io.validate.path')
ABresult
1X1
011
000
35  
36 2 ▶ if ( $type eq 'dir' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "dir") 2 0 2
37 2 require File::Spec;
38 2 $path = File::Spec->canonpath( $path );
39 2 my $wdir;
40  
41 2 if ( IS_WINDOWS ) {
42 $wdir = Win32::GetFullPathName( $path );
43 if( Win32::GetLastError() ) {
44 LOG( FAIL => "Win32::GetFullPathName( $path ): $^E" ) if DEBUG;
45 $wdir = EMPTY_STRING; # die "Win32::GetFullPathName: $^E";
46 }
47 else {
48 my $ok = -e $wdir && -d _;
49 $wdir = EMPTY_STRING if not $ok;
50 }
51 }
52  
53 2 ▶ $path = $wdir if $wdir;
Branch: 1 branch
BranchTrueFalseTotal
if $wdir 0 2 2
54 2 ▶ my $ok = -e $path && -d _;
Condition33%1/3
-e $path && -d _
!ll&&!rl&&r
002
MC/DC0%0/2
-e $path && -d _
-e $path -d _
Truth table33%1/3
A && B
A= -e $pathB= -d _
ABresult
0X0
100
111
55 2 ▶ return if not $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 2 2
56 2 return $path;
57 }
58  
59 0 return fatal('tts.io.validate.file');
60 }
61  
62 417 ▶sub layer {
Subroutine layer: called CC=3 SCAR=11.2
Pod documented
63 417 return if ! UNICODE_PERL;
64 417 my $self = shift;
65 417 ▶ my $fh = shift || fatal('tts.io.layer.fh');
Condition33%1/3
shift() || fatal('tts.io.layer.fh')
l!l&&r!l&&!r
41700
MC/DC0%0/2
shift() || fatal('tts.io.layer.fh')
shift() fatal('tts.io.layer.fh')
Truth table33%1/3
A || B
A= shift()B= fatal('tts.io.layer.fh')
ABresult
1X1
011
000
66 417 my $layer = $self->[MY_IO_LAYER];
67 417 ▶ binary_mode( $fh, $layer ) if $layer;
Branch: 1 branch
BranchTrueFalseTotal
if $layer 1 416 417
68 417 return;
69 }
70  
71 416 ▶sub slurp {
Subroutine slurp: called CC=11 SCAR=25.7
Pod documented
72 416 require IO::File;
73 416 require Fcntl;
74 416 my $self = shift;
75 416 my $file = shift;
76 416 my($fh, $seek);
77  
78 416 ▶ LOG(IO_SLURP => $file) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 416 416
79  
80 416 ▶ if ( ref $file && fileno $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $file and fileno $file) { } 0 416 416
Condition33%1/3
ref $file and fileno $file
!ll&&!rl&&r
41600
MC/DC0%0/2
ref $file and fileno $file
ref $file fileno $file
Truth table33%1/3
A and B
A= ref $fileB= fileno $file
ABresult
0X0
100
111
81 0 $fh = $file;
82 0 $seek = 1;
83 }
84 else {
85 416 $fh = IO::File->new;
86 416 ▶ $fh->open($file, 'r') or fatal('tts.io.slurp.open', $file, $!);
Branch: 1 branch
BranchTrueFalseTotal
$fh->open($file, "r") or fatal("tts.io.slurp.open", $file, $!) 414 2 416
87 }
88  
89 414 flock $fh, Fcntl::LOCK_SH();
90 414 ▶ seek $fh, 0, Fcntl::SEEK_SET() if $seek;
Branch: 1 branch
BranchTrueFalseTotal
if $seek 0 414 414
91 414 ▶ $self->layer( $fh ) if ! $seek; # apply the layer only if we opened this
Branch: 1 branch
BranchTrueFalseTotal
unless $seek 414 0 414
92  
93 414 ▶ if ( $self->_handle_looks_safe( $fh ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_handle_looks_safe($fh)) 414 0 414
94 414 require IO::Handle;
95 414 my $rv = IO::Handle::untaint( $fh );
96 414 ▶ fatal('tts.io.slurp.taint') if $rv != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $rv != 0 0 414 414
97 }
98  
99 414 my $tmp = do { local $/; my $rv = <$fh>; $rv };
100 414 flock $fh, Fcntl::LOCK_UN();
101 414 ▶ if ( ! $seek ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($seek) 414 0 414
102 # close only if we opened this
103 414 ▶ close $fh or die "Unable to close filehandle: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "Unable to close filehandle: $!\n" 414 0 414
104 }
105 414 return $tmp;
106 }
107  
108 414 ▶sub _handle_looks_safe {
Subroutine _handle_looks_safe: called CC=14 SCAR=31.7
109 # Cargo Culting: original taint checking code was taken from "The Camel"
110 414 my $self = shift;
111 414 my $fh = shift;
112 414 ▶ fatal('tts.io.hls.invalid') if ! $fh || ! fileno $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless $fh and fileno $fh 0 414 414
Condition33%1/3
$fh and fileno $fh
!ll&&!rl&&r
00414
MC/DC0%0/2
$fh and fileno $fh
$fh fileno $fh
Truth table33%1/3
A and B
A= $fhB= fileno $fh
ABresult
0X0
100
111
113  
114 414 require File::stat;
115 414 my $i = File::stat::stat( $fh );
116 414 ▶ return if ! $i;
Branch: 1 branch
BranchTrueFalseTotal
unless $i 0 414 414
117  
118 414 my $tmode = $self->[MY_TAINT_MODE];
119  
120 # ignore this check if the user is root
121 # can happen with cpan clients
122 414 ▶ if ( $< != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($< != 0) 414 0 414
123 # owner neither superuser nor "me", whose
124 # real uid is in the $< variable
125 414 ▶ return if $i->uid != 0 && $i->uid != $<;
Branch: 1 branch
BranchTrueFalseTotal
if $i->uid != 0 and $i->uid != $< 0 414 414
Condition33%1/3
$i->uid != 0 and $i->uid != $<
!ll&&!rl&&r
04140
MC/DC0%0/2
$i->uid != 0 and $i->uid != $<
$i->uid != 0 $i->uid != $<
Truth table33%1/3
A and B
A= $i->uid != 0B= $i->uid != $<
ABresult
0X0
100
111
126 }
127  
128 # Check whether group or other can write file.
129 # Read check is disabled by default
130 # Mode is always 0666 on Windows, so all tests below are disabled on Windows
131 # unless you force them to run
132 414 ▶ LOG( FILE_MODE => sprintf '%04o', $i->mode & FTYPE_MASK) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 414 414
133  
134 414 my $bypass = IS_WINDOWS && ! ( $tmode & TAINT_CHECK_WINDOWS ) ? 1 : 0;
135 414 ▶ my $go_write = $bypass ? 0 : $i->mode & FMODE_GO_WRITABLE;
Branch: 1 branch
BranchTrueFalseTotal
$bypass ? : 0 414 414
136 414 ▶ my $go_read = ! $bypass && ( $tmode & TAINT_CHECK_FH_READ )
Branch: 1 branch
BranchTrueFalseTotal
!$bypass && $tmode & 8 ? : 0 414 414
Condition33%1/3
not $bypass and $tmode & 8
!ll&&!rl&&r
04140
MC/DC0%0/2
not $bypass and $tmode & 8
not $bypass $tmode & 8
Truth table33%1/3
not(A) and B
A= not $bypassB= $tmode & 8
ABresult
0X0
100
111
137 ? $i->mode & FMODE_GO_READABLE
138 : 0;
139  
140 414 ▶ LOG( TAINT => "tmode:$tmode; bypass:$bypass; "
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 414 414
141 ."go_write:$go_write; go_read:$go_read") if DEBUG;
142  
143 414 ▶ return if $go_write || $go_read;
Branch: 1 branch
BranchTrueFalseTotal
if $go_write or $go_read 0 414 414
Condition33%1/3
$go_write or $go_read
l!l&&r!l&&!r
00414
MC/DC0%0/2
$go_write or $go_read
$go_write $go_read
Truth table33%1/3
A or B
A= $go_writeB= $go_read
ABresult
1X1
011
000
144 414 return 1;
145 }
146  
147 671 ▶sub is_file {
Subroutine is_file: called CC=3 SCAR=11.4
Pod documented
148 # safer than a simple "-e"
149 671 my $self = shift;
150 671 ▶ my $file = shift || return;
Condition50%1/2
shift() || (return)
l!l
6710
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
151 671 ▶ return $self->_looks_like_file( $file ) && ! -d $file;
Condition66%2/3
$self->_looks_like_file($file) && !(-d $file)
!ll&&!rl&&r
1230548
MC/DC50%1/2
$self->_looks_like_file($file) && !(-d $file)
$self->_looks_like_file($file) !(-d $file)
Truth table66%2/3
A && not(B)
A= $self->_looks_like_file($file)B= !(-d $file)
ABresult
0X0
100
111
152 }
153  
154 136 ▶sub is_dir {
Subroutine is_dir: called CC=3 SCAR=11.4
Pod documented
155 # safer than a simple "-d"
156 136 my $self = shift;
157 136 ▶ my $file = shift || return;
Condition50%1/2
shift() || (return)
l!l
1360
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
158 136 ▶ return $self->_looks_like_file( $file ) && -d $file;
Condition66%2/3
$self->_looks_like_file($file) && -d $file
!ll&&!rl&&r
91270
MC/DC0%0/2
$self->_looks_like_file($file) && -d $file
$self->_looks_like_file($file) -d $file
Truth table66%2/3
A && B
A= $self->_looks_like_file($file)B= -d $file
ABresult
0X0
100
111
159 }
160  
161 665 ▶sub file_exists {
Subroutine file_exists: called CC=4 SCAR=13.9
Pod documented
162 665 my $self = shift;
163 665 my $file = shift;
164  
165 665 ▶ return $file if $self->is_file( $file );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_file($file) 544 121 665
166  
167 121 foreach my $path ( @{ $self->[MY_INCLUDE_PATHS] } ) {
168 6 my $test = File::Spec->catfile( $path, $file );
169 6 ▶ return $test if $self->is_file( $test );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_file($test) 4 2 6
170 }
171  
172 117 return; # fail!
173 }
174  
175 807 ▶sub _looks_like_file {
Subroutine _looks_like_file: called CC=6 SCAR=18.6
176 807 my $self = shift;
177 807 ▶ my $file = shift || return;
Condition50%1/2
shift() || (return)
l!l
8070
MC/DC0%0/1
shift() || (return)
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
178 807 ▶ return ref $file ? 0
Branch: 4 branches
BranchTrueFalseTotal
ref $file ? : 0 807 807
$file =~ / [ \n \r < > * ? ] /msx ? : 122 685 807
length $file >= 255 ? : 0 685 685
-e $file ? : 675 10 685
179 : $file =~ RE_NONFILE ? 0
180 : length $file >= MAX_PATH_LENGTH ? 0
181 : -e $file ? 1
182 : 0
183 ;
184 }
185  
186 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
187 0 my $self = shift;
188 0 ▶ LOG( DESTROY => ref $self ) if DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if DEBUG() 0 0 0
189 0 return;
190 }
191  
192 32 1;
193  
194 __END__
« Simple/Base/Compiler.pm ↑ Summary Simple/Compiler/Safe.pm »