Skip to content

Commit be14d4e

Browse files
committed
better fix for #72374
1 parent f3c24ec commit be14d4e

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

ext/zip/php_zip.c

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1644,7 +1644,7 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
16441644
{
16451645
zval *self = ZEND_THIS;
16461646
char *path = ".";
1647-
char *remove_path = NULL, *save_remove_path;
1647+
char *remove_path = NULL;
16481648
char *add_path = NULL;
16491649
size_t add_path_len, remove_path_len = 0, path_len = 1;
16501650
zend_long remove_all_path = 0;
@@ -1676,15 +1676,6 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
16761676
RETURN_FALSE;
16771677
}
16781678

1679-
save_remove_path = remove_path;
1680-
if (remove_path && remove_path_len > 1) {
1681-
size_t real_len = strlen(remove_path);
1682-
if ((real_len > 1) && ((remove_path[real_len - 1] == '/') || (remove_path[real_len - 1] == '\\'))) {
1683-
remove_path = estrndup(remove_path, real_len - 1);
1684-
remove_path_len -= 1;
1685-
}
1686-
}
1687-
16881679
if (type == 1) {
16891680
found = php_zip_glob(ZSTR_VAL(pattern), ZSTR_LEN(pattern), glob_flags, return_value);
16901681
} else {
@@ -1707,8 +1698,13 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
17071698
file_stripped = ZSTR_VAL(basename);
17081699
file_stripped_len = ZSTR_LEN(basename);
17091700
} else if (remove_path && strstr(Z_STRVAL_P(zval_file), remove_path) != NULL) {
1710-
file_stripped = Z_STRVAL_P(zval_file) + remove_path_len;
1711-
file_stripped_len = Z_STRLEN_P(zval_file) - remove_path_len;
1701+
if (IS_SLASH(Z_STRVAL_P(zval_file)[remove_path_len])) {
1702+
file_stripped = Z_STRVAL_P(zval_file) + remove_path_len + 1;
1703+
file_stripped_len = Z_STRLEN_P(zval_file) - remove_path_len - 1;
1704+
} else {
1705+
file_stripped = Z_STRVAL_P(zval_file) + remove_path_len;
1706+
file_stripped_len = Z_STRLEN_P(zval_file) - remove_path_len;
1707+
}
17121708
} else {
17131709
file_stripped = Z_STRVAL_P(zval_file);
17141710
file_stripped_len = Z_STRLEN_P(zval_file);
@@ -1741,9 +1737,6 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
17411737
}
17421738
}
17431739
}
1744-
if (remove_path != save_remove_path) {
1745-
efree(remove_path);
1746-
}
17471740
}
17481741
/* }}} */
17491742

ext/zip/tests/bug72374.phpt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,27 @@ if(!extension_loaded('zip')) die('skip');
99
$dirname = dirname(__FILE__) . '/';
1010
include $dirname . 'utils.inc';
1111

12-
$dirname = $dirname . 'bug72374/';
12+
$dirname = $dirname . 'bug72374';
1313
mkdir($dirname);
14-
$file = $dirname . 'some-foo.txt';
15-
touch($file);
14+
$file1 = $dirname . '/some-foo.txt';
15+
touch($file1);
16+
$file2 = $dirname . '/some-bar.txt';
17+
touch($file2);
1618

1719
$zip = new ZipArchive();
18-
$zip->open($dirname . 'test.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
19-
$zip->addGlob($file, 0, array('remove_path' => $dirname . 'some-'));
20-
$zip->addGlob($file, 0, array('remove_path' => $dirname));
21-
verify_entries($zip, ['foo.txt', '/some-foo.txt']);
20+
$zip->open($dirname . '/test.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
21+
$zip->addGlob($file1, 0, array('remove_path' => $dirname . '/some-'));
22+
$zip->addGlob($file1, 0, array('remove_path' => $dirname . '/'));
23+
$zip->addGlob($file2, 0, array('remove_path' => $dirname));
24+
verify_entries($zip, ['foo.txt', 'some-foo.txt', 'some-bar.txt']);
2225
$zip->close();
2326
?>
2427
--CLEAN--
2528
<?php
2629
$dirname = dirname(__FILE__) . '/';
2730
include $dirname . 'utils.inc';
2831

29-
$dirname = $dirname . 'bug72374/';
32+
$dirname = $dirname . 'bug72374';
3033
rmdir_rf($dirname);
3134
?>
3235
--EXPECT--

ext/zip/tests/oo_addpattern.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ if (!$zip->open($file)) {
2525
exit('failed');
2626
}
2727
$dir = realpath($dirname);
28-
$options = array('add_path' => 'baz', 'remove_path' => $dir);
28+
$options = array('add_path' => 'baz/', 'remove_path' => $dir);
2929
if (!$zip->addPattern('/\.txt$/', $dir, $options)) {
3030
echo "failed 1\n";
3131
}
@@ -44,8 +44,8 @@ if ($zip->status == ZIPARCHIVE::ER_OK) {
4444
"foobar/",
4545
"foobar/baz",
4646
"entry1.txt",
47-
"baz" . DIRECTORY_SEPARATOR . "foo.txt",
48-
"baz" . DIRECTORY_SEPARATOR . "bar.txt"
47+
"baz/foo.txt",
48+
"baz/bar.txt"
4949
])) {
5050
echo "failed\n";
5151
} else {

0 commit comments

Comments
 (0)