Skip to content

Commit bc5973d

Browse files
committed
[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).
1 parent 14a259f commit bc5973d

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

llvm/lib/Support/Windows/Path.inc

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,12 +1387,31 @@ std::error_code remove_directories(const Twine &path, bool IgnoreErrors) {
13871387
Path16.push_back(0);
13881388
Path16.push_back(0);
13891389

1390-
SHFILEOPSTRUCTW shfos = {};
1391-
shfos.wFunc = FO_DELETE;
1392-
shfos.pFrom = Path16.data();
1393-
shfos.fFlags = FOF_NO_UI;
1390+
HRESULT HR = CoInitializeEx(NULL, COINIT_MULTITHREADED);
1391+
if (SUCCEEDED(HR)) {
1392+
IFileOperation *FileOp;
1393+
HR = CoCreateInstance(CLSID_FileOperation, NULL, CLSCTX_ALL,
1394+
IID_PPV_ARGS(&FileOp));
1395+
if (SUCCEEDED(HR)) {
1396+
HR = FileOp->SetOperationFlags(FOF_NO_UI | FOFX_NOCOPYHOOKS);
1397+
if (SUCCEEDED(HR)) {
1398+
IShellItem *ShItem = NULL;
1399+
HR = SHCreateItemFromParsingName(Path16.data(), NULL,
1400+
IID_PPV_ARGS(&ShItem));
1401+
if (SUCCEEDED(HR)) {
1402+
HR = FileOp->DeleteItem(ShItem, NULL);
1403+
ShItem->Release();
1404+
}
1405+
if (SUCCEEDED(HR)) {
1406+
HR = FileOp->PerformOperations();
1407+
}
1408+
}
1409+
FileOp->Release();
1410+
}
1411+
}
1412+
CoUninitialize();
13941413

1395-
int result = ::SHFileOperationW(&shfos);
1414+
int result = FAILED(HR) ? HRESULT_CODE(HR) : 0;
13961415
if (result != 0 && !IgnoreErrors)
13971416
return mapWindowsError(result);
13981417
return std::error_code();

0 commit comments

Comments
 (0)