From 1770a1ea210074fef6e380bc0dfbf9934d78c560 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Mon, 20 Apr 2020 11:49:25 -0700 Subject: [PATCH] Prevent invalid decrement in tool manager Iterating over the tool stack, we potentially remove the current iterator. If this removal happens at the beginning of the toolstack, we cannot decrement the iterator to a position before the stack without creating an invalid state. --- common/tool/tool_manager.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/common/tool/tool_manager.cpp b/common/tool/tool_manager.cpp index 0c5915fb93..175b823889 100644 --- a/common/tool/tool_manager.cpp +++ b/common/tool/tool_manager.cpp @@ -648,16 +648,22 @@ bool TOOL_MANAGER::dispatchInternal( const TOOL_EVENT& aEvent ) wxLogTrace( kicadTraceToolStack, "TOOL_MANAGER::dispatchInternal %s", aEvent.Format() ); + auto it = m_activeTools.begin(); + // iterate over active tool stack - for( auto it = m_activeTools.begin(); it != m_activeTools.end(); ++it ) + while( it != m_activeTools.end() ) { TOOL_STATE* st = m_toolIdIndex[*it]; + bool increment = true; // forward context menu events to the tool that created the menu if( aEvent.IsChoiceMenu() ) { if( *it != m_menuOwner ) + { + ++it; continue; + } } // the tool state handler is waiting for events (i.e. called Wait() method) @@ -685,7 +691,7 @@ bool TOOL_MANAGER::dispatchInternal( const TOOL_EVENT& aEvent ) if( end ) { it = finishTool( st ); - --it; + increment = false; } } @@ -701,6 +707,9 @@ bool TOOL_MANAGER::dispatchInternal( const TOOL_EVENT& aEvent ) } } } + + if( increment ) + ++it; } for( auto& state : m_toolState )