| 1 | package Perl::Critic::Policy::ErrorHandling::RequireCheckedIndirectResults 0.001; | ||
| 2 | |||
| 3 | # ABSTRACT: Check the result of a syscall or an eval that a sub returns to you. | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings FATAL => 'all'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use re '/aa'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 0 | ▶ | use Readonly; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 10 | use List::Util qw{any first}; | ||
| 11 | use Perl::Critic::Distribution (); | ||
| 12 | use Perl::Critic::Utils qw{ :severities }; | ||
| 13 | |||
| 14 | use parent qw{Perl::Critic::Policy}; | ||
| 15 | |||
| 16 | |||
| 17 | Readonly::Scalar my $DESC => q{The result of %s, returned by a sub, is not checked}; | ||
| 18 | Readonly::Scalar my $EXPL => q{Check what the call returns, or make the sub die on failure}; | ||
| 19 | |||
| 20 | # Those of InputOutput::RequireCheckedSyscalls, which this policy mirrors. | ||
| 21 | Readonly::Array my @DEFAULT_FUNCTIONS => qw{ open close print say }; | ||
| 22 | Readonly::Array my @BUILTIN_FUNCTIONS => qw{ | ||
| 23 | accept bind binmode chdir chmod chown close closedir connect | ||
| 24 | dbmclose dbmopen exec fcntl flock fork ioctl kill link listen | ||
| 25 | mkdir msgctl msgget msgrcv msgsnd open opendir pipe print read | ||
| 26 | readdir readline readlink readpipe recv rename rmdir say seek seekdir | ||
| 27 | semctl semget semop send setpgrp setpriority setsockopt shmctl | ||
| 28 | shmget shmread shutdown sleep socket socketpair symlink syscall | ||
| 29 | sysopen sysread sysseek system syswrite tell telldir truncate | ||
| 30 | umask unlink utime wait waitpid | ||
| 31 | }; | ||
| 32 | |||
| 33 | Readonly::Hash my %RESULT => map { $_ => 1 } @BUILTIN_FUNCTIONS, 'eval'; | ||
| 34 | Readonly::Hash my %MODIFIER => map { $_ => 1 } qw{ if unless while until for foreach }; | ||
| 35 | Readonly::Hash my %SEPARATOR => map { $_ => 1 } ( q{,}, q{=>} ); | ||
| 36 | Readonly::Hash my %DIES_ITSELF => map { $_ => 1 } qw{ autodie Fatal }; | ||
| 37 | |||
| 38 | # Change it when what the collector returns changes shape. | ||
| 39 | Readonly::Scalar my $DATA_FORMAT => 1; | ||
| 40 | |||
| 41 | # The part of a name after its last ::, which is all of a bare name. | ||
| 42 | my $bare_name = sub { | ||
| 43 | my ($name) = @_; | ||
| 44 | my $at = rindex $name, '::'; | ||
| 45 | return $at < 0 ? $name : substr $name, $at + 2; | ||
| 46 | }; | ||
| 47 | |||
| 48 | |||
| 49 | sub supported_parameters { | ||
| 50 | return ( | ||
| 51 | { | ||
| 52 | name => 'functions', | ||
| 53 | description => 'The builtins whose result a sub must not return to a caller that drops it. :builtins is every one that returns a status.', | ||
| 54 | default_string => join( q{ }, @DEFAULT_FUNCTIONS ), | ||
| 55 | behavior => 'string list', | ||
| 56 | }, | ||
| 57 | { | ||
| 58 | name => 'exclude_functions', | ||
| 59 | description => 'Builtins to leave out of functions.', | ||
| 60 | default_string => q{}, | ||
| 61 | behavior => 'string list', | ||
| 62 | }, | ||
| 63 | ); | ||
| 64 | } | ||
| 65 | |||
| 66 | |||
| 67 | sub default_severity { return $SEVERITY_MEDIUM } | ||
| 68 | sub default_themes { return qw{ bugs maintenance } } | ||
| 69 | sub applies_to { return 'PPI::Document' } | ||
| 70 | |||
| 71 | |||
| 72 | sub initialize_if_enabled { | ||
| 73 | my ( $self, $config ) = @_; | ||
| 74 | |||
| 75 | my @functions = map { $_ eq ':builtins' ? @BUILTIN_FUNCTIONS : $_ } keys %{ $self->{_functions} }; | ||
| 76 | my %counted = map { ( $_ => 1 ) } @functions, 'eval'; | ||
| 77 | delete @counted{ keys %{ $self->{_exclude_functions} } }; | ||
| 78 | $self->{_counted} = \%counted; | ||
| 79 | |||
| 80 | Perl::Critic::Distribution->register( | ||
| 81 | name => __PACKAGE__, | ||
| 82 | version => join( q{/}, $DATA_FORMAT, Perl::Critic::Distribution->stamp(__FILE__) // q{} ), | ||
| 83 | collect => sub { | ||
| 84 | my ($ppi) = @_; | ||
| 85 | return { result_subs => { map { ( $_->[1] => $_->[2] ) } grep { !$_->[0]->type } result_subs_in( $ppi, packages_in($ppi) ) } }; | ||
| 86 | }, | ||
| 87 | ); | ||
| 88 | return $self->SUPER::initialize_if_enabled($config); | ||
| 89 | } | ||
| 90 | |||
| 91 | |||
| 92 | sub violates { | ||
| 93 | my ( $self, undef, $doc ) = @_; | ||
| 94 | |||
| 95 | # The subs of this file by the name that a call here uses, and those of | ||
| 96 | # the rest of the distribution by their full name, each with the builtin | ||
| 97 | # whose result it returns. | ||
| 98 | my $packages = packages_in($doc); | ||
| 99 | my %local = map { ( $_->[0]->name => $_->[2] ) } result_subs_in( $doc, $packages ); | ||
| 100 | my %remote; | ||
| 101 | my $filename = $doc->filename; | ||
| 102 | if ( my $dist = defined $filename && Perl::Critic::Distribution->for_file($filename) ) { | ||
| 103 | %remote = map { %{ $_->{result_subs} } } values %{ $dist->collected(__PACKAGE__) }; | ||
| 104 | } | ||
| 105 | my $counted = $self->{_counted}; | ||
| 106 | delete @local{ grep { !$counted->{ $local{$_} } } keys %local }; | ||
| 107 | delete @remote{ grep { !$counted->{ $remote{$_} } } keys %remote }; | ||
| 108 | return if !%local && !%remote; | ||
| 109 | |||
| 110 | # The last part of each full name, so that a word that cannot be one is | ||
| 111 | # passed over before its package is looked for. | ||
| 112 | my %bare = map { ( $bare_name->($_) => 1 ) } keys %remote; | ||
| 113 | |||
| 114 | my @violations; | ||
| 115 | foreach my $word ( @{ $doc->find('PPI::Token::Word') || [] } ) { | ||
| 116 | my $name = $word->content; | ||
| 117 | next if !$local{$name} && !$bare{ $bare_name->($name) }; | ||
| 118 | my $full = index( $name, '::' ) >= 0 ? $name : package_at( $word, $packages ) . "::$name"; | ||
| 119 | my $builtin = $local{$name} // $remote{$full} or next; | ||
| 120 | next if !is_dropped($word); | ||
| 121 | |||
| 122 | push @violations, $self->violation( sprintf( $DESC, $builtin eq 'eval' ? 'an eval block' : $builtin ), $EXPL, $word ); | ||
| 123 | } | ||
| 124 | return @violations; | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | sub result_subs_in { | ||
| 129 | my ( $ppi, $packages ) = @_; | ||
| 130 | |||
| 131 | my $dies = any { $DIES_ITSELF{ $_->module // q{} } } @{ $ppi->find('PPI::Statement::Include') || [] }; | ||
| 132 | |||
| 133 | my @found; | ||
| 134 | foreach my $sub ( @{ $ppi->find('PPI::Statement::Sub') || [] } ) { | ||
| 135 | my $block = $sub->block or next; | ||
| 136 | next if $block->find_any( sub { $_[1]->isa('PPI::Token::Word') && $_[1]->content eq 'wantarray' } ); | ||
| 137 | my $builtin = result_of($block) // next; | ||
| 138 | next if $dies && $builtin ne 'eval'; | ||
| 139 | |||
| 140 | my $name = $sub->name; | ||
| 141 | push @found, [ $sub, index( $name, '::' ) >= 0 ? $name : package_at( $sub, $packages ) . "::$name", $builtin ]; | ||
| 142 | } | ||
| 143 | return @found; | ||
| 144 | } | ||
| 145 | |||
| 146 | |||
| 147 | sub result_of { | ||
| 148 | my ($block) = @_; | ||
| 149 | |||
| 150 | # The builtin that a statement starts with, past a return when it has one. | ||
| 151 | my $leads = sub { | ||
| 152 | my ( $statement, $skip ) = @_; | ||
| 153 | my @parts = $statement->schildren; | ||
| 154 | shift @parts if $skip; | ||
| 155 | my $first = $parts[0]; | ||
| 156 | return if !$first || !$first->isa('PPI::Token::Word') || !$RESULT{ $first->content }; | ||
| 157 | return if $first->content eq 'eval' && !( $parts[1] && $parts[1]->isa('PPI::Structure::Block') ); | ||
| 158 | return $first->content; | ||
| 159 | }; | ||
| 160 | |||
| 161 | my $returns = $block->find( sub { $_[1]->isa('PPI::Statement::Break') && ( $_[1]->schild(0) // q{} ) eq 'return' } ) || []; | ||
| 162 | foreach my $return (@$returns) { | ||
| 163 | my $builtin = $leads->( $return, 1 ) // next; | ||
| 164 | |||
| 165 | # One inside an inner sub returns from that sub. | ||
| 166 | my $inner = 0; | ||
| 167 | for ( my $up = $return->parent; $up && $up != $block; $up = $up->parent ) { | ||
| 168 | $inner ||= $up->isa('PPI::Statement::Sub') || ( $up->isa('PPI::Structure::Block') && ( $up->sprevious_sibling // q{} ) eq 'sub' ); | ||
| 169 | } | ||
| 170 | return $builtin if !$inner; | ||
| 171 | } | ||
| 172 | |||
| 173 | my $last = ( grep { $_->isa('PPI::Statement') } $block->schildren )[-1]; | ||
| 174 | return if !$last || ref $last ne 'PPI::Statement'; | ||
| 175 | return $leads->( $last, 0 ); | ||
| 176 | } | ||
| 177 | |||
| 178 | |||
| 179 | sub packages_in { | ||
| 180 | my ($ppi) = @_; | ||
| 181 | return $ppi->find('PPI::Statement::Package') || []; | ||
| 182 | } | ||
| 183 | |||
| 184 | |||
| 185 | sub package_at { | ||
| 186 | my ( $elem, $packages ) = @_; | ||
| 187 | |||
| 188 | my $package = 'main'; | ||
| 189 | foreach my $statement (@$packages) { | ||
| 190 | my $block = first { $_->isa('PPI::Structure::Block') } $statement->schildren; | ||
| 191 | if ($block) { | ||
| 192 | for ( my $up = $elem->parent; $up; $up = $up->parent ) { | ||
| 193 | return $statement->namespace if $up == $block; | ||
| 194 | } | ||
| 195 | next; | ||
| 196 | } | ||
| 197 | my ( $line, $col ) = @{ $statement->location }[ 0, 1 ]; | ||
| 198 | my ( $at_line, $at_col ) = @{ $elem->location }[ 0, 1 ]; | ||
| 199 | $package = $statement->namespace if $line < $at_line || ( $line == $at_line && $col < $at_col ); | ||
| 200 | } | ||
| 201 | return $package; | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | sub is_dropped { | ||
| 206 | my ($word) = @_; | ||
| 207 | |||
| 208 | # A plain statement that starts with the call: not a declaration, a | ||
| 209 | # return, or an if. | ||
| 210 | my $statement = $word->parent; | ||
| 211 | return 0 if ref $statement ne 'PPI::Statement' || $statement->schild(0) != $word; | ||
| 212 | |||
| 213 | # Its arguments, then a postfix modifier or the end. Any other operator | ||
| 214 | # does something with the result, such as or die. | ||
| 215 | my @parts = $statement->schildren; | ||
| 216 | foreach my $part ( @parts[ 1 .. $#parts ] ) { | ||
| 217 | last if $part->isa('PPI::Token::Word') && $MODIFIER{ $part->content }; | ||
| 218 | return 0 if $part->isa('PPI::Token::Operator') && !$SEPARATOR{ $part->content }; | ||
| 219 | } | ||
| 220 | |||
| 221 | # The last statement of a block passes its value on, unless the block is | ||
| 222 | # that of an if, a loop or the like. | ||
| 223 | my $holder = $statement->parent; | ||
| 224 | return 1 if $statement->snext_sibling || !$holder->isa('PPI::Structure::Block'); | ||
| 225 | return $holder->parent && $holder->parent->isa('PPI::Statement::Compound') ? 1 : 0; | ||
| 226 | } | ||
| 227 | |||
| 228 | 1; | ||
| 229 | |||
| 230 | __END__ | ||