https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88884

            Bug ID: 88884
           Summary: std::filesystem::absolute("//") does not produce an
                    absolute path on mingw
           Product: gcc
           Version: 9.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: libstdc++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: redi at gcc dot gnu.org
  Target Milestone: ---

The current implementation of filesystem::absolute uses GetFullPathNameW which
turns a path like "//" into "\\", and "////a" into "\\\\a", which are not
considered absolute by the filesystem::path::is_absolute() function.

The Windows function might be interpreting it as a UNC path (probably an
invalid one) but the std::filesystem library doesn't support UNC names. We
might need special handling in filesystem::absolute, something like:

--- a/libstdc++-v3/src/c++17/fs_ops.cc
+++ b/libstdc++-v3/src/c++17/fs_ops.cc
@@ -86,13 +86,24 @@ fs::absolute(const path& p, error_code& ec)
       return ret;
     }
 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
-  const wstring& s = p.native();
+  const wstring& native = p.native();
+  const wchar_t* s = native.c_str();
+  const auto pos = native.find_first_not_of(L"/\\");
+  // If there are multiple directory separators at the start,
+  // skip all but one of them:
+  if (pos > 1)
+    {
+      if (pos == native.npos)
+       s += native.length() - 1;
+      else
+       s += pos - 1;
+    }
   uint32_t len = 1024;
   wstring buf;
   do
     {
       buf.resize(len);
-      len = GetFullPathNameW(s.c_str(), len, buf.data(), nullptr);
+      len = GetFullPathNameW(s, len, buf.data(), nullptr);
     }
   while (len > buf.size());

Reply via email to