From 8a296386e35462e7b2ef064112e0077709c4a400 Mon Sep 17 00:00:00 2001 From: Jyrki Vesterinen Date: Fri, 22 Jul 2016 21:05:08 +0300 Subject: [PATCH] Fix undefined behavior on destroying an event context The destructor of the context class accidentally incremented the iterator twice per iteration. If the number of event handlers was odd, the destructor ended up incrementing the end iterator, which is UB. I rewrote the whole destructor. It's unnecessary to manually remove event handlers from the list because the list will do it automatically when it's destroyed. --- src/events.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/events.cpp b/src/events.cpp index 94b0d59daac..fb4fba13066 100644 --- a/src/events.cpp +++ b/src/events.cpp @@ -132,15 +132,15 @@ void context::set_focus(const sdl_handler* ptr) context::~context() { - if (!handlers.empty()) { - for (handler_list::iterator it = handlers.begin(); it != handlers.end(); ++it) { - if ((*it)->has_joined()) { - (*it)->has_joined_ = false; - } - if ((*it)->has_joined_global()) { - (*it)->has_joined_global_ = false; - } - it = handlers.erase(it); + for (sdl_handler* h : handlers) + { + if (h->has_joined()) + { + h->has_joined_ = false; + } + if (h->has_joined_global()) + { + h->has_joined_global_ = false; } } }