Skip to content

Commit

Permalink
Guard Control UpdateAppearance/Settings against UAF (#17770)
Browse files Browse the repository at this point in the history
When you close a window, it naturally loses focus.

We were trying to use members of the control to update its appearance on
focus loss after it got torn down.

Closes #17520

(cherry picked from commit eabebc4)
Service-Card-Id: PVTI_lADOAF3p4s4AmhmszgSIM6I
Service-Version: 1.21
  • Loading branch information
DHowett committed Aug 22, 2024
1 parent f2e9041 commit cf7a661
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,14 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// terminal.
co_await wil::resume_foreground(Dispatcher());

_core.UpdateSettings(settings, unfocusedAppearance);
if (auto strongThis{ weakThis.get() })
{
_core.UpdateSettings(settings, unfocusedAppearance);

_UpdateSettingsFromUIThread();
_UpdateSettingsFromUIThread();

_UpdateAppearanceFromUIThread(_focused ? _core.FocusedAppearance() : _core.UnfocusedAppearance());
_UpdateAppearanceFromUIThread(_focused ? _core.FocusedAppearance() : _core.UnfocusedAppearance());
}
}

// Method Description:
Expand All @@ -687,10 +690,15 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// - newAppearance: the new appearance to set
winrt::fire_and_forget TermControl::UpdateAppearance(IControlAppearance newAppearance)
{
auto weakThis{ get_weak() };

// Dispatch a call to the UI thread
co_await wil::resume_foreground(Dispatcher());

_UpdateAppearanceFromUIThread(newAppearance);
if (auto strongThis{ weakThis.get() })
{
_UpdateAppearanceFromUIThread(newAppearance);
}
}

// Method Description:
Expand Down

0 comments on commit cf7a661

Please sign in to comment.