« blib/lib/Perl/Lint/Policy/ControlStructures/ProhibitPostfixControls.pm
↑ Summary
blib/lib/Perl/Lint/Policy/Modules/ProhibitEvilModules.pm »
| 1 | package Perl::Lint::Policy::InputOutput::RequireCheckedSyscalls; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 1 | ▶ | use List::Util qw/any/; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use Perl::Lint::Constants::Type; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Perl::Lint::Constants::Kind; | ||
| 7 | use B::Keywords; | ||
| 8 | use parent "Perl::Lint::Policy"; | ||
| 9 | |||
| 10 | use constant { | ||
| 11 | DESC => 'Return value of flagged function ignored', | ||
| 12 | EXPL => [208, 278], | ||
| 13 | }; | ||
| 14 | |||
| 15 | sub evaluate { | ||
| 16 | my ($class, $file, $tokens, $src, $args) = @_; | ||
| 17 | |||
| 18 | my $is_target_all = 0; | ||
| 19 | my @target_functions = qw/open close say/; | ||
| 20 | my $allowed_functions; | ||
| 21 | if (my $required_checked_syscalls_arg = $args->{require_checked_syscalls}) { | ||
| 22 | if (my $functions = $required_checked_syscalls_arg->{functions}) { | ||
| 23 | if ($functions eq ':builtins') { | ||
| 24 | @target_functions = @B::Keywords::Functions; | ||
| 25 | } | ||
| 26 | elsif ($functions eq ':all') { | ||
| 27 | $is_target_all = 1; | ||
| 28 | } | ||
| 29 | else { | ||
| 30 | @target_functions = ($functions); | ||
| 31 | } | ||
| 32 | } | ||
| 33 | |||
| 34 | if ($allowed_functions = $required_checked_syscalls_arg->{exclude_functions}) { | ||
| 35 | @target_functions = grep {$_ ne $allowed_functions} @target_functions; | ||
| 36 | } | ||
| 37 | } | ||
| 38 | |||
| 39 | my $is_in_assign_context = 0; | ||
| 40 | my $is_in_statement_context = 0; | ||
| 41 | my $is_called_syscalls_in_void = 0; | ||
| 42 | my $is_enabled_autodie = 0; | ||
| 43 | my @violations; | ||
| 44 | for (my $i = 0; my $token = $tokens->[$i]; $i++) { | ||
| 45 | my $token_type = $token->{type}; | ||
| 46 | my $token_kind = $token->{kind}; | ||
| 47 | my $token_data = $token->{data}; | ||
| 48 | |||
| 49 | if ($token_type == ASSIGN) { | ||
| 50 | $is_in_assign_context = 1; | ||
| 51 | next; | ||
| 52 | } | ||
| 53 | |||
| 54 | if ($token_type == USED_NAME) { | ||
| 55 | if ($token_data eq 'Fatal') { | ||
| 56 | my $next_token = $tokens->[$i+1]; | ||
| 57 | my $next_token_type = $next_token->{type}; | ||
| 58 | my $next_token_data = $next_token->{data}; | ||
| 59 | if ($next_token_type == REG_LIST) { | ||
| 60 | for ($i += 3; my $token = $tokens->[$i]; $i++) { | ||
| 61 | my $token_type = $token->{type}; | ||
| 62 | my $token_data = $token->{data}; | ||
| 63 | if ($token_type == REG_EXP) { | ||
| 64 | @target_functions = grep {$_ ne $token_data} @target_functions | ||
| 65 | } | ||
| 66 | elsif ($token_type == REG_DELIM) { | ||
| 67 | last; | ||
| 68 | } | ||
| 69 | } | ||
| 70 | } | ||
| 71 | elsif ($next_token_type == LEFT_PAREN) { | ||
| 72 | my $left_paren_num = 1; | ||
| 73 | for ($i += 2; my $token = $tokens->[$i]; $i++) { | ||
| 74 | my $token_type = $token->{type}; | ||
| 75 | my $token_data = $token->{data}; | ||
| 76 | if ($token_type == LEFT_PAREN) { | ||
| 77 | $left_paren_num++; | ||
| 78 | } | ||
| 79 | elsif (($token_type == STRING || $token_type == RAW_STRING) && any {$_ eq $token_data} @target_functions) { | ||
| 80 | return []; | ||
| 81 | } | ||
| 82 | else { | ||
| 83 | last if --$left_paren_num <= 0; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | elsif (($next_token_type == STRING || $next_token_type == RAW_STRING) && any {$_ eq $next_token_data} @target_functions) { | ||
| 88 | last; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | elsif ($token_data eq 'autodie') { | ||
| 92 | if ($tokens->[$i+1]->{type} == REG_LIST) { | ||
| 93 | for ($i += 3; my $token = $tokens->[$i]; $i++) { | ||
| 94 | my $token_type = $token->{type}; | ||
| 95 | if ($token_type == REG_EXP && $token->{data} =~ /\A\s*:io\s*\Z/) { | ||
| 96 | $is_enabled_autodie = 1; | ||
| 97 | } | ||
| 98 | elsif ($token_type == REG_DELIM) { | ||
| 99 | last; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | } | ||
| 103 | else { | ||
| 104 | $is_enabled_autodie = 1; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | |||
| 108 | next; | ||
| 109 | } | ||
| 110 | |||
| 111 | if ($token_type == NAMESPACE && $token_data eq 'Fatal') { | ||
| 112 | my $skipped_token = $tokens->[$i+2]; | ||
| 113 | if ($skipped_token && $skipped_token->{type} == NAMESPACE && $skipped_token->{data} eq 'Exception') { | ||
| 114 | for ($i += 3; my $token = $tokens->[$i]; $i++) { | ||
| 115 | my $token_type = $token->{type}; | ||
| 116 | my $token_data = $token->{data}; | ||
| 117 | if ($token_type == REG_LIST) { | ||
| 118 | for ($i += 2; my $token = $tokens->[$i]; $i++) { | ||
| 119 | my $token_type = $token->{type}; | ||
| 120 | my $token_data = $token->{data}; | ||
| 121 | if ($token_type == REG_EXP) { | ||
| 122 | @target_functions = grep {$_ ne $token_data} @target_functions | ||
| 123 | } | ||
| 124 | elsif ($token_type == REG_DELIM) { | ||
| 125 | last; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | elsif ($token_type == LEFT_PAREN) { | ||
| 130 | my $left_paren_num = 1; | ||
| 131 | for ($i++; my $token = $tokens->[$i]; $i++) { | ||
| 132 | my $token_type = $token->{type}; | ||
| 133 | my $token_data = $token->{data}; | ||
| 134 | if ($token_type == LEFT_PAREN) { | ||
| 135 | $left_paren_num++; | ||
| 136 | } | ||
| 137 | elsif (($token_type == STRING || $token_type == RAW_STRING) && any {$_ eq $token_data} @target_functions) { | ||
| 138 | return []; | ||
| 139 | } | ||
| 140 | else { | ||
| 141 | last if --$left_paren_num <= 0; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } | ||
| 145 | elsif (($token_type == STRING || $token_type == RAW_STRING) && any {$_ eq $token_data} @target_functions) { | ||
| 146 | last; | ||
| 147 | } | ||
| 148 | elsif ($token->{kind} == KIND_STMT_END) { | ||
| 149 | last; | ||
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | next; | ||
| 155 | } | ||
| 156 | |||
| 157 | if ($token_kind == KIND_STMT) { | ||
| 158 | $is_in_statement_context = 1; | ||
| 159 | |||
| 160 | if ($tokens->[$i+1]->{type} == LEFT_PAREN) { | ||
| 161 | $i++; | ||
| 162 | my $left_paren_num = 1; | ||
| 163 | for ($i++; my $token = $tokens->[$i]; $i++) { | ||
| 164 | my $token_type = $token->{type}; | ||
| 165 | if ($token_type == LEFT_PAREN) { | ||
| 166 | $left_paren_num++; | ||
| 167 | } | ||
| 168 | else { | ||
| 169 | last if --$left_paren_num <= 0; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
| 173 | next; | ||
| 174 | } | ||
| 175 | |||
| 176 | if ($token_type == BUILTIN_FUNC || ($is_target_all && $token_type == RETURN)) { | ||
| 177 | if ($is_target_all || any {$_ eq $token_data} @target_functions) { | ||
| 178 | if (!$is_in_assign_context && !$is_in_statement_context) { | ||
| 179 | $is_called_syscalls_in_void = 1; | ||
| 180 | } | ||
| 181 | } | ||
| 182 | elsif ($token_data eq 'no') { | ||
| 183 | my $next_token = $tokens->[++$i]; | ||
| 184 | if ($next_token->{type} == KEY && $next_token->{data} eq 'autodie') { | ||
| 185 | $is_enabled_autodie = 0; | ||
| 186 | } | ||
| 187 | } | ||
| 188 | next; | ||
| 189 | } | ||
| 190 | |||
| 191 | if ( | ||
| 192 | $is_target_all && | ||
| 193 | ($token_type == CALL || ($token_type == KEY && $tokens->[++$i]->{type} == LEFT_PAREN)) | ||
| 194 | ) { | ||
| 195 | if ($allowed_functions && $token_data =~ /\A$allowed_functions\s*\(?/) { | ||
| 196 | next; | ||
| 197 | } | ||
| 198 | elsif (!$is_in_assign_context && !$is_in_statement_context) { | ||
| 199 | $is_called_syscalls_in_void = 1; | ||
| 200 | } | ||
| 201 | next; | ||
| 202 | } | ||
| 203 | |||
| 204 | if ($token_kind == KIND_OP) { | ||
| 205 | $is_in_statement_context = 1; | ||
| 206 | $is_called_syscalls_in_void = 0; | ||
| 207 | next; | ||
| 208 | } | ||
| 209 | |||
| 210 | if ($token_kind == KIND_STMT_END) { | ||
| 211 | next if $is_enabled_autodie; | ||
| 212 | |||
| 213 | if ($is_called_syscalls_in_void) { | ||
| 214 | push @violations, { | ||
| 215 | filename => $file, | ||
| 216 | line => $token->{line}, | ||
| 217 | description => DESC, | ||
| 218 | explanation => EXPL, | ||
| 219 | policy => __PACKAGE__, | ||
| 220 | }; | ||
| 221 | } | ||
| 222 | |||
| 223 | $is_in_assign_context = 0; | ||
| 224 | $is_in_statement_context = 0; | ||
| 225 | $is_called_syscalls_in_void = 0; | ||
| 226 | next; | ||
| 227 | } | ||
| 228 | |||
| 229 | } | ||
| 230 | |||
| 231 | return \@violations; | ||
| 232 | } | ||
| 233 | |||
| 234 | 1; | ||
| 235 | |||