Skip to content

Commit

Permalink
Fix #81490: ZipArchive::extractTo() may leak memory
Browse files Browse the repository at this point in the history
We always need to free the CWD state.

Closes GH-7536.
  • Loading branch information
cmb69 committed Sep 30, 2021
1 parent 23e13e2 commit 4d44271
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
3 changes: 3 additions & 0 deletions NEWS
Expand Up @@ -32,6 +32,9 @@ PHP NEWS
. Fixed bug #70962 (XML_OPTION_SKIP_WHITE strips embedded whitespace).
(Aliaksandr Bystry, cmb)

- Zip:
. Fixed bug #81490 (ZipArchive::extractTo() may leak memory). (cmb, Remi)

23 Sep 2021, PHP 7.4.24

- Core:
Expand Down
4 changes: 3 additions & 1 deletion ext/zip/php_zip.c
Expand Up @@ -161,11 +161,13 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t
virtual_file_ex(&new_state, file, NULL, CWD_EXPAND);
path_cleaned = php_zip_make_relative_path(new_state.cwd, new_state.cwd_length);
if(!path_cleaned) {
CWD_STATE_FREE(new_state.cwd);
return 0;
}
path_cleaned_len = strlen(path_cleaned);

if (path_cleaned_len >= MAXPATHLEN || zip_stat(za, file, 0, &sb) != 0) {
CWD_STATE_FREE(new_state.cwd);
return 0;
}

Expand Down Expand Up @@ -200,8 +202,8 @@ static int php_zip_extract_file(struct zip * za, char *dest, char *file, size_t
efree(file_dirname_fullpath);
if (!is_dir_only) {
zend_string_release_ex(file_basename, 0);
CWD_STATE_FREE(new_state.cwd);
}
CWD_STATE_FREE(new_state.cwd);
return 0;
}
}
Expand Down
21 changes: 21 additions & 0 deletions ext/zip/tests/bug81490.phpt
@@ -0,0 +1,21 @@
--TEST--
Bug #81490 (ZipArchive::extractTo() may leak memory)
--SKIPIF--
<?php
if (!extension_loaded("zip")) die("skip zip extension not available");
?>
--FILE--
<?php
$zip = new ZipArchive();
$zip->open(__DIR__ . "/bug81490.zip", ZipArchive::CREATE|ZipArchive::OVERWRITE);
$zip->addFromString("", "yada yada");
mkdir(__DIR__ . "/bug81490");
$zip->open(__DIR__ . "/bug81490.zip");
$zip->extractTo(__DIR__ . "/bug81490", "");
?>
--EXPECT--
--CLEAN--
<?php
@unlink(__DIR__ . "/bug81490.zip");
@rmdir(__DIR__ . "/bug81490");
?>

1 comment on commit 4d44271

@remicollet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cmb69 thanks

FYI, also applied in pecl/zip pierrejoye/php_zip@80bcec9

Please sign in to comment.