[PATCH] D86564: [Support][Windows] Fix incorrect GetFinalPathNameByHandleW() return value check in realPathFromHandle()

Aleksandr Platonov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 26 06:09:15 PDT 2020


ArcsinX updated this revision to Diff 287948.
ArcsinX added a comment.

Check `GetFinalPathNameByHandleW()` return value for zero first


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D86564/new/

https://reviews.llvm.org/D86564

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
@@ -352,16 +352,16 @@
 static std::error_code realPathFromHandle(HANDLE H,
                                           SmallVectorImpl<wchar_t> &Buffer) {
   DWORD CountChars = ::GetFinalPathNameByHandleW(
-      H, Buffer.begin(), Buffer.capacity() - 1, FILE_NAME_NORMALIZED);
-  if (CountChars > Buffer.capacity()) {
+      H, Buffer.begin(), Buffer.capacity(), FILE_NAME_NORMALIZED);
+  if (CountChars == 0)
+    return mapWindowsError(GetLastError());
+  if (CountChars >= Buffer.capacity()) {
     // The buffer wasn't big enough, try again.  In this case the return value
     // *does* indicate the size of the null terminator.
     Buffer.reserve(CountChars);
     CountChars = ::GetFinalPathNameByHandleW(
-        H, Buffer.data(), Buffer.capacity() - 1, FILE_NAME_NORMALIZED);
+        H, Buffer.data(), Buffer.capacity(), FILE_NAME_NORMALIZED);
   }
-  if (CountChars == 0)
-    return mapWindowsError(GetLastError());
   Buffer.set_size(CountChars);
   return std::error_code();
 }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D86564.287948.patch
Type: text/x-patch
Size: 1179 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200826/23ace755/attachment.bin>


More information about the llvm-commits mailing list