From: Steve Hancock Date: Sat, 18 Jun 2022 15:37:18 +0000 (-0700) Subject: check all eval return values X-Git-Tag: 20220613.01~35 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=4a26fe5c9687671991a7a6a51fa4f2361bc9e0a2;p=perltidy.git check all eval return values --- diff --git a/.perlcriticrc b/.perlcriticrc index 98ae2e0a..a68dc59a 100644 --- a/.perlcriticrc +++ b/.perlcriticrc @@ -11,8 +11,10 @@ verbose = %f: [%p] %m at line %l, column %c.\n # Example command to run a single policy on single module: # perlcritic --single-policy Subroutines::ProhibitSubroutinePrototypes Module.pm -# Below is a list of policies that customize perlcritic to the needs of -# Perl::Tidy. +# Below is a list of policies that are skipped or customized to the needs of +# Perl::Tidy. After experimenting with ## no critic comments, I decided that +# they cause more trouble than the value they provide, so policies are either +# 'on' or 'off'. #-------------------------------------------------------------- # Following is a list of policies to be skipped for severity=4: @@ -27,7 +29,7 @@ verbose = %f: [%p] %m at line %l, column %c.\n # There is a stringy eval in Formatter.pm and Tokenizer.pm which is essential # for checking user input. So we have to skip this. -[-BuiltinFunctions::ProhibitStringyEval] +[-BuiltinFunctions::ProhibitStringyEval] # Tidy.pm exports 'perltidy'. Changing this could break existing scripts. [-Modules::ProhibitAutomaticExportation] @@ -82,9 +84,6 @@ max_nests=11 # Agree - these are in process of being converted to if's [-ControlStructures::ProhibitNegativeExpressionsInUnlessAndUntilConditions] -# Agree - checks are in process of being checked -[-ErrorHandling::RequireCheckingReturnValueOfEval] - # This is a good general policy but not always the best for efficiency [-Subroutines::ProhibitManyArgs] diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 3fb419ff..d70b4ec5 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -4533,8 +4533,8 @@ sub bad_pattern { # but it should be safe because the pattern has been constructed # by this program. my ($pattern) = @_; - eval "'##'=~/$pattern/"; - return $EVAL_ERROR; + my $ok = eval "'##'=~/$pattern/"; + return !defined($ok) || $EVAL_ERROR; } { ## begin closure prepare_cuddled_block_types diff --git a/lib/Perl/Tidy/LineSource.pm b/lib/Perl/Tidy/LineSource.pm index 9c56ea7c..288beb58 100644 --- a/lib/Perl/Tidy/LineSource.pm +++ b/lib/Perl/Tidy/LineSource.pm @@ -8,8 +8,11 @@ package Perl::Tidy::LineSource; use strict; use warnings; +use English qw( -no_match_vars ); our $VERSION = '20220613'; +use constant DEVEL_MODE => 0; + sub AUTOLOAD { # Catch any undefined sub calls so that we are sure to get @@ -86,7 +89,10 @@ sub close_input_file { # Only close physical files, not STDIN and other objects my $filename = $self->{_filename}; if ( $filename ne '-' && !ref $filename ) { - eval { $self->{_fh}->close() }; + my $ok = eval { $self->{_fh}->close(); 1 }; + if ( !$ok && DEVEL_MODE ) { + Fault("Could not close file handle(): $EVAL_ERROR\n"); + } } return; } diff --git a/lib/Perl/Tidy/Logger.pm b/lib/Perl/Tidy/Logger.pm index b005abd9..93e54b3d 100644 --- a/lib/Perl/Tidy/Logger.pm +++ b/lib/Perl/Tidy/Logger.pm @@ -10,6 +10,7 @@ use warnings; our $VERSION = '20220613'; use English qw( -no_match_vars ); +use constant DEVEL_MODE => 0; use constant EMPTY_STRING => q{}; use constant SPACE => q{ }; @@ -496,11 +497,13 @@ sub finish { my $routput_array = $self->{_output_array}; foreach my $line ( @{$routput_array} ) { $fh->print($line) } if ( $log_file ne '-' && !ref $log_file ) { - eval { $fh->close() }; + my $ok = eval { $fh->close(); 1 }; + if ( !$ok && DEVEL_MODE ) { + Fault("Could not close file handle(): $EVAL_ERROR\n"); + } } } } return; } 1; - diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm index ba87a97a..894bb70a 100644 --- a/lib/Perl/Tidy/Tokenizer.pm +++ b/lib/Perl/Tidy/Tokenizer.pm @@ -281,8 +281,8 @@ sub bad_pattern { # but it should be safe because the pattern has been constructed # by this program. my ($pattern) = @_; - eval "'##'=~/$pattern/"; - return $EVAL_ERROR; + my $ok = eval "'##'=~/$pattern/"; + return !defined($ok) || $EVAL_ERROR; } sub make_code_skipping_pattern {