From 872ad9e0b8a49bb6a0edb036c7d4e2d29f3e0828 Mon Sep 17 00:00:00 2001 From: costdev Date: Wed, 27 Sep 2023 16:31:19 +0000 Subject: [PATCH] Filesystem API: Free the archive in `_unzip_file_ziparchive()`. There are several early returns in `_unzip_file_ziparchive()` which don't close the archive prior to returning. As this function is used in installation and upgrade processes which are memory-intensive, this calls `ZipArchive::close()` to free the archive prior to each early return. This excludes the first return which is a result of a failure to open the archive, which is [https://github.com/nih-at/libzip/blob/main/lib/zip_open.c#L62-L73 freed internally] when the failure occurs. References: - PHP.net: [https://www.php.net/manual/en/ziparchive.open.php ZipArchive::open()] and [https://www.php.net/manual/en/ziparchive.close.php ZipArchive::close()] - libzip: [https://libzip.org/documentation/zip_open.html zip_open()] and [https://libzip.org/documentation/zip_close.html zip_close()] Follow-up to: [13005], [13006], [13015], [13221], [14346] [25779]. Props azaozz, afragen, joemcgill, costdev. Fixes #59467 Built from https://develop.svn.wordpress.org/trunk@56735 git-svn-id: http://core.svn.wordpress.org/trunk@56247 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/file.php | 6 ++++++ wp-includes/version.php | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/wp-admin/includes/file.php b/wp-admin/includes/file.php index 6999566467..e6c8f0fd4a 100644 --- a/wp-admin/includes/file.php +++ b/wp-admin/includes/file.php @@ -1672,6 +1672,7 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) { $info = $z->statIndex( $i ); if ( ! $info ) { + $z->close(); return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) ); } @@ -1709,6 +1710,7 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) { $available_space = function_exists( 'disk_free_space' ) ? @disk_free_space( WP_CONTENT_DIR ) : false; if ( $available_space && ( $required_space > $available_space ) ) { + $z->close(); return new WP_Error( 'disk_full_unzip_file', __( 'Could not copy files. You may have run out of disk space.' ), @@ -1746,6 +1748,7 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) { foreach ( $needed_dirs as $_dir ) { // Only check to see if the Dir exists upon creation failure. Less I/O this way. if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) { + $z->close(); return new WP_Error( 'mkdir_failed_ziparchive', __( 'Could not create directory.' ), $_dir ); } } @@ -1774,6 +1777,7 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) { $info = $z->statIndex( $i ); if ( ! $info ) { + $z->close(); return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) ); } @@ -1793,10 +1797,12 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) { $contents = $z->getFromIndex( $i ); if ( false === $contents ) { + $z->close(); return new WP_Error( 'extract_failed_ziparchive', __( 'Could not extract file from archive.' ), $info['name'] ); } if ( ! $wp_filesystem->put_contents( $to . $info['name'], $contents, FS_CHMOD_FILE ) ) { + $z->close(); return new WP_Error( 'copy_failed_ziparchive', __( 'Could not copy file.' ), $info['name'] ); } } diff --git a/wp-includes/version.php b/wp-includes/version.php index ac1188db13..43d38528ef 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.4-beta1-56734'; +$wp_version = '6.4-beta1-56735'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.