From d1ec81e4b43d0e473f03bf84b989f2189bfac82f Mon Sep 17 00:00:00 2001 From: simutisernestas <35775651+simutisernestas@users.noreply.github.com> Date: Thu, 20 May 2021 01:26:43 +0300 Subject: [PATCH] Avoid using invalid std::list iterators (#293) + Fix accessing free'd resources #386 (#419) * fix accessing freed resources (#386) * Avoid using invalid std::list iterators (#293) Signed-off-by: Michael Carroll Co-authored-by: Kazunari Tanaka Co-authored-by: Michael Carroll --- tf2_ros/include/tf2_ros/message_filter.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tf2_ros/include/tf2_ros/message_filter.h b/tf2_ros/include/tf2_ros/message_filter.h index e3d5e8cb3..ab65514fd 100644 --- a/tf2_ros/include/tf2_ros/message_filter.h +++ b/tf2_ros/include/tf2_ros/message_filter.h @@ -459,16 +459,17 @@ class MessageFilter : public MessageFilterBase, public message_filters::SimpleFi { namespace mt = message_filters::message_traits; - // find the message this request is associated with - typename L_MessageInfo::iterator msg_it = messages_.begin(); - typename L_MessageInfo::iterator msg_end = messages_.end(); - MEvent saved_event; + bool event_found = false; { // We will be accessing and mutating messages now, require unique lock std::unique_lock lock(messages_mutex_); + // find the message this request is associated with + typename L_MessageInfo::iterator msg_it = messages_.begin(); + typename L_MessageInfo::iterator msg_end = messages_.end(); + for (; msg_it != msg_end; ++msg_it) { MessageInfo & info = *msg_it; auto handle_it = std::find(info.handles.begin(), info.handles.end(), handle); @@ -479,15 +480,14 @@ class MessageFilter : public MessageFilterBase, public message_filters::SimpleFi saved_event = msg_it->event; messages_.erase(msg_it); --message_count_; - } else { - msg_it = msg_end; - } + event_found = true; + } break; } } } - if (msg_it == msg_end) { + if (!event_found) { return; }