From 33500bb6dbbeddc36ad8c12643a5a536e3a78ec9 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 27 May 2023 18:37:02 +0200 Subject: [PATCH] LibWeb: Protect against null deref during Web::Page initialization BrowsingContext::set_active_document() may end up asking for the Page's top level browsing context before Page has one. Do a workaround for now where we have an API to ask if it's initialized. Long-term we should find a cleaner solution. --- Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp | 2 +- Userland/Libraries/LibWeb/Page/Page.cpp | 5 +++++ Userland/Libraries/LibWeb/Page/Page.h | 3 +++ 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp index 50785f8235a..074a0de2784 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp @@ -532,7 +532,7 @@ void BrowsingContext::set_active_document(JS::NonnullGCPtr docume // AD-HOC: document->set_browsing_context(this); - if (m_page && this == &m_page->top_level_browsing_context()) + if (m_page && m_page->top_level_browsing_context_is_initialized() && this == &m_page->top_level_browsing_context()) m_page->client().page_did_change_title(document->title()); if (previously_active_document && previously_active_document != document.ptr()) diff --git a/Userland/Libraries/LibWeb/Page/Page.cpp b/Userland/Libraries/LibWeb/Page/Page.cpp index 9b1a332f101..b30f3c62f96 100644 --- a/Userland/Libraries/LibWeb/Page/Page.cpp +++ b/Userland/Libraries/LibWeb/Page/Page.cpp @@ -158,6 +158,11 @@ bool Page::handle_keyup(KeyCode key, unsigned modifiers, u32 code_point) return focused_context().event_handler().handle_keyup(key, modifiers, code_point); } +bool Page::top_level_browsing_context_is_initialized() const +{ + return m_top_level_browsing_context; +} + HTML::BrowsingContext& Page::top_level_browsing_context() { return *m_top_level_browsing_context; diff --git a/Userland/Libraries/LibWeb/Page/Page.h b/Userland/Libraries/LibWeb/Page/Page.h index 3bc9c474ba9..fa8368cfd47 100644 --- a/Userland/Libraries/LibWeb/Page/Page.h +++ b/Userland/Libraries/LibWeb/Page/Page.h @@ -44,6 +44,9 @@ public: PageClient& client() { return m_client; } PageClient const& client() const { return m_client; } + // FIXME: This is a hack. + bool top_level_browsing_context_is_initialized() const; + HTML::BrowsingContext& top_level_browsing_context(); HTML::BrowsingContext const& top_level_browsing_context() const;