From 80ba02caf5cca8b5da32c40a04fa0ae997c09057 Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Thu, 8 Oct 2020 14:09:16 +0100 Subject: [PATCH] Fix iterator bug in group selection filtering. Fixes https://gitlab.com/kicad/code/kicad/issues/5804 --- pcbnew/tools/edit_tool.cpp | 15 ++++++++------- pcbnew/tools/selection_tool.cpp | 17 +++++++++++------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/pcbnew/tools/edit_tool.cpp b/pcbnew/tools/edit_tool.cpp index 5f0296f7af..a3c93c162b 100644 --- a/pcbnew/tools/edit_tool.cpp +++ b/pcbnew/tools/edit_tool.cpp @@ -1311,13 +1311,14 @@ int EDIT_TOOL::MoveExact( const TOOL_EVENT& aEvent ) m_commit->Modify( item ); if( item->Type() == PCB_GROUP_T ) - { - static_cast( item )->RunOnDescendants( - [&]( BOARD_ITEM* bItem ) - { - m_commit->Modify( bItem ); - }); - } + { + PCB_GROUP* group = static_cast( item ); + + group->RunOnDescendants( [&]( BOARD_ITEM* bItem ) + { + m_commit->Modify( bItem ); + }); + } } item->Move( translation ); diff --git a/pcbnew/tools/selection_tool.cpp b/pcbnew/tools/selection_tool.cpp index fc920f79d3..3fbd09a4f0 100644 --- a/pcbnew/tools/selection_tool.cpp +++ b/pcbnew/tools/selection_tool.cpp @@ -2576,23 +2576,28 @@ void SELECTION_TOOL::FilterCollectorForGroups( GENERAL_COLLECTOR& aCollector ) c std::unordered_set toAdd; // If any element is a member of a group, replace those elements with the top containing group. - for( int j = 0; j < aCollector.GetCount(); ++j ) + for( int j = 0; j < aCollector.GetCount(); ) { - PCB_GROUP* aTop = PCB_GROUP::TopLevelGroup( aCollector[j], m_enteredGroup ); + BOARD_ITEM* item = aCollector[j]; + PCB_GROUP* aTop = PCB_GROUP::TopLevelGroup( item, m_enteredGroup ); if( aTop != NULL ) { - if( aTop != aCollector[j] ) + if( aTop != item ) { toAdd.insert( aTop ); - aCollector.Remove( aCollector[j] ); + aCollector.Remove( item ); + continue; } } - else if( m_enteredGroup && !PCB_GROUP::WithinScope( aCollector[j], m_enteredGroup ) ) + else if( m_enteredGroup && !PCB_GROUP::WithinScope( item, m_enteredGroup ) ) { // If a group is entered, disallow selections of objects outside the group. - aCollector.Remove( aCollector[j] ); + aCollector.Remove( item ); + continue; } + + ++j; } for( BOARD_ITEM* item : toAdd )