From 0629040c7cb9f46d132599563735f4fffe5f03e0 Mon Sep 17 00:00:00 2001 From: balaraj74 Date: Sun, 26 Oct 2025 13:57:14 +0530 Subject: [PATCH] Fix crash when canceling file picker in profile settings Wraps SetClientGuid, SetFileTypes, and related dialog setup calls in try-catch blocks to prevent unhandled exceptions when the user cancels the file selection dialog or when dialog configuration fails. Previously, if these operations threw an exception (which could happen during cancellation or in certain system states), it would crash the terminal. Now these operations are treated as non-fatal, allowing the dialog to still open even if some configuration steps fail. Fixes #19501 --- .../TerminalSettingsEditor/Profiles_Base.cpp | 32 ++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/cascadia/TerminalSettingsEditor/Profiles_Base.cpp b/src/cascadia/TerminalSettingsEditor/Profiles_Base.cpp index 774ecbb34de..75574a700a2 100644 --- a/src/cascadia/TerminalSettingsEditor/Profiles_Base.cpp +++ b/src/cascadia/TerminalSettingsEditor/Profiles_Base.cpp @@ -116,16 +116,24 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation static constexpr winrt::guid clientGuidExecutables{ 0x2E7E4331, 0x0800, 0x48E6, { 0xB0, 0x17, 0xA1, 0x4C, 0xD8, 0x73, 0xDD, 0x58 } }; const auto parentHwnd{ reinterpret_cast(_windowRoot.GetHostingWindow()) }; auto path = co_await OpenFilePicker(parentHwnd, [](auto&& dialog) { - THROW_IF_FAILED(dialog->SetClientGuid(clientGuidExecutables)); + try + { + THROW_IF_FAILED(dialog->SetClientGuid(clientGuidExecutables)); + } + CATCH_LOG(); // non-fatal try { auto folderShellItem{ winrt::capture(&SHGetKnownFolderItem, FOLDERID_ComputerFolder, KF_FLAG_DEFAULT, nullptr) }; dialog->SetDefaultFolder(folderShellItem.get()); } CATCH_LOG(); // non-fatal - THROW_IF_FAILED(dialog->SetFileTypes(ARRAYSIZE(supportedFileTypes), supportedFileTypes)); - THROW_IF_FAILED(dialog->SetFileTypeIndex(1)); // the array is 1-indexed - THROW_IF_FAILED(dialog->SetDefaultExtension(L"exe;cmd;bat")); + try + { + THROW_IF_FAILED(dialog->SetFileTypes(ARRAYSIZE(supportedFileTypes), supportedFileTypes)); + THROW_IF_FAILED(dialog->SetFileTypeIndex(1)); // the array is 1-indexed + THROW_IF_FAILED(dialog->SetDefaultExtension(L"exe;cmd;bat")); + } + CATCH_LOG(); // non-fatal }); if (!path.empty()) @@ -152,7 +160,11 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation const auto parentHwnd{ reinterpret_cast(_windowRoot.GetHostingWindow()) }; auto folder = co_await OpenFilePicker(parentHwnd, [](auto&& dialog) { static constexpr winrt::guid clientGuidFolderPicker{ 0xAADAA433, 0xB04D, 0x4BAE, { 0xB1, 0xEA, 0x1E, 0x6C, 0xD1, 0xCD, 0xA6, 0x8B } }; - THROW_IF_FAILED(dialog->SetClientGuid(clientGuidFolderPicker)); + try + { + THROW_IF_FAILED(dialog->SetClientGuid(clientGuidFolderPicker)); + } + CATCH_LOG(); // non-fatal try { auto folderShellItem{ winrt::capture(&SHGetKnownFolderItem, FOLDERID_ComputerFolder, KF_FLAG_DEFAULT, nullptr) }; @@ -160,9 +172,13 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation } CATCH_LOG(); // non-fatal - DWORD flags{}; - THROW_IF_FAILED(dialog->GetOptions(&flags)); - THROW_IF_FAILED(dialog->SetOptions(flags | FOS_PICKFOLDERS)); // folders only + try + { + DWORD flags{}; + THROW_IF_FAILED(dialog->GetOptions(&flags)); + THROW_IF_FAILED(dialog->SetOptions(flags | FOS_PICKFOLDERS)); // folders only + } + CATCH_LOG(); // non-fatal }); if (!folder.empty())