Skip to content

Commit 7a93d2c

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: ext/zip: fix memory leak when encryption is passed as userland array option.
2 parents 83e3a6d + 0e9c39d commit 7a93d2c

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

ext/zip/php_zip.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,11 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
17521752
}
17531753
#ifdef HAVE_ENCRYPTION
17541754
if (opts.enc_method >= 0) {
1755+
if (UNEXPECTED(zip_file_set_encryption(ze_obj->za, ze_obj->last_id, ZIP_EM_NONE, NULL) < 0)) {
1756+
zend_array_destroy(Z_ARR_P(return_value));
1757+
php_error_docref(NULL, E_WARNING, "password reset failed");
1758+
RETURN_FALSE;
1759+
}
17551760
if (zip_file_set_encryption(ze_obj->za, ze_obj->last_id, opts.enc_method, opts.enc_password)) {
17561761
zend_array_destroy(Z_ARR_P(return_value));
17571762
RETURN_FALSE;

ext/zip/tests/oo_addglob_leak.phpt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
--TEST--
2+
ZipArchive::addGlob() method leaking after several calls when encryption is set.
3+
--EXTENSIONS--
4+
zip
5+
--SKIPIF--
6+
<?php
7+
if (!method_exists('ZipArchive', 'setEncryptionName')) die('skip encrytion not supported');
8+
if(!defined("GLOB_BRACE")) die ('skip requires GLOB_BRACE');
9+
?>
10+
--FILE--
11+
<?php
12+
$dirname = __DIR__ . '/';
13+
include $dirname . 'utils.inc';
14+
15+
$dirname = __DIR__ . '/__tmp_oo_addglob2/';
16+
$file = $dirname . 'test.zip';
17+
18+
@mkdir($dirname);
19+
copy(__FILE__, $dirname . 'foo.txt');
20+
copy(__FILE__, $dirname . 'bar.txt');
21+
22+
$zip = new ZipArchive();
23+
if (!$zip->open($file, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
24+
exit('failed');
25+
}
26+
27+
$options = [
28+
'remove_all_path' => true,
29+
'comp_method' => ZipArchive::CM_STORE,
30+
'comp_flags' => 5,
31+
'enc_method' => ZipArchive::EM_AES_256,
32+
'enc_password' => 'secret',
33+
];
34+
var_dump($zip->addGlob($dirname . 'bar.*', GLOB_BRACE, $options));
35+
var_dump($zip->addGlob($dirname . 'bar.*', GLOB_BRACE, $options));
36+
?>
37+
--CLEAN--
38+
<?php
39+
$dirname = __DIR__ . '/';
40+
include $dirname . 'utils.inc';
41+
rmdir_rf(__DIR__ . '/__tmp_oo_addglob2/');
42+
?>
43+
--EXPECTF--
44+
array(1) {
45+
[0]=>
46+
string(%d) "%s"
47+
}
48+
array(1) {
49+
[0]=>
50+
string(%d) "%s"
51+
}

0 commit comments

Comments
 (0)