From d11196927acb017c6ea8a741102906041c447778 Mon Sep 17 00:00:00 2001 From: Moonchild Date: Wed, 30 Sep 2020 16:03:36 +0000 Subject: Issue #1643 - Follow-up: Make sure things aren't changed while iterating. This fixes some crashing scenarios. --- dom/base/ResizeObserverController.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'dom/base') diff --git a/dom/base/ResizeObserverController.cpp b/dom/base/ResizeObserverController.cpp index 349cb66ba..d4166155e 100644 --- a/dom/base/ResizeObserverController.cpp +++ b/dom/base/ResizeObserverController.cpp @@ -119,6 +119,10 @@ ResizeObserverController::Notify() return; } + // Hold a strong reference to the document, because otherwise calling + // all active observers on it might yank it out from under us. + RefPtr document(mDocument); + uint32_t shallowestTargetDepth = 0; GatherAllActiveObservations(shallowestTargetDepth); @@ -153,7 +157,7 @@ ResizeObserverController::Notify() nsEventStatus status = nsEventStatus_eIgnore; nsCOMPtr window = - mDocument->GetWindow()->GetCurrentInnerWindow(); + document->GetWindow()->GetCurrentInnerWindow(); if (window) { nsCOMPtr sgo = do_QueryInterface(window); @@ -185,7 +189,11 @@ ResizeObserverController::BroadcastAllActiveObservations() { uint32_t shallowestTargetDepth = UINT32_MAX; - for (auto observer : mResizeObservers) { + // Use a copy of the observers as this invokes the callbacks of the observers + // which could register/unregister observers at will. + nsTArray> tempObservers(mResizeObservers); + + for (auto observer : tempObservers) { uint32_t targetDepth = observer->BroadcastActiveObservations(); -- cgit v1.2.3