Skip to content

Commit 9243104

Browse files
authored
Fix infinite recursion in TSF (microsoft#18248)
The current `FindWindowOfActiveTSF` implementation can result in infinite recursion which we must guard again. This change is not tested as I don't know how to trigger the issue to begin with (a missing CoreInput thread).
1 parent adac608 commit 9243104

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/tsf/Implementation.cpp

+14-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,20 @@ void Implementation::Uninitialize() noexcept
8484
}
8585
}
8686

87-
HWND Implementation::FindWindowOfActiveTSF() const noexcept
88-
{
87+
HWND Implementation::FindWindowOfActiveTSF() noexcept
88+
{
89+
// We don't know what ITfContextOwner we're going to get in
90+
// the code below and it may very well be us (this instance).
91+
// It's also possible that our IDataProvider's GetHwnd()
92+
// implementation calls this FindWindowOfActiveTSF() function.
93+
// This can result in infinite recursion because we're calling
94+
// GetWnd() below, which may call GetHwnd(), which may call
95+
// FindWindowOfActiveTSF(), and so on.
96+
// By temporarily clearing the _provider we fix that flaw.
97+
const auto restore = wil::scope_exit([this, provider = std::move(_provider)]() mutable {
98+
_provider = std::move(provider);
99+
});
100+
89101
wil::com_ptr<IEnumTfDocumentMgrs> enumDocumentMgrs;
90102
if (FAILED_LOG(_threadMgrEx->EnumDocumentMgrs(enumDocumentMgrs.addressof())))
91103
{

src/tsf/Implementation.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ namespace Microsoft::Console::TSF
2222

2323
void Initialize();
2424
void Uninitialize() noexcept;
25-
HWND FindWindowOfActiveTSF() const noexcept;
25+
HWND FindWindowOfActiveTSF() noexcept;
2626
void AssociateFocus(IDataProvider* provider);
2727
void Focus(IDataProvider* provider);
2828
void Unfocus(IDataProvider* provider);

0 commit comments

Comments
 (0)