Skip to content

Commit 315bdeb

Browse files
committed
Fix GH-18438: Handling of empty data and errors in ZipArchive::addPattern
There is a ZPP arginfo violation because the empty return or error return is not always properly handled. And there is also a memory leak if creating the regular expression instance fails.
1 parent b066ac0 commit 315bdeb

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

ext/zip/php_zip.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,10 @@ int php_zip_pcre(zend_string *regexp, char *path, int path_len, zval *return_val
761761

762762
re = pcre_get_compiled_regex(regexp, &capture_count);
763763
if (!re) {
764+
for (i = 0; i < files_cnt; i++) {
765+
zend_string_release_ex(namelist[i], 0);
766+
}
767+
efree(namelist);
764768
php_error_docref(NULL, E_WARNING, "Invalid expression");
765769
return -1;
766770
}
@@ -1837,6 +1841,10 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
18371841
#endif
18381842
}
18391843
}
1844+
} else if (found == 0) {
1845+
RETURN_EMPTY_ARRAY();
1846+
} else {
1847+
RETURN_FALSE;
18401848
}
18411849
}
18421850
/* }}} */

ext/zip/tests/gh18438.phpt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
--TEST--
2+
GH-18438 (Handling of empty data and errors in ZipArchive::addPattern)
3+
--EXTENSIONS--
4+
zip
5+
--FILE--
6+
<?php
7+
class CustomStreamWrapper {
8+
public $context;
9+
function dir_closedir(): bool {
10+
return true;
11+
}
12+
function dir_opendir(string $path, int $options): bool {
13+
return true;
14+
}
15+
function dir_readdir(): string|false {
16+
return false;
17+
}
18+
function dir_rewinddir(): bool {
19+
return false;
20+
}
21+
}
22+
23+
$file = __DIR__ . '/gh18438.zip';
24+
$zip = new ZipArchive;
25+
$zip->open($file, ZIPARCHIVE::CREATE);
26+
var_dump($zip->addPattern('/nomatches/'));
27+
var_dump($zip->addPattern('/invalid'));
28+
29+
stream_wrapper_register('custom', CustomStreamWrapper::class);
30+
var_dump($zip->addPattern('/invalid', 'custom://'));
31+
?>
32+
--EXPECTF--
33+
array(0) {
34+
}
35+
36+
Warning: ZipArchive::addPattern(): No ending delimiter '/' found in %s on line %d
37+
38+
Warning: ZipArchive::addPattern(): Invalid expression in %s on line %d
39+
bool(false)
40+
array(0) {
41+
}

0 commit comments

Comments
 (0)