Skip to content

Commit 4d6f88e

Browse files
tyagecmb69
authored andcommitted
Fix #72374: remove_path strips first char of filename
1 parent 8a2a9e0 commit 4d6f88e

File tree

5 files changed

+48
-5
lines changed

5 files changed

+48
-5
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,7 @@ PHP NEWS
6868
. Changed functions to accept/return XMKWriter objects instead of resources.
6969
(cmb)
7070

71+
- Zip:
72+
. Fixed bug #72374 (remove_path strips first char of filename). (tyage)
73+
7174
<<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>

UPGRADING

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,13 @@ PHP 8.0 UPGRADE NOTES
280280
. The XMLWriter functions now accept and return, respectively, XMLWriter
281281
objects instead of resources.
282282

283+
- Zip:
284+
. The remove_path option of ZipArchive::addGlob() and ::addPattern() is now
285+
treated as arbitrary string prefix (for consistency with the add_path
286+
option), whereas formerly it was treated as directory name. This means that
287+
if no trailing directory separator is given, the following character is
288+
no longer stripped from the filename.
289+
283290
- Zlib:
284291
. gzgetss() has been removed.
285292

ext/zip/php_zip.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1602,6 +1602,7 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
16021602
size_t real_len = strlen(remove_path);
16031603
if ((real_len > 1) && ((remove_path[real_len - 1] == '/') || (remove_path[real_len - 1] == '\\'))) {
16041604
remove_path[real_len - 1] = '\0';
1605+
remove_path_len -= 1;
16051606
}
16061607
}
16071608

@@ -1627,8 +1628,8 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
16271628
file_stripped = ZSTR_VAL(basename);
16281629
file_stripped_len = ZSTR_LEN(basename);
16291630
} else if (remove_path && strstr(Z_STRVAL_P(zval_file), remove_path) != NULL) {
1630-
file_stripped = Z_STRVAL_P(zval_file) + remove_path_len + 1;
1631-
file_stripped_len = Z_STRLEN_P(zval_file) - remove_path_len - 1;
1631+
file_stripped = Z_STRVAL_P(zval_file) + remove_path_len;
1632+
file_stripped_len = Z_STRLEN_P(zval_file) - remove_path_len;
16321633
} else {
16331634
file_stripped = Z_STRVAL_P(zval_file);
16341635
file_stripped_len = Z_STRLEN_P(zval_file);

ext/zip/tests/bug72374.phpt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
--TEST--
2+
Bug #72374 (ZipArchive::addGlob remove_path option strips first char of filename)
3+
--SKIPIF--
4+
<?php
5+
if(!extension_loaded('zip')) die('skip');
6+
?>
7+
--FILE--
8+
<?php
9+
$dirname = dirname(__FILE__) . '/';
10+
include $dirname . 'utils.inc';
11+
12+
$dirname = $dirname . 'bug72374/';
13+
mkdir($dirname);
14+
$file = $dirname . 'some-foo.txt';
15+
touch($file);
16+
17+
$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']);
22+
$zip->close();
23+
?>
24+
--CLEAN--
25+
<?php
26+
$dirname = dirname(__FILE__) . '/';
27+
include $dirname . 'utils.inc';
28+
29+
$dirname = $dirname . 'bug72374/';
30+
rmdir_rf($dirname);
31+
?>
32+
--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\n";
3131
}
@@ -35,8 +35,8 @@ if ($zip->status == ZIPARCHIVE::ER_OK) {
3535
"foobar/",
3636
"foobar/baz",
3737
"entry1.txt",
38-
"baz/foo.txt",
39-
"baz/bar.txt"
38+
"baz" . DIRECTORY_SEPARATOR . "foo.txt",
39+
"baz" . DIRECTORY_SEPARATOR . "bar.txt"
4040
])) {
4141
echo "failed\n";
4242
} else {

0 commit comments

Comments
 (0)