Skip to content

Fix #72374: ZipArchive::addGlob remove_path option strips first char of filename #1939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ext/zip/php_zip.c
Original file line number Diff line number Diff line change
Expand Up @@ -1652,6 +1652,7 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
size_t real_len = strlen(remove_path);
if ((real_len > 1) && ((remove_path[real_len - 1] == '/') || (remove_path[real_len - 1] == '\\'))) {
remove_path[real_len - 1] = '\0';
remove_path_len -= 1;
}
}

Expand All @@ -1677,8 +1678,8 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
file_stripped = ZSTR_VAL(basename);
file_stripped_len = ZSTR_LEN(basename);
} else if (remove_path && strstr(Z_STRVAL_P(zval_file), remove_path) != NULL) {
file_stripped = Z_STRVAL_P(zval_file) + remove_path_len + 1;
file_stripped_len = Z_STRLEN_P(zval_file) - remove_path_len - 1;
file_stripped = Z_STRVAL_P(zval_file) + remove_path_len;
file_stripped_len = Z_STRLEN_P(zval_file) - remove_path_len;
} else {
file_stripped = Z_STRVAL_P(zval_file);
file_stripped_len = Z_STRLEN_P(zval_file);
Expand Down
34 changes: 34 additions & 0 deletions ext/zip/tests/bug72374.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
--TEST--
Bug #72374 (ZipArchive::addGlob remove_path option strips first char of filename)
--SKIPIF--
<?php
if(!extension_loaded('zip')) die('skip');
?>
--FILE--
<?php
$dirname = dirname(__FILE__) . '/';
include $dirname . 'utils.inc';

$dirname = $dirname . 'bug72374/';
mkdir($dirname);
$file = $dirname . 'some-foo.txt';
touch($file);

$zip = new ZipArchive();
$zip->open($dirname . 'test.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addGlob($file, 0, array('add_path' => 'prefix-', 'remove_path' => $dirname . 'some-'));
$zip->addGlob($file, 0, array('add_path' => 'dir-prefix', 'remove_path' => $dirname));
dump_entries_name($zip);
$zip->close();
?>
--CLEAN--
<?php
$dirname = dirname(__FILE__) . '/';
include $dirname . 'utils.inc';

$dirname = $dirname . 'bug72374/';
rmdir_rf($dirname);
?>
--EXPECTF--
0 prefix-foo.txt
1 dir-prefix/some-foo.txt
2 changes: 1 addition & 1 deletion ext/zip/tests/oo_addpattern.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ if (!$zip->open($file)) {
exit('failed');
}
$dir = realpath($dirname);
$options = array('add_path' => 'baz/', 'remove_path' => $dir);
$options = array('add_path' => 'baz', 'remove_path' => $dir);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This necessary change hints at a potential BC break, so the PR should probably go to PHP 7.1 only (or maybe 8.0?). After all, this PHPT shows, that add_path and remove_path can be used together (albeit add_path needs the trailing slash, and remove_path must not have it).

if (!$zip->addPattern('/\.txt$/', $dir, $options)) {
echo "failed\n";
}
Expand Down