From 2db4fe8ba6fa37f511b9dbc8a661508cac8749fd Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Sat, 28 Oct 2023 18:22:53 -0700 Subject: [PATCH] check size of files arriving from stdin --- bin/perltidy | 5 ++++- lib/Perl/Tidy.pm | 20 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/bin/perltidy b/bin/perltidy index 0a3741ea..0bf970f5 100755 --- a/bin/perltidy +++ b/bin/perltidy @@ -5343,7 +5343,10 @@ MB for example would be perltidy -maxfs=20 -This only applies to files specified by filename on the command line. +This length test is applied to named files before they are read into memory. +It is applied to files arriving from standard input after they are read into +memory. It is not applied to character strings arriving by a call to the +Perl::Tidy module. B<--maximum-level-errors=n> or B<-maxle=n> specifies the maximum number of indentation level errors are allowed before perltidy skips formatting and just diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index ef043b4f..70c6e35d 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -1573,6 +1573,21 @@ sub get_decoded_string_buffer { # zero length, but keep going } + # Check size of strings arriving from the standard input. These + # could not be checked until now. + if ( $input_file eq '-' ) { + my $size_in_mb = + length( ${$rinput_string} ) / ( CONST_1024 * CONST_1024 ); + my $maximum_file_size_mb = $rOpts->{'maximum-file-size-mb'}; + if ( $size_in_mb > $maximum_file_size_mb ) { + $size_in_mb = sprintf( "%0.1f", $size_in_mb ); + Warn( +"skipping file: : size $size_in_mb MB exceeds limit $maximum_file_size_mb; use -maxfs=i to change\n" + ); + return; + } + } + $rinput_string = $self->set_line_separator($rinput_string); my $encoding_in = EMPTY_STRING; @@ -2013,10 +2028,11 @@ sub process_all_files { # files into memory, trying to process an extremely large file # could cause system problems. my $size_in_mb = ( -s $input_file ) / ( CONST_1024 * CONST_1024 ); - if ( $size_in_mb > $rOpts->{'maximum-file-size-mb'} ) { + my $maximum_file_size_mb = $rOpts->{'maximum-file-size-mb'}; + if ( $size_in_mb > $maximum_file_size_mb ) { $size_in_mb = sprintf( "%0.1f", $size_in_mb ); Warn( -"skipping file: $input_file: size $size_in_mb MB exceeds limit $rOpts->{'maximum-file-size-mb'}; use -mfs=i to change\n" +"skipping file: $input_file: size $size_in_mb MB exceeds limit $maximum_file_size_mb; use -maxfs=i to change\n" ); next; } -- 2.39.5