[libcxx] Avoid infinite recursion in create_directories, if the root directory doesn...
authorMartin Storsjö <martin@martin.st>
Sat, 27 Feb 2021 17:12:25 +0000 (19:12 +0200)
committerMartin Storsjö <martin@martin.st>
Fri, 5 Mar 2021 08:49:01 +0000 (10:49 +0200)
Differential Revision: https://reviews.llvm.org/D97618

libcxx/src/filesystem/operations.cpp
libcxx/test/std/input.output/filesystems/fs.op.funcs/fs.op.create_directories/create_directories.pass.cpp

index f112168..35f6937 100644 (file)
@@ -1019,6 +1019,8 @@ bool __create_directories(const path& p, error_code* ec) {
     if (not status_known(parent_st))
       return err.report(m_ec);
     if (not exists(parent_st)) {
+      if (parent == p)
+        return err.report(errc::invalid_argument);
       __create_directories(parent, ec);
       if (ec && *ec) {
         return false;
index 9ce450a..54820ca 100644 (file)
@@ -138,4 +138,18 @@ TEST_CASE(dest_final_part_is_file)
     TEST_CHECK(!exists(dir));
 }
 
+#ifdef _WIN32
+TEST_CASE(nonexistent_root)
+{
+    std::error_code ec = GetTestEC();
+    // If Q:\ doesn't exist, create_directories would try to recurse upwards
+    // to parent_path() until it finds a directory that does exist. As the
+    // whole path is the root name, parent_path() returns itself, and it
+    // would recurse indefinitely, unless the recursion is broken.
+    if (!exists("Q:\\"))
+       TEST_CHECK(fs::create_directories("Q:\\", ec) == false);
+    TEST_CHECK(fs::create_directories("\\\\nonexistentserver", ec) == false);
+}
+#endif
+
 TEST_SUITE_END()