Skip to content

Commit

Permalink
Fix infinite recursion in TSF (microsoft#18248)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
lhecker authored Nov 26, 2024
1 parent adac608 commit 9243104
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/tsf/Implementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 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.
const auto restore = wil::scope_exit([this, provider = std::move(_provider)]() mutable {
_provider = std::move(provider);
});

wil::com_ptr<IEnumTfDocumentMgrs> enumDocumentMgrs;
if (FAILED_LOG(_threadMgrEx->EnumDocumentMgrs(enumDocumentMgrs.addressof())))
{
Expand Down
2 changes: 1 addition & 1 deletion src/tsf/Implementation.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 9243104

Please sign in to comment.