]> granicus.if.org Git - php/commitdiff
better fix for #72374
authorRemi Collet <remi@remirepo.net>
Thu, 5 Mar 2020 14:26:26 +0000 (15:26 +0100)
committerRemi Collet <remi@php.net>
Fri, 6 Mar 2020 09:27:29 +0000 (10:27 +0100)
ext/zip/php_zip.c
ext/zip/tests/bug72374.phpt
ext/zip/tests/oo_addpattern.phpt

index c167ceedc2949cf307288a450c98e7b32672961c..898627960242a3080659c0e257b4bd380ec1a534 100644 (file)
@@ -1644,7 +1644,7 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
 {
        zval *self = ZEND_THIS;
        char *path = ".";
-       char *remove_path = NULL, *save_remove_path;
+       char *remove_path = NULL;
        char *add_path = NULL;
        size_t  add_path_len, remove_path_len = 0, path_len = 1;
        zend_long remove_all_path = 0;
@@ -1676,15 +1676,6 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
                RETURN_FALSE;
        }
 
-       save_remove_path = remove_path;
-       if (remove_path && remove_path_len > 1) {
-               size_t real_len = strlen(remove_path);
-               if ((real_len > 1) && ((remove_path[real_len - 1] == '/') || (remove_path[real_len - 1] == '\\'))) {
-                       remove_path = estrndup(remove_path, real_len - 1);
-                       remove_path_len -= 1;
-               }
-       }
-
        if (type == 1) {
                found = php_zip_glob(ZSTR_VAL(pattern), ZSTR_LEN(pattern), glob_flags, return_value);
        } else {
@@ -1707,8 +1698,13 @@ 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;
-                                       file_stripped_len = Z_STRLEN_P(zval_file) - remove_path_len;
+                                       if (IS_SLASH(Z_STRVAL_P(zval_file)[remove_path_len])) {
+                                               file_stripped = Z_STRVAL_P(zval_file) + remove_path_len + 1;
+                                               file_stripped_len = Z_STRLEN_P(zval_file) - remove_path_len - 1;
+                                       } else {
+                                               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);
@@ -1741,9 +1737,6 @@ static void php_zip_add_from_pattern(INTERNAL_FUNCTION_PARAMETERS, int type) /*
                        }
                }
        }
-       if (remove_path != save_remove_path) {
-               efree(remove_path);
-       }
 }
 /* }}} */
 
index b214be3eaf66bb0444b78724fcf7317e212a5d86..b9906c4a9e9722c901cf35789631b67241769c9d 100644 (file)
@@ -9,16 +9,19 @@ if(!extension_loaded('zip')) die('skip');
 $dirname = dirname(__FILE__) . '/';
 include $dirname . 'utils.inc';
 
-$dirname = $dirname . 'bug72374/';
+$dirname = $dirname . 'bug72374';
 mkdir($dirname);
-$file = $dirname . 'some-foo.txt';
-touch($file);
+$file1 = $dirname . '/some-foo.txt';
+touch($file1);
+$file2 = $dirname . '/some-bar.txt';
+touch($file2);
 
 $zip = new ZipArchive();
-$zip->open($dirname . 'test.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
-$zip->addGlob($file, 0, array('remove_path' => $dirname . 'some-'));
-$zip->addGlob($file, 0, array('remove_path' => $dirname));
-verify_entries($zip, ['foo.txt', '/some-foo.txt']);
+$zip->open($dirname . '/test.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE);
+$zip->addGlob($file1, 0, array('remove_path' => $dirname . '/some-'));
+$zip->addGlob($file1, 0, array('remove_path' => $dirname . '/'));
+$zip->addGlob($file2, 0, array('remove_path' => $dirname));
+verify_entries($zip, ['foo.txt', 'some-foo.txt', 'some-bar.txt']);
 $zip->close();
 ?>
 --CLEAN--
@@ -26,7 +29,7 @@ $zip->close();
 $dirname = dirname(__FILE__) . '/';
 include $dirname . 'utils.inc';
 
-$dirname = $dirname . 'bug72374/';
+$dirname = $dirname . 'bug72374';
 rmdir_rf($dirname);
 ?>
 --EXPECT--
index 227a89bb178f9f6793dd28ff6a35e1f0e3577962..2b9ef1164778a1eebceb9be11bae9346571d97a1 100644 (file)
@@ -25,7 +25,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);
 if (!$zip->addPattern('/\.txt$/', $dir, $options)) {
        echo "failed 1\n";
 }
@@ -44,8 +44,8 @@ if ($zip->status == ZIPARCHIVE::ER_OK) {
             "foobar/",
             "foobar/baz",
             "entry1.txt",
-            "baz" . DIRECTORY_SEPARATOR . "foo.txt",
-            "baz" . DIRECTORY_SEPARATOR . "bar.txt"
+            "baz/foo.txt",
+            "baz/bar.txt"
         ])) {
             echo "failed\n";
         } else {