Bug 1376931 Part 4: Before MediaQueryList iteration for event listeners, copy the array before sending any events. draft
authorBrad Werth <bwerth@mozilla.com>
Tue, 19 Sep 2017 17:05:16 -0700
changeset 675197 a5a7799849d5db98479075daa86959101c3b954c
parent 675196 a8a1b08a3e4d6d8881b4aff763fd453f8cd6b6c5
child 675198 8724ae3e98335ccc6d9a94891518e0092506b692
push id83058
push userbwerth@mozilla.com
push dateWed, 04 Oct 2017 23:14:13 +0000
bugs1376931
milestone58.0a1
Bug 1376931 Part 4: Before MediaQueryList iteration for event listeners, copy the array before sending any events. MozReview-Commit-ID: 4eDs6viuxvT
layout/base/nsPresContext.cpp
--- a/layout/base/nsPresContext.cpp
+++ b/layout/base/nsPresContext.cpp
@@ -2112,17 +2112,26 @@ nsPresContext::MediaFeatureValuesChanged
   // flushes.  (We're already running off an event.)
   //
   // Note that we do this after the new style from media queries in
   // style sheets has been computed.
 
   if (!mDocument->MediaQueryLists().isEmpty()) {
     // We build a list of all the notifications we're going to send
     // before we send any of them.
-    for (auto mql : mDocument->MediaQueryLists()) {
+
+    // Copy pointers to all the lists into a new array, in case one of our
+    // notifications modifies the list.
+    nsTArray<RefPtr<mozilla::dom::MediaQueryList>> localMediaQueryLists;
+    for (auto* mql : mDocument->MediaQueryLists()) {
+      localMediaQueryLists.AppendElement(mql);
+    }
+
+    // Now iterate our local array of the lists.
+    for (auto mql : localMediaQueryLists) {
       nsAutoMicroTask mt;
       mql->MaybeNotify();
     }
   }
 }
 
 void
 nsPresContext::PostMediaFeatureValuesChangedEvent()