Skip to content

Truly independent web browser

LibWeb: Don’t touch a dead document when middle-button scrolling ends

sideshowbarkere9090d269e81
diff --git a/Libraries/LibWeb/Page/EventHandler.cpp b/Libraries/LibWeb/Page/EventHandler.cpp
index 2a5940a53d0..3759481d065 100644
--- a/Libraries/LibWeb/Page/EventHandler.cpp
+++ b/Libraries/LibWeb/Page/EventHandler.cpp
@@ -798,7 +798,7 @@ EventResult EventHandler::handle_mouseup(CSSPixelPoint visual_viewport_position,
stop_updating_selection();
if (middle_button_autoscrolled && button == UIEvents::MouseButton::Middle)
- m_middle_button_scroll_handler = nullptr;
+ stop_middle_button_scroll();
});
if (should_ignore_device_input_event()) {
@@ -2071,7 +2071,7 @@ EventResult EventHandler::handle_keydown(UIEvents::KeyCode key, u32 modifiers, u
if (key == UIEvents::KeyCode::Key_Escape) {
// NB: Exit autoscroll if active before handling other close requests.
if (m_middle_button_scroll_handler) {
- m_middle_button_scroll_handler = nullptr;
+ stop_middle_button_scroll();
return EventResult::Handled;
}
@@ -3555,7 +3555,7 @@ static bool is_inside_button(GC::Ptr<DOM::Node const> node)
void EventHandler::run_mousedown_default_actions(Layout::BegunRead const& read, DOM::Document& document, CSSPixelPoint visual_viewport_position, unsigned button, unsigned modifiers, int click_count)
{
if (m_middle_button_scroll_handler) {
- m_middle_button_scroll_handler = nullptr;
+ stop_middle_button_scroll();
return;
}
@@ -6056,6 +6056,14 @@ bool EventHandler::has_committed_root_box() const
return document && document->has_committed_viewport_box();
}
+void EventHandler::stop_middle_button_scroll()
+{
+ if (!m_middle_button_scroll_handler)
+ return;
+ m_middle_button_scroll_handler->request_indicator_repaint();
+ m_middle_button_scroll_handler = nullptr;
+}
+
void EventHandler::release_held_scroll_key()
{
m_held_scroll_key.clear();
@@ -6067,8 +6075,8 @@ void EventHandler::clear_per_test_input_state(Badge<Internals::Internals>)
clear_mousedown_tracking();
stop_updating_selection();
release_held_scroll_key();
+ stop_middle_button_scroll();
- m_middle_button_scroll_handler = nullptr;
m_mouse_pointer.prevent_mouse_event = false;
m_additional_pointers.clear();
m_captured_chrome_widget = nullptr;
diff --git a/Libraries/LibWeb/Page/EventHandler.h b/Libraries/LibWeb/Page/EventHandler.h
index 160d4b2d094..2f2c9ffd674 100644
--- a/Libraries/LibWeb/Page/EventHandler.h
+++ b/Libraries/LibWeb/Page/EventHandler.h
@@ -294,6 +294,7 @@ private:
void clear_mousedown_tracking();
void stop_updating_selection();
+ void stop_middle_button_scroll();
enum class HoverUpdateCause : u8 {
PointerMoved,
diff --git a/Libraries/LibWeb/Page/MiddleButtonScrollHandler.cpp b/Libraries/LibWeb/Page/MiddleButtonScrollHandler.cpp
index fcda25d912e..8ff7c814cfc 100644
--- a/Libraries/LibWeb/Page/MiddleButtonScrollHandler.cpp
+++ b/Libraries/LibWeb/Page/MiddleButtonScrollHandler.cpp
@@ -26,10 +26,14 @@ MiddleButtonScrollHandler::MiddleButtonScrollHandler(DOM::Element& container, CS
, m_origin(origin)
, m_mouse_position(origin)
{
- static_cast<DOM::Node&>(m_container_element->document()).set_needs_repaint(InvalidateDisplayList::PaintCommands);
+ request_indicator_repaint();
}
-MiddleButtonScrollHandler::~MiddleButtonScrollHandler()
+// The handler can die along with its navigable in a GC sweep, after its element and document are already gone — so
+// the destructor must not touch them. Instead, EventHandler asks for the repaint when it stops the scroll.
+MiddleButtonScrollHandler::~MiddleButtonScrollHandler() = default;
+
+void MiddleButtonScrollHandler::request_indicator_repaint()
{
static_cast<DOM::Node&>(m_container_element->document()).set_needs_repaint(InvalidateDisplayList::PaintCommands);
}
diff --git a/Libraries/LibWeb/Page/MiddleButtonScrollHandler.h b/Libraries/LibWeb/Page/MiddleButtonScrollHandler.h
index 39cab3a1c0d..7fc4c02a3cc 100644
--- a/Libraries/LibWeb/Page/MiddleButtonScrollHandler.h
+++ b/Libraries/LibWeb/Page/MiddleButtonScrollHandler.h
@@ -27,6 +27,7 @@ public:
void update_mouse_position(CSSPixelPoint position) { m_mouse_position = position; }
void perform_tick();
+ void request_indicator_repaint();
CSSPixelPoint origin() const { return m_origin; }
bool mouse_has_moved_beyond_dead_zone() const { return m_mouse_has_moved_beyond_dead_zone; }
diff --git a/Tests/LibWeb/Crash/Page/middle-button-scroll-in-removed-iframe-gc.html b/Tests/LibWeb/Crash/Page/middle-button-scroll-in-removed-iframe-gc.html
new file mode 100644
index 00000000000..c76bfccfbf7
--- /dev/null
+++ b/Tests/LibWeb/Crash/Page/middle-button-scroll-in-removed-iframe-gc.html
@@ -0,0 +1,25 @@
+<!DOCTYPE html>
+<html class="test-wait">
+<body>
+<script>
+ const iframe = document.createElement("iframe");
+ iframe.style = "position: absolute; left: 0; top: 0; width: 300px; height: 200px; border: none";
+ iframe.srcdoc = "<div style='height: 5000px'>tall</div>";
+ iframe.onload = () => {
+ // Start middle-button autoscroll inside the iframe, and leave it running.
+ internals.mouseDown(100, 100, 1, internals.BUTTON_MIDDLE);
+ internals.mouseUp(100, 100, internals.BUTTON_MIDDLE);
+
+ // Drop the iframe while its navigable's event handler still owns the autoscroll, and collect it.
+ iframe.remove();
+ internals.gc();
+
+ // Sweeping is incremental, so give it time to reach the navigable.
+ setTimeout(() => {
+ internals.gc();
+ setTimeout(() => document.documentElement.classList.remove("test-wait"), 500);
+ }, 500);
+ };
+ document.body.appendChild(iframe);
+</script>
+</body>