Skip to content

Commit 9b8a647

Browse files
committed
Merge branch 'PHP-8.5'
* PHP-8.5: Fix phpGH-21698: memory leak in ZipArchive::addGlob on early returns.
2 parents 9b48fe7 + 493b59a commit 9b8a647

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

ext/zip/php_zip.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,12 +676,14 @@ int php_zip_glob(zend_string *spattern, zend_long flags, zval *return_value) /*
676676

677677
/* now catch the FreeBSD style of "no matches" */
678678
if (!globbuf.gl_pathc || !globbuf.gl_pathv) {
679+
globfree(&globbuf);
679680
return 0;
680681
}
681682

682683
/* we assume that any glob pattern will match files from one directory only
683684
so checking the dirname of the first match should be sufficient */
684685
if (ZIP_OPENBASEDIR_CHECKPATH(globbuf.gl_pathv[0])) {
686+
globfree(&globbuf);
685687
return -1;
686688
}
687689

ext/zip/tests/gh21698.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
GH-21698 (ZipArchive::addGlob memory leak when open_basedir rejects the match)
3+
--EXTENSIONS--
4+
zip
5+
--FILE--
6+
<?php
7+
$zipfile = __DIR__ . '/gh21698.zip';
8+
$zip = new ZipArchive();
9+
$zip->open($zipfile, ZipArchive::CREATE | ZipArchive::OVERWRITE);
10+
11+
ini_set('open_basedir', '/nonexistent_dir_for_gh21698');
12+
var_dump($zip->addGlob(__FILE__, 0, []));
13+
$zip->close();
14+
?>
15+
--CLEAN--
16+
<?php
17+
@unlink(__DIR__ . '/gh21698.zip');
18+
?>
19+
--EXPECTF--
20+
Warning: ZipArchive::addGlob(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (%s) in %s on line %d
21+
bool(false)

0 commit comments

Comments
 (0)