From 563ea09789613bcfc78ca0a16e89a85e81774cee Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 16 Feb 2021 18:40:29 +0000 Subject: [PATCH] Filesystem API: Make sure to only call `fread()` on non-empty files in the PclZip library. 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. Props yakimun, fierevere, jrf, DavidAnderson, SergeyBiryukov. Fixes #52018. git-svn-id: https://develop.svn.wordpress.org/trunk@50355 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-admin/includes/class-pclzip.php | 35 ++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 5 deletions(-) diff --git a/src/wp-admin/includes/class-pclzip.php b/src/wp-admin/includes/class-pclzip.php index 8a085f8bd0..b8f2fdca86 100644 --- a/src/wp-admin/includes/class-pclzip.php +++ b/src/wp-admin/includes/class-pclzip.php @@ -3884,7 +3884,12 @@ // ----- Read the compressed file in a buffer (one shot) - $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']); + if ( $p_entry['compressed_size'] > 0 ) { + $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']); + } + else { + $v_buffer = false; + } // ----- Decompress the file $v_file_content = @gzinflate($v_buffer); @@ -4096,7 +4101,12 @@ if ($p_entry['compressed_size'] == $p_entry['size']) { // ----- Read the file in a buffer (one shot) - $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']); + if ( $p_entry['compressed_size'] > 0 ) { + $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']); + } + else { + $v_buffer = false; + } // ----- Send the file to the output echo $v_buffer; @@ -4105,7 +4115,12 @@ else { // ----- Read the compressed file in a buffer (one shot) - $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']); + if ( $p_entry['compressed_size'] > 0 ) { + $v_buffer = @fread($this->zip_fd, $p_entry['compressed_size']); + } + else { + $v_buffer = false; + } // ----- Decompress the file $v_file_content = gzinflate($v_buffer); @@ -4209,12 +4224,22 @@ if ($p_entry['compression'] == 0) { // ----- Reading the file - $p_string = @fread($this->zip_fd, $p_entry['compressed_size']); + if ( $p_entry['compressed_size'] > 0 ) { + $p_string = @fread($this->zip_fd, $p_entry['compressed_size']); + } + else { + $p_string = false; + } } else { // ----- Reading the file - $v_data = @fread($this->zip_fd, $p_entry['compressed_size']); + if ( $p_entry['compressed_size'] > 0 ) { + $v_data = @fread($this->zip_fd, $p_entry['compressed_size']); + } + else { + $v_data = false; + } // ----- Decompress the file if (($p_string = @gzinflate($v_data)) === FALSE) {