]> granicus.if.org Git - php/commitdiff
Fix bug #51068 (glob:// do not support current path relative)
authorAhmed Abdou <email@ahmed.ro>
Tue, 29 Jan 2019 07:59:45 +0000 (08:59 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 11 Feb 2019 14:50:27 +0000 (15:50 +0100)
Fix DirectoryIterator glob://* current path relative queries

NEWS
ext/spl/spl_directory.c
ext/spl/tests/bug51068.phpt [new file with mode: 0644]
main/streams/glob_wrapper.c

diff --git a/NEWS b/NEWS
index f823f484b934f0de1e0984f12c5d21fcaf96117e..fa5200ce845abbe2d4a89a11f874493f16da6b3b 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -10,6 +10,10 @@ PHP                                                                        NEWS
   . Support Oracle Database tracing attributes ACTION, MODULE,
     CLIENT_INFO, and CLIENT_IDENTIFIER. (Cameron Porter)
 
+- SPL:
+  . Fixed bug #51068 (DirectoryIterator glob:// don't support current path
+    relative queries). (Ahmed Abdou)
+
 - Standard:
   . Fixed bug #77552 (Unintialized php_stream_statbuf in stat functions).
     (John Stevenson)
index f411fca5395df60c6e26e1011c3e7d53a521db84..bc05044bc72c1ed802a6e80e9adf4a08684cae58 100644 (file)
@@ -212,12 +212,21 @@ static inline void spl_filesystem_object_get_file_name(spl_filesystem_object *in
                        }
                        break;
                case SPL_FS_DIR:
-                       if (intern->file_name) {
-                               efree(intern->file_name);
+                       {
+                               size_t path_len = 0;
+                               char *path = spl_filesystem_object_get_path(intern, &path_len);
+                               if (intern->file_name) {
+                                       efree(intern->file_name);
+                               }
+                               /* if there is parent path, ammend it, otherwise just use the given path as is */
+                               if (path_len == 0) {
+                                       intern->file_name_len = spprintf(
+                                               &intern->file_name, 0, "%s", intern->u.dir.entry.d_name);
+                               } else {
+                                       intern->file_name_len = spprintf(
+                                               &intern->file_name, 0, "%s%c%s", path, slash, intern->u.dir.entry.d_name);
+                               }
                        }
-                       intern->file_name_len = spprintf(&intern->file_name, 0, "%s%c%s",
-                                                        spl_filesystem_object_get_path(intern, NULL),
-                                                        slash, intern->u.dir.entry.d_name);
                        break;
        }
 } /* }}} */
diff --git a/ext/spl/tests/bug51068.phpt b/ext/spl/tests/bug51068.phpt
new file mode 100644 (file)
index 0000000..0263536
--- /dev/null
@@ -0,0 +1,36 @@
+--TEST--
+SPL: glob wrapper interactions with DirectoryIterator
+--FILE--
+<?php
+touch('bug.51068');
+mkdir('bug.51068.dir');
+touch('bug.51068.dir/lvl2.bug.51068');
+$iter = new DirectoryIterator('glob://*.51068');
+foreach ($iter as $f) {
+       var_dump($f->getFilename());
+       var_dump($f->getSize());
+}
+$iter = new DirectoryIterator('glob://bug.51068.dir/*.51068');
+foreach ($iter as $f) {
+  var_dump($f->getFilename());
+  var_dump($f->getSize());
+}
+$iter = new DirectoryIterator('glob://bug.51068.dir');
+foreach ($iter as $f) {
+  var_dump($f->getFilename());
+  var_dump($f->getSize() >= 0);
+}
+?>
+--CLEAN--
+<?php
+unlink('bug.51068');
+unlink('bug.51068.dir/lvl2.bug.51068');
+rmdir('bug.51068.dir');
+?>
+--EXPECT--
+string(9) "bug.51068"
+int(0)
+string(14) "lvl2.bug.51068"
+int(0)
+string(13) "bug.51068.dir"
+bool(true)
index 903119705ba504dcc32535af18f010996c0b852f..b10318fef21f88e7d29cd34d8e3282a7ad531387 100644 (file)
@@ -128,7 +128,7 @@ static void php_glob_stream_path_split(glob_s_t *pglob, const char *path, int ge
                if (pglob->path) {
                        efree(pglob->path);
                }
-               if (path != gpath) {
+               if ((path - gpath) > 1) {
                        path--;
                }
                pglob->path_len = path - gpath;