From 4cb63b6ed66c5635c7b75fb33175a35bde32eaee Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Sat, 6 Nov 2021 05:25:37 -0700 Subject: [PATCH] Fix undefined variable reference, c102 --- lib/Perl/Tidy/Formatter.pm | 8 ++++++-- local-docs/BugLog.pod | 26 ++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/lib/Perl/Tidy/Formatter.pm b/lib/Perl/Tidy/Formatter.pm index 17645897..3c85cf38 100644 --- a/lib/Perl/Tidy/Formatter.pm +++ b/lib/Perl/Tidy/Formatter.pm @@ -17491,7 +17491,9 @@ EOM # prepare for a new list when depth increases # token $i is a '(','{', or '[' #------------------------------------------------------------ - if ( $depth > $current_depth ) { + # hardened against bad input syntax: depth jump must be 1 and type + # must be opening..fixes c102 + if ( $depth == $current_depth + 1 && $is_opening_type{$type} ) { $type_sequence_stack[$depth] = $type_sequence; $override_cab3[$depth] = @@ -17578,7 +17580,9 @@ EOM # finish off any old list when depth decreases # token $i is a ')','}', or ']' #------------------------------------------------------------ - elsif ( $depth < $current_depth ) { + # hardened against bad input syntax: depth jump must be 1 and type + # must be closing .. fixes c102 + elsif ( $depth == $current_depth - 1 && $is_closing_type{$type} ) { check_for_new_minimum_depth($depth); diff --git a/local-docs/BugLog.pod b/local-docs/BugLog.pod index c442a321..2b4820f1 100644 --- a/local-docs/BugLog.pod +++ b/local-docs/BugLog.pod @@ -2,6 +2,32 @@ =over 4 +=item B + +Random testing produced an undefined variable reference for the following input + + make_sorter ( sort_sha => sub {sha512 ( $_} ); + make_sorter ( sort_ids => sub {/^ID:(\d+)/} ); + +when formatted with the following input parameters: + + --space-function-paren + --maximum-line-length=26 + --noadd-newlines + +Notice that the input script has a peculiar syntax error - the last two closing +tokens of the first line are transposed. (Ironically, this snippet is taken +from code which accompanied the book Perl Best Practices). The perltidy +tokenizer caught the syntax error, but the formatter touched an undefined +variable while attempting to do the formatting. It would be possible to just +skip formatting for errors like this, but it can sometimes help finding bugs to +see an attempted formatting. So the formatter coding has been corrected to +avoid the undefined variable reference. + +This fixes issue c102. + +5 Nov 2021. + =item B In some rare cases, one-line blocks with side comments were exceeding the line -- 2.39.5