From 8f6d1460109e9acdb9bc35e143e223f513ddfb8d Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Mon, 25 Nov 2024 23:40:50 +0100 Subject: [PATCH 1/2] Fix infinite recursion in TSF --- src/tsf/Implementation.cpp | 16 ++++++++++++++-- src/tsf/Implementation.h | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/tsf/Implementation.cpp b/src/tsf/Implementation.cpp index 234c43df233..4cde664592b 100644 --- a/src/tsf/Implementation.cpp +++ b/src/tsf/Implementation.cpp @@ -84,8 +84,20 @@ void Implementation::Uninitialize() noexcept } } -HWND Implementation::FindWindowOfActiveTSF() const noexcept -{ +HWND Implementation::FindWindowOfActiveTSF() noexcept +{ + // We don't know what ITfContextOwner we're going to get in + // the code below and it may very well be us (this instance). + // It's also possible that our IDataProvider's GetHwnd() + // implementation calls this FindWindowOfActiveTSF() function. + // This can then in infinite recursion because we're calling + // GetWnd() below, which may call GetHwnd(), which may call + // FindWindowOfActiveTSF(), and so on. + // By temporarily clearing the _provider we fix that flaw. + const auto restore = wil::scope_exit([this, provider = std::move(_provider)]() mutable { + _provider = std::move(provider); + }); + wil::com_ptr enumDocumentMgrs; if (FAILED_LOG(_threadMgrEx->EnumDocumentMgrs(enumDocumentMgrs.addressof()))) { diff --git a/src/tsf/Implementation.h b/src/tsf/Implementation.h index 5bf7251f6a0..fecec35cea4 100644 --- a/src/tsf/Implementation.h +++ b/src/tsf/Implementation.h @@ -22,7 +22,7 @@ namespace Microsoft::Console::TSF void Initialize(); void Uninitialize() noexcept; - HWND FindWindowOfActiveTSF() const noexcept; + HWND FindWindowOfActiveTSF() noexcept; void AssociateFocus(IDataProvider* provider); void Focus(IDataProvider* provider); void Unfocus(IDataProvider* provider); From 573756318a9500de77608a4aca7200c0413bb168 Mon Sep 17 00:00:00 2001 From: Leonard Hecker Date: Tue, 26 Nov 2024 01:12:49 +0100 Subject: [PATCH 2/2] Typo --- src/tsf/Implementation.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tsf/Implementation.cpp b/src/tsf/Implementation.cpp index 4cde664592b..f2cd9589336 100644 --- a/src/tsf/Implementation.cpp +++ b/src/tsf/Implementation.cpp @@ -90,7 +90,7 @@ HWND Implementation::FindWindowOfActiveTSF() noexcept // the code below and it may very well be us (this instance). // It's also possible that our IDataProvider's GetHwnd() // implementation calls this FindWindowOfActiveTSF() function. - // This can then in infinite recursion because we're calling + // This can result in infinite recursion because we're calling // GetWnd() below, which may call GetHwnd(), which may call // FindWindowOfActiveTSF(), and so on. // By temporarily clearing the _provider we fix that flaw.