]> git.donarmstrong.com Git - perltidy.git/commitdiff
check on trimming zero length input stream in tokenizer (c286)
authorSteve Hancock <perltidy@users.sourceforge.net>
Thu, 5 Oct 2023 14:40:49 +0000 (07:40 -0700)
committerSteve Hancock <perltidy@users.sourceforge.net>
Thu, 5 Oct 2023 14:40:49 +0000 (07:40 -0700)
lib/Perl/Tidy/Tokenizer.pm

index 6b0ae8cb2110332d58bcc53270dd22afbc7bb091..98aa41aea1bfa04e11ce2de56e11fa5326a90c41 100644 (file)
@@ -656,8 +656,12 @@ EOM
     # Get trimmed lines. It is much faster to strip leading whitespace from
     # the whole input file at once than line-by-line.
 
-    # Add a terminal newline if needed to keep line count unchanged (c283)
-    if ( $source_string && $source_string !~ /\n$/ ) { $source_string .= "\n" }
+    # Add a terminal newline if needed to keep line count unchanged:
+    # - avoids problem of losing a last line which is just \r and no \n  (c283)
+    # - but check input line count to avoid adding line to an empty file (c286)
+    if ( @{$rinput_lines} && $source_string !~ /\n$/ ) {
+        $source_string .= "\n";
+    }
 
     # Remove leading whitespace except newlines
     $source_string =~ s/^ [^\S\n]+ //gxm;