]> source.dussan.org Git - nextcloud-server.git/commitdiff
[stable10] Prevent endless loop in \OC\Files\View::createParentDirectories 2440/head
authorLukas Reschke <lukas@statuscode.ch>
Thu, 1 Dec 2016 15:52:12 +0000 (16:52 +0100)
committerLukas Reschke <lukas@statuscode.ch>
Thu, 1 Dec 2016 15:53:26 +0000 (16:53 +0100)
\OC\Files\View::createParentDirectories was previously prone to an endless loop. If a path such as /foo/existingfile.txt/bar/foo was passed and existingfile.txt existed in foo the loop was never left and running until the PHP process timed out.

This commit changes the logic to a foreach loop over an array and additionally additional error handling using is_file.

Signed-off-by: Lukas Reschke <lukas@statuscode.ch>
lib/private/Files/View.php

index fa6ba20c342f37f33031d2b73a0057c5e46bad6f..ee6542216d285440160d5b9652036463b10a8c7d 100644 (file)
@@ -2120,14 +2120,19 @@ class View {
         * @return bool
         */
        private function createParentDirectories($filePath) {
-               $parentDirectory = dirname($filePath);
-               while(!$this->file_exists($parentDirectory)) {
-                       $result = $this->createParentDirectories($parentDirectory);
-                       if($result === false) {
+               $directoryParts = explode('/', $filePath);
+               $directoryParts = array_filter($directoryParts);
+               foreach($directoryParts as $key => $part) {
+                       $currentPathElements = array_slice($directoryParts, 0, $key);
+                       $currentPath = '/' . implode('/', $currentPathElements);
+                       if($this->is_file($currentPath)) {
                                return false;
                        }
+                       if(!$this->file_exists($currentPath)) {
+                               $this->mkdir($currentPath);
+                       }
                }
-               $this->mkdir($filePath);
+
                return true;
        }
 }