]> granicus.if.org Git - php/commitdiff
Fix #72374: remove_path strips first char of filename
authortyage <namatyage@gmail.com>
Fri, 10 Jun 2016 08:17:50 +0000 (17:17 +0900)
committerChristoph M. Becker <cmbecker69@gmx.de>
Wed, 2 Oct 2019 13:33:28 +0000 (15:33 +0200)
NEWS
UPGRADING
ext/zip/php_zip.c
ext/zip/tests/bug72374.phpt [new file with mode: 0644]
ext/zip/tests/oo_addpattern.phpt

diff --git a/NEWS b/NEWS
index c290ef0cd5943a5fca9d09ea27bd09d718545ade..1692bd9b877b17cfbec3f47c47f3f5f3826a106a 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -68,4 +68,7 @@ PHP                                                                        NEWS
   . Changed functions to accept/return XMKWriter objects instead of resources.
     (cmb)
 
+- Zip:
+  . Fixed bug #72374 (remove_path strips first char of filename). (tyage)
+
 <<< NOTE: Insert NEWS from last stable release here prior to actual release! >>>
index 99c41d8adb5bb1d19fa1c5504173f75a074afd47..302daa57ec706d17c5ebfd768afc5880370dc885 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -280,6 +280,13 @@ PHP 8.0 UPGRADE NOTES
   . The XMLWriter functions now accept and return, respectively, XMLWriter
     objects instead of resources.
 
+- Zip:
+  . The remove_path option of ZipArchive::addGlob() and ::addPattern() is now
+    treated as arbitrary string prefix (for consistency with the add_path
+    option), whereas formerly it was treated as directory name. This means that
+    if no trailing directory separator is given, the following character is
+    no longer stripped from the filename.
+
 - Zlib:
   . gzgetss() has been removed.
 
index cba217f1de8c63ecb23fce53fc093c1d04899768..d30ad8747f654352f2341436e6c756218f25f3df 100644 (file)
@@ -1602,6 +1602,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;
                }
        }
 
@@ -1627,8 +1628,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);
diff --git a/ext/zip/tests/bug72374.phpt b/ext/zip/tests/bug72374.phpt
new file mode 100644 (file)
index 0000000..b214be3
--- /dev/null
@@ -0,0 +1,32 @@
+--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('remove_path' => $dirname . 'some-'));
+$zip->addGlob($file, 0, array('remove_path' => $dirname));
+verify_entries($zip, ['foo.txt', '/some-foo.txt']);
+$zip->close();
+?>
+--CLEAN--
+<?php
+$dirname = dirname(__FILE__) . '/';
+include $dirname . 'utils.inc';
+
+$dirname = $dirname . 'bug72374/';
+rmdir_rf($dirname);
+?>
+--EXPECT--
index fe1a57368df68c43349887cd12087f021ac4762c..19453bee491546e6c2d4308ede843acb5a356284 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\n";
 }
@@ -35,8 +35,8 @@ if ($zip->status == ZIPARCHIVE::ER_OK) {
             "foobar/",
             "foobar/baz",
             "entry1.txt",
-            "baz/foo.txt",
-            "baz/bar.txt"
+            "baz" . DIRECTORY_SEPARATOR . "foo.txt",
+            "baz" . DIRECTORY_SEPARATOR . "bar.txt"
         ])) {
             echo "failed\n";
         } else {