From c00a0586670b8d1c0db06475be9674237a6683d8 Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Thu, 5 Oct 2023 07:40:49 -0700 Subject: [PATCH] check on trimming zero length input stream in tokenizer (c286) --- lib/Perl/Tidy/Tokenizer.pm | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/Perl/Tidy/Tokenizer.pm b/lib/Perl/Tidy/Tokenizer.pm index 6b0ae8cb..98aa41ae 100644 --- a/lib/Perl/Tidy/Tokenizer.pm +++ b/lib/Perl/Tidy/Tokenizer.pm @@ -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; -- 2.39.5