Fix undefined variable reference, c102
authorSteve Hancock <perltidy@users.sourceforge.net>
Sat, 6 Nov 2021 12:25:37 +0000 (05:25 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Sat, 6 Nov 2021 12:25:37 +0000 (05:25 -0700)
lib/Perl/Tidy/Formatter.pm
local-docs/BugLog.pod

index 176458970e48b5196fa516d07bb2221e4248b2a5..3c85cf3858bf5a30573d4cb0bbcdada2203c1aab 100644 (file)
@@ -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);
 
index c442a321e8a69c34d4b05d5dcbb67d7cd868a938..2b4820f16fe3a67f7f8082b4bc492220319f4f38 100644 (file)
@@ -2,6 +2,32 @@
 
 =over 4
 
+=item B<Fix undefined variable reference, c102>
+
+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<Some blocks with side comments exceed line length>
 
 In some rare cases, one-line blocks with side comments were exceeding the line