Skip to content

Commit 901f71e

Browse files
committed
Fix GH-19688: Remove pattern overflow in zip addGlob()
memcmp() can overread the filename if the filename is shorter than the pattern. Closes GH-19689.
1 parent f6f1748 commit 901f71e

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ PHP NEWS
1010
. Fixed bug GH-12265 (Cloning an object breaks serialization recursion).
1111
(nielsdos)
1212

13+
- Zip:
14+
. Fixed bug GH-19688 (Remove pattern overflow in zip addGlob()). (nielsdos)
15+
1316
25 Sep 2025, PHP 8.3.26
1417

1518
- Core:

ext/zip/php_zip.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1784,7 +1784,7 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
17841784
basename = php_basename(Z_STRVAL_P(zval_file), Z_STRLEN_P(zval_file), NULL, 0);
17851785
file_stripped = ZSTR_VAL(basename);
17861786
file_stripped_len = ZSTR_LEN(basename);
1787-
} else if (opts.remove_path && !memcmp(Z_STRVAL_P(zval_file), opts.remove_path, opts.remove_path_len)) {
1787+
} else if (opts.remove_path && Z_STRLEN_P(zval_file) > opts.remove_path_len && !memcmp(Z_STRVAL_P(zval_file), opts.remove_path, opts.remove_path_len)) {
17881788
if (IS_SLASH(Z_STRVAL_P(zval_file)[opts.remove_path_len])) {
17891789
file_stripped = Z_STRVAL_P(zval_file) + opts.remove_path_len + 1;
17901790
file_stripped_len = Z_STRLEN_P(zval_file) - opts.remove_path_len - 1;

ext/zip/tests/gh19688.phpt

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
--TEST--
2+
GH-19688 (Remove pattern overflow in zip addGlob())
3+
--EXTENSIONS--
4+
zip
5+
--FILE--
6+
<?php
7+
$dir = __DIR__ . '/';
8+
$testfile = $dir . '001.phpt';
9+
$zip = new ZipArchive();
10+
$filename = $dir . '/gh19688.zip';
11+
$zip->open($filename, ZipArchive::CREATE | ZipArchive::OVERWRITE);
12+
$options = array('remove_path' => $dir . 'a very long string here that will overrun');
13+
$zip->addGlob($testfile, 0, $options);
14+
var_dump($zip->getNameIndex(0));
15+
?>
16+
--CLEAN--
17+
<?php
18+
@unlink(__DIR__ . '/gh19688.zip');
19+
?>
20+
--EXPECTF--
21+
string(%d) "%s001.phpt"

0 commit comments

Comments
 (0)