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

Dmitry Vasilyev via llvm-commits llvm-commits at lists.llvm.org
Fri Dec 6 05:46:13 PST 2024


================
@@ -1387,12 +1387,33 @@ 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_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
+  if (SUCCEEDED(HR)) {
+    IFileOperation *FileOp = NULL;
+    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)) {
+        PIDLIST_ABSOLUTE PIDL = ILCreateFromPathW(Path16.data());
+        IShellItem *ShItem = NULL;
+        HR = SHCreateItemFromIDList(PIDL, IID_PPV_ARGS(&ShItem));
+        if (SUCCEEDED(HR)) {
+          HR = FileOp->DeleteItem(ShItem, NULL);
+          if (SUCCEEDED(HR)) {
+            HR = FileOp->PerformOperations();
----------------
slydiman wrote:

How to ensure the release order w/o scope {}?
```
FileOp->Release();
CoUninitialize();
```
[Use Early Exits and continue to Simplify Code](https://llvm.org/docs/CodingStandards.html#id40) means use it when it makes sense, does not complicate the code, and does not make it less stable and predictable.

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


More information about the llvm-commits mailing list