From e3ad5731f7cb3c295e6b6fee644fe92f5a0ad613 Mon Sep 17 00:00:00 2001 From: MacDue Date: Sun, 18 Dec 2022 22:45:05 +0000 Subject: [PATCH] Browser: Close inspectors and JS console when tab closes Keeping these around can lead to use-after-frees and crashes. --- Userland/Applications/Browser/Tab.cpp | 18 ++++++++++++++++++ Userland/Applications/Browser/Tab.h | 3 ++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/Userland/Applications/Browser/Tab.cpp b/Userland/Applications/Browser/Tab.cpp index 53c73de795..da59152a70 100644 --- a/Userland/Applications/Browser/Tab.cpp +++ b/Userland/Applications/Browser/Tab.cpp @@ -42,6 +42,11 @@ namespace Browser { +Tab::~Tab() +{ + close_sub_widgets(); +} + URL url_from_user_input(DeprecatedString const& input) { if (input.starts_with('?') && !g_search_engine.is_empty()) @@ -674,6 +679,19 @@ void Tab::show_inspector_window(Browser::Tab::InspectorTarget inspector_target) window->move_to_front(); } +void Tab::close_sub_widgets() +{ + auto close_widget_window = [](auto& widget) { + if (widget) { + auto* window = widget->window(); + window->close(); + } + }; + close_widget_window(m_dom_inspector_widget); + close_widget_window(m_console_widget); + close_widget_window(m_storage_widget); +} + void Tab::show_console_window() { if (!m_console_widget) { diff --git a/Userland/Applications/Browser/Tab.h b/Userland/Applications/Browser/Tab.h index 80fba79e86..8f2bd484ea 100644 --- a/Userland/Applications/Browser/Tab.h +++ b/Userland/Applications/Browser/Tab.h @@ -34,7 +34,7 @@ class Tab final : public GUI::Widget { friend class BrowserWindow; public: - virtual ~Tab() override = default; + virtual ~Tab() override; URL url() const; @@ -106,6 +106,7 @@ private: void start_download(const URL& url); void view_source(const URL& url, DeprecatedString const& source); void update_status(Optional text_override = {}, i32 count_waiting = 0); + void close_sub_widgets(); enum class MayAppendTLD { No,