From ee343e126307d14460b818c810ca2e86e0ed9980 Mon Sep 17 00:00:00 2001 From: Seth Hillbrand Date: Fri, 15 Sep 2023 15:42:33 -0700 Subject: [PATCH] Fix crash when duplicating symbol Adding symbol to the screen can change the library reference. But when we are pasting, we want the library reference for annotation, so we need to keep a map of old->new libsymbols and fixup the pasted symbols before using Fixes https://gitlab.com/kicad/code/kicad/-/issues/15419 --- eeschema/tools/sch_editor_control.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/eeschema/tools/sch_editor_control.cpp b/eeschema/tools/sch_editor_control.cpp index caef36d9eb..e922e148b7 100644 --- a/eeschema/tools/sch_editor_control.cpp +++ b/eeschema/tools/sch_editor_control.cpp @@ -1574,6 +1574,9 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent ) std::map itemMap; hierarchy.FillItemMap( itemMap ); + // Keep track of updated library symbols + std::map libItemMap; + // Keep track of pasted sheets and symbols for the different paths to the hierarchy. std::map pastedSymbols; std::map pastedSheets; @@ -1768,8 +1771,18 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent ) item->SetFlags( STARTPOINT | ENDPOINT ); item->SetFlags( IS_NEW | IS_PASTED | IS_MOVING ); + LIB_SYMBOL* libItem = nullptr; + SCH_SYMBOL* sym = dyn_cast( item ); + + if( sym ) + libItem = sym->GetLibSymbolRef().get(); + m_frame->AddItemToScreenAndUndoList( m_frame->GetScreen(), (SCH_ITEM*) item, i > 0 ); + // Keep track of updated library symbols + if( libItem && sym ) + libItemMap[libItem] = sym->GetLibSymbolRef().get(); + // Reset flags for subsequent move operation item->SetFlags( IS_NEW | IS_PASTED | IS_MOVING ); @@ -1819,6 +1832,14 @@ int SCH_EDITOR_CONTROL::Paste( const TOOL_EVENT& aEvent ) || pasteMode == PASTE_MODE::RESPECT_OPTIONS || pastedSymbols[sheetPath][i].AlwaysAnnotate() ) { + LIB_SYMBOL* libItem = pastedSymbols[sheetPath][i].GetLibPart(); + auto it = libItemMap.find( libItem ); + + if( it != libItemMap.end() ) + pastedSymbols[sheetPath][i] = SCH_REFERENCE( pastedSymbols[sheetPath][i].GetSymbol(), + it->second, + pastedSymbols[sheetPath][i].GetSheetPath() ); + annotatedSymbols[sheetPath].AddItem( pastedSymbols[sheetPath][i] ); } }