[PATCH] D67166: Win: handle \\?UNC\ prefix in realPathFromHandle (PR43204)

Hans Wennborg via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 4 06:27:03 PDT 2019


hans created this revision.
hans added reviewers: rnk, amccarth.
Herald added a subscriber: hiraditya.
Herald added a project: LLVM.

After r361885, realPathFromHandle() ends up getting called on the working directory on each Clang invocation. This unveiled that the code didn't work for paths on network shares.

For example, if one maps the local dir c:\src\tmp to x:

  net use x: \\localhost\c$\tmp

and run e.g. "clang -c foo.cc" in x:\, realPathFromHandle will get \\?\UNC\localhost\c$\src\tmp\ back from GetFinalPathNameByHandleW, and would strip off the initial \\?\ prefix, ending up with a path that doesn't work.

This patch makes the prefix stripping a little smarter to handle this case.


https://reviews.llvm.org/D67166

Files:
  llvm/lib/Support/Windows/Path.inc


Index: llvm/lib/Support/Windows/Path.inc
===================================================================
--- llvm/lib/Support/Windows/Path.inc
+++ llvm/lib/Support/Windows/Path.inc
@@ -371,16 +371,21 @@
   if (std::error_code EC = realPathFromHandle(H, Buffer))
     return EC;
 
-  const wchar_t *Data = Buffer.data();
+  // Strip the \\?\ prefix. We don't want it ending up in output, and such
+  // paths don't get canonicalized by file APIs.
+  wchar_t *Data = Buffer.data();
   DWORD CountChars = Buffer.size();
-  if (CountChars >= 4) {
-    if (0 == ::memcmp(Data, L"\\\\?\\", 8)) {
-      CountChars -= 4;
-      Data += 4;
-    }
+  if (CountChars >= 7 && ::memcmp(Data, L"\\\\?UNC\\", 14) == 0) {
+    // Convert \\?UNC\foo\bar to \\foo\bar
+    CountChars -= 5;
+    Data += 5;
+    Data[0] = '\\';
+  } else if (CountChars >= 4 && ::memcmp(Data, L"\\\\?\\", 8) == 0) {
+    // Convert \\?\c:\foo to c:\foo
+    CountChars -= 4;
+    Data += 4;
   }
 
-  // Convert the result from UTF-16 to UTF-8.
   return UTF16ToUTF8(Data, CountChars, RealPath);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67166.218675.patch
Type: text/x-patch
Size: 1070 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190904/7ccf75fc/attachment.bin>


More information about the llvm-commits mailing list