From ac4978cf8ad97f31e5b15f6c26efc7451a64a18c Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Wed, 23 Sep 2020 18:42:35 +0100 Subject: [PATCH] Some defensive code which may prevent a freed-memory-access. Fixes https://gitlab.com/kicad/code/kicad/issues/5747 --- common/rc_item.cpp | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/common/rc_item.cpp b/common/rc_item.cpp index 6cceac68af..9b43141a00 100644 --- a/common/rc_item.cpp +++ b/common/rc_item.cpp @@ -432,18 +432,32 @@ void RC_TREE_MODEL::DeleteItems( bool aCurrentOnly, bool aIncludeExclusions, boo return; } + int lastGood = -1; + bool found = false; + + if( m_view ) + m_view->UnselectAll(); + for( int i = m_rcItemsProvider->GetCount() - 1; i >= 0; --i ) { - std::shared_ptr rcItem = m_rcItemsProvider->GetItem( i ); - MARKER_BASE* marker = rcItem->GetParent(); - bool excluded = marker ? marker->IsExcluded() : false; + std::shared_ptr rcItem = m_rcItemsProvider->GetItem( i ); + MARKER_BASE* marker = rcItem->GetParent(); + bool excluded = marker ? marker->IsExcluded() : false; if( aCurrentOnly && rcItem != current_item ) + { + if( found && lastGood >= 0 ) + break; + + lastGood = i; continue; + } if( excluded && !aIncludeExclusions ) continue; + found = true; + wxDataViewItem markerItem = ToItem( m_tree[i] ); wxDataViewItemArray childItems; wxDataViewItem parentItem = ToItem( m_tree[i]->m_Parent ); @@ -466,6 +480,9 @@ void RC_TREE_MODEL::DeleteItems( bool aCurrentOnly, bool aIncludeExclusions, boo m_rcItemsProvider->DeleteItem( i, aDeep && aCurrentOnly ); } + if( m_view && aCurrentOnly && lastGood >= 0 ) + m_view->Select( ToItem( m_tree[ lastGood ] ) ); + if( !aCurrentOnly ) { m_rcItemsProvider->DeleteAllItems( aIncludeExclusions, aDeep );