Skip to content

Commit e34c8b7

Browse files
committed
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 git-svn-id: https://develop.svn.wordpress.org/trunk@56735 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d740727 commit e34c8b7

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/wp-admin/includes/file.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,6 +1672,7 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) {
16721672
$info = $z->statIndex( $i );
16731673

16741674
if ( ! $info ) {
1675+
$z->close();
16751676
return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
16761677
}
16771678

@@ -1709,6 +1710,7 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) {
17091710
$available_space = function_exists( 'disk_free_space' ) ? @disk_free_space( WP_CONTENT_DIR ) : false;
17101711

17111712
if ( $available_space && ( $required_space > $available_space ) ) {
1713+
$z->close();
17121714
return new WP_Error(
17131715
'disk_full_unzip_file',
17141716
__( '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() ) {
17461748
foreach ( $needed_dirs as $_dir ) {
17471749
// Only check to see if the Dir exists upon creation failure. Less I/O this way.
17481750
if ( ! $wp_filesystem->mkdir( $_dir, FS_CHMOD_DIR ) && ! $wp_filesystem->is_dir( $_dir ) ) {
1751+
$z->close();
17491752
return new WP_Error( 'mkdir_failed_ziparchive', __( 'Could not create directory.' ), $_dir );
17501753
}
17511754
}
@@ -1774,6 +1777,7 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) {
17741777
$info = $z->statIndex( $i );
17751778

17761779
if ( ! $info ) {
1780+
$z->close();
17771781
return new WP_Error( 'stat_failed_ziparchive', __( 'Could not retrieve file from archive.' ) );
17781782
}
17791783

@@ -1793,10 +1797,12 @@ function _unzip_file_ziparchive( $file, $to, $needed_dirs = array() ) {
17931797
$contents = $z->getFromIndex( $i );
17941798

17951799
if ( false === $contents ) {
1800+
$z->close();
17961801
return new WP_Error( 'extract_failed_ziparchive', __( 'Could not extract file from archive.' ), $info['name'] );
17971802
}
17981803

17991804
if ( ! $wp_filesystem->put_contents( $to . $info['name'], $contents, FS_CHMOD_FILE ) ) {
1805+
$z->close();
18001806
return new WP_Error( 'copy_failed_ziparchive', __( 'Could not copy file.' ), $info['name'] );
18011807
}
18021808
}

0 commit comments

Comments
 (0)