From 552815d48617c08a6ec29f22c7ecdf1906988e11 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Sat, 15 Jun 2019 17:47:18 -0700 Subject: [PATCH] tool_mgr: Don't invalidate our own iterators Re-arranging the stack will invalidate the iterator that is removed and inserted (begin()). Because this is not a threaded operation, we can only do it to ourselves, so check that the operation isn't a NOP before performing. Fixes: lp:1832930 * https://bugs.launchpad.net/kicad/+bug/1832930 --- common/tool/tool_manager.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/common/tool/tool_manager.cpp b/common/tool/tool_manager.cpp index c3a745543d..54772d12cf 100644 --- a/common/tool/tool_manager.cpp +++ b/common/tool/tool_manager.cpp @@ -2,6 +2,7 @@ * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2013-2018 CERN + * Copyright (C) 2019 KiCad Developers, see AUTHORS.txt for contributors. * @author Tomasz Wlostowski * @author Maciej Suminski * @@ -362,11 +363,20 @@ bool TOOL_MANAGER::runTool( TOOL_BASE* aTool ) static_cast( aTool )->resetTransitions(); // If the tool is already active, bring it to the top of the active tools stack - if( isActive( aTool ) ) + if( isActive( aTool ) && m_activeTools.size() > 1 ) { - m_activeTools.erase( std::find( m_activeTools.begin(), m_activeTools.end(), id ) ); - m_activeTools.push_front( id ); - return false; + auto it = std::find( m_activeTools.begin(), m_activeTools.end(), id ); + + if( it != m_activeTools.end() ) + { + if( it != m_activeTools.begin() ) + { + m_activeTools.erase( it ); + m_activeTools.push_front( id ); + } + + return false; + } } setActiveState( m_toolIdIndex[id] );