Skip to content

Commit 08f0885

Browse files
timuribweltling
authored andcommitted
Fix bug #76524 - ZipArchive memory leak
Bugfix #76524: Free up zip internal state and adjust the tests for Windows Bugfix #76524: Fix possible use after free for libzip 1.3.1 Bugfix #76524: Make the test independent of platform
1 parent a820aab commit 08f0885

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

ext/zip/php_zip.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,10 +1003,13 @@ static void php_zip_object_free_storage(zend_object *object) /* {{{ */
10031003
}
10041004
if (intern->za) {
10051005
if (zip_close(intern->za) != 0) {
1006+
#if LIBZIP_VERSION_MAJOR == 1 && LIBZIP_VERSION_MINOR == 3 && LIBZIP_VERSION_MICRO == 1
1007+
php_error_docref(NULL, E_WARNING, "Cannot destroy the zip context: %s", "zip_close have failed");
1008+
#else
10061009
php_error_docref(NULL, E_WARNING, "Cannot destroy the zip context: %s", zip_strerror(intern->za));
1007-
return;
1010+
zip_discard(intern->za);
1011+
#endif
10081012
}
1009-
intern->za = NULL;
10101013
}
10111014

10121015
if (intern->buffers_cnt>0) {

ext/zip/tests/bug76524.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
ZipArchive Bug #76524 (memory leak with ZipArchive::OVERWRITE flag and empty archive)
3+
--SKIPIF--
4+
<?php
5+
if(!extension_loaded('zip')) die('skip');
6+
?>
7+
--FILE--
8+
<?php
9+
10+
$filename = __DIR__ . '/nonexistent.zip';
11+
12+
$zip = new ZipArchive();
13+
$zip->open($filename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
14+
echo 'ok';
15+
16+
/* Zip-related error messages depend on platform and libzip version,
17+
so the regex is used to check that Zend MM does NOT show warnings
18+
about leaks: */
19+
?>
20+
--EXPECTREGEX--
21+
ok((?!memory leaks detected).)*

0 commit comments

Comments
 (0)