[llvm] [Windows] Avoid crash calling remove_directories() (PR #118677)

Dmitry Vasilyev via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 4 10:08:05 PST 2024


https://github.com/slydiman created https://github.com/llvm/llvm-project/pull/118677

We faced an unexpected crash in SHELL32_CallFileCopyHooks() on the buildbot [lldb-remote-linux-win](https://lab.llvm.org/staging/#/builders/197/builds/1066). The host is Windows Server 2022 w/o any 3rd party shell extensions. See #118032 for more details.
Based on [this article](https://devblogs.microsoft.com/oldnewthing/20120330-00/?p=7963).

>From bc5973d214d1761c4a433f3f794bb7854d7d342b Mon Sep 17 00:00:00 2001
From: Dmitry Vasilyev <dvassiliev at accesssoftek.com>
Date: Wed, 4 Dec 2024 13:30:21 +0400
Subject: [PATCH] [Windows] Avoid crash calling remove_directories()

We faced an unexpected crash in SHELL32_CallFileCopyHooks() on the buildbot [lldb-remote-linux-win](https://lab.llvm.org/staging/#/builders/197/builds/1066).
The host is Windows Server 2022 w/o any 3rd party shell extensions.
See #118032 for more details.
Based on [this article](https://devblogs.microsoft.com/oldnewthing/20120330-00/?p=7963).
---
 llvm/lib/Support/Windows/Path.inc | 29 ++++++++++++++++++++++++-----
 1 file changed, 24 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Support/Windows/Path.inc b/llvm/lib/Support/Windows/Path.inc
index c4bd5e24723517..ecea1efefe0098 100644
--- a/llvm/lib/Support/Windows/Path.inc
+++ b/llvm/lib/Support/Windows/Path.inc
@@ -1387,12 +1387,31 @@ std::error_code remove_directories(const Twine &path, bool IgnoreErrors) {
   Path16.push_back(0);
   Path16.push_back(0);
 
-  SHFILEOPSTRUCTW shfos = {};
-  shfos.wFunc = FO_DELETE;
-  shfos.pFrom = Path16.data();
-  shfos.fFlags = FOF_NO_UI;
+  HRESULT HR = CoInitializeEx(NULL, COINIT_MULTITHREADED);
+  if (SUCCEEDED(HR)) {
+    IFileOperation *FileOp;
+    HR = CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL,
+                          IID_PPV_ARGS(&FileOp));
+    if (SUCCEEDED(HR)) {
+      HR = FileOp->SetOperationFlags(FOF_NO_UI | FOFX_NOCOPYHOOKS);
+      if (SUCCEEDED(HR)) {
+        IShellItem *ShItem = NULL;
+        HR = SHCreateItemFromParsingName(Path16.data(), NULL,
+                                         IID_PPV_ARGS(&ShItem));
+        if (SUCCEEDED(HR)) {
+          HR = FileOp->DeleteItem(ShItem, NULL);
+          ShItem->Release();
+        }
+        if (SUCCEEDED(HR)) {
+          HR = FileOp->PerformOperations();
+        }
+      }
+      FileOp->Release();
+    }
+  }
+  CoUninitialize();
 
-  int result = ::SHFileOperationW(&shfos);
+  int result = FAILED(HR) ? HRESULT_CODE(HR) : 0;
   if (result != 0 && !IgnoreErrors)
     return mapWindowsError(result);
   return std::error_code();



More information about the llvm-commits mailing list