From 2045d88f2199a649c3fe34e8b2b6f1244542087c Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Wed, 18 Sep 2013 17:19:13 -0300 Subject: [PATCH] [Notification] Fix wrong loop in OnDetailedChanged The loop was missing incrementing the iterator. This led to an infinite loop, and then to crash in the extension process. This patch experiments with std::find_if to avoid having to write the raw loop at all. --- notification/mobile/notification_manager.cc | 18 +++++++++++++++++- notification/mobile/notification_manager.h | 9 --------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/notification/mobile/notification_manager.cc b/notification/mobile/notification_manager.cc index 2fcf480..4af3538 100644 --- a/notification/mobile/notification_manager.cc +++ b/notification/mobile/notification_manager.cc @@ -4,6 +4,8 @@ #include "notification/mobile/notification_manager.h" +#include + NotificationManager::NotificationManager() { notification_register_detailed_changed_cb( OnDetailedChanged, reinterpret_cast(this)); @@ -59,6 +61,19 @@ void NotificationManager::DetachClient(NotificationClient* client) { } } +namespace { + +template +struct MatchPrivID { + explicit MatchPrivID(int priv_id) : priv_id(priv_id) {} + bool operator()(const typename T::value_type& value) { + return value.second.priv_id == priv_id; + } + int priv_id; +}; + +} // namespace + void NotificationManager::OnDetailedChanged( notification_type_e type, notification_op* op_list, int num_op) { // This function gets warned about every notification event for every @@ -69,7 +84,8 @@ void NotificationManager::OnDetailedChanged( if (operation->type != NOTIFICATION_OP_DELETE) continue; - IDMap::iterator it = FindByPrivID(operation->priv_id); + IDMap::iterator it = std::find_if(id_map_.begin(), id_map_.end(), + MatchPrivID(operation->priv_id)); if (it == id_map_.end()) continue; diff --git a/notification/mobile/notification_manager.h b/notification/mobile/notification_manager.h index 8cab3e9..813d6d3 100644 --- a/notification/mobile/notification_manager.h +++ b/notification/mobile/notification_manager.h @@ -69,15 +69,6 @@ class NotificationManager { }; typedef std::map IDMap; - IDMap::iterator FindByPrivID(int priv_id) { - IDMap::iterator it = id_map_.begin(); - while (it != id_map_.end()) { - if (it->second.priv_id == priv_id) - break; - } - return it; - } - IDMap id_map_; }; -- 2.7.4