From 8dee6dcc55f635e58e0a5ad8b29329a33b33dfc5 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sun, 29 Aug 2021 01:32:46 +0000 Subject: [PATCH] Filesystem API: Make sure to only call `fread()` on non-empty files in `PclZip::privAddFile()`. This avoids a fatal error on PHP 8 caused by passing a zero value to `fread()` as the `$length` argument, which must be greater than zero. This commit also amends the previous solution for similar issues elsewhere in the file to ensure consistent type for string values, instead of changing the type from `string` to `bool` when trying to read from an empty file. Follow-up to [50355]. Props DavidAnderson, jrf, SergeyBiryukov. Fixes #54036. git-svn-id: https://develop.svn.wordpress.org/trunk@51686 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/class-pclzip.php | 27 +++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/src/wp-admin/includes/class-pclzip.php b/src/wp-admin/includes/class-pclzip.php index b8f2fdca86..b1b39e2dfe 100644 --- a/src/wp-admin/includes/class-pclzip.php +++ b/src/wp-admin/includes/class-pclzip.php @@ -2674,7 +2674,12 @@ } // ----- Read the file content - $v_content = @fread($v_file, $p_header['size']); + if ($p_header['size'] > 0) { + $v_content = @fread($v_file, $p_header['size']); + } + else { + $v_content = ''; + } // ----- Close the file @fclose($v_file); @@ -3884,11 +3889,11 @@ // ----- Read the compressed file in a buffer (one shot) - if ( $p_entry['compressed_size'] > 0 ) { + if ($p_entry['compressed_size'] > 0) { $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']); } else { - $v_buffer = false; + $v_buffer = ''; } // ----- Decompress the file @@ -4101,11 +4106,11 @@ if ($p_entry['compressed_size'] == $p_entry['size']) { // ----- Read the file in a buffer (one shot) - if ( $p_entry['compressed_size'] > 0 ) { + if ($p_entry['compressed_size'] > 0) { $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']); } else { - $v_buffer = false; + $v_buffer = ''; } // ----- Send the file to the output @@ -4115,11 +4120,11 @@ else { // ----- Read the compressed file in a buffer (one shot) - if ( $p_entry['compressed_size'] > 0 ) { + if ($p_entry['compressed_size'] > 0) { $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']); } else { - $v_buffer = false; + $v_buffer = ''; } // ----- Decompress the file @@ -4224,21 +4229,21 @@ if ($p_entry['compression'] == 0) { // ----- Reading the file - if ( $p_entry['compressed_size'] > 0 ) { + if ($p_entry['compressed_size'] > 0) { $p_string = @fread($this->zip_fd, $p_entry['compressed_size']); } else { - $p_string = false; + $p_string = ''; } } else { // ----- Reading the file - if ( $p_entry['compressed_size'] > 0 ) { + if ($p_entry['compressed_size'] > 0) { $v_data = @fread($this->zip_fd, $p_entry['compressed_size']); } else { - $v_data = false; + $v_data = ''; } // ----- Decompress the file