From ab45b7660aec83978871ade009846dcb8c34579f Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 20 Aug 2023 23:25:02 +0300 Subject: [PATCH] Clear saved input focus if the widget is removed from the container Checking the saved focus widget's parent in focus_in can lead to use after free if the widget was deleted in the meantime. --- source/container.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/container.cpp b/source/container.cpp index 393973c..9ef46f1 100644 --- a/source/container.cpp +++ b/source/container.cpp @@ -35,6 +35,8 @@ void Container::remove(Widget &wdg) if(i==children.end()) throw hierarchy_error("widget not in container"); + if(&wdg==saved_input_focus) + saved_input_focus = nullptr; wdg.set_parent(nullptr); delete *i; children.erase(i); @@ -317,7 +319,7 @@ bool Container::character(wchar_t ch) void Container::focus_in() { - if(saved_input_focus && saved_input_focus->get_parent()==this) + if(saved_input_focus) set_input_focus(saved_input_focus); Widget::focus_in(); } -- 2.45.2