[llvm] [llvm][Support][Windows] Avoid crash calling remove_directories() (PR #118677)
Pavel Labath via llvm-commits
llvm-commits at lists.llvm.org
Thu Dec 5 00:50:33 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();
----------------
labath wrote:
We generally try to avoid these kinds of deeply nested blocks. It looks like this could be make mostly linear with the use of [early returns](https://llvm.org/docs/CodingStandards.html#use-early-exits-and-continue-to-simplify-code) (possibly together with some helper functions) and `make_scope_exit` to run cleanups.
https://github.com/llvm/llvm-project/pull/118677
More information about the llvm-commits
mailing list