Browse Source

chore: avoid crash while notification removal (#43040)

* avoid crash of operation on an invalid entry while erase set iterator.

* fix notification removal crash due to the nullptr presenter

---------

Co-authored-by: bill.shen <[email protected]>
BILL SHEN 8 months ago
parent
commit
77ba40bc01

+ 3 - 1
shell/browser/notifications/notification.cc

@@ -46,7 +46,9 @@ void Notification::NotificationFailed(const std::string& error) {
 void Notification::Remove() {}
 
 void Notification::Destroy() {
-  presenter()->RemoveNotification(this);
+  if (presenter()) {
+    presenter()->RemoveNotification(this);
+  }
 }
 
 }  // namespace electron

+ 5 - 0
shell/browser/notifications/notification_presenter.cc

@@ -27,6 +27,11 @@ base::WeakPtr<Notification> NotificationPresenter::CreateNotification(
 }
 
 void NotificationPresenter::RemoveNotification(Notification* notification) {
+  auto it = notifications_.find(notification);
+  if (it == notifications_.end()) {
+    return;
+  }
+
   notifications_.erase(notification);
   delete notification;
 }