From 16c87f4914a24ff2fa20341ff0784d204886c025 Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Fri, 1 Nov 2024 21:05:17 +0100 Subject: [PATCH] Pcbnew: fix crash when loading a FP from older Kicad version in some cases: if a "old" footprint (from version 7 and older) use a non kicad font in ref or value field, a use after free pointer created a crash. The use after free is fixed, but the initial font is lost, so this commit needs refinements. --- .../kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp b/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp index 5b578b4c13..bbee91cbb0 100644 --- a/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp +++ b/pcbnew/pcb_io/kicad_sexpr/pcb_io_kicad_sexpr_parser.cpp @@ -4750,20 +4750,34 @@ FOOTPRINT* PCB_IO_KICAD_SEXPR_PARSER::parseFOOTPRINT_unchecked( wxArrayString* a { // Fields other than reference and value weren't historically // stored in fp_texts so we don't need to handle them here + bool text_must_be_deleted = false; + switch( field->GetId() ) { case REFERENCE_FIELD: footprint->Reference() = PCB_FIELD( *text, REFERENCE_FIELD ); const_cast( footprint->Reference().m_Uuid ) = text->m_Uuid; - delete text; + text_must_be_deleted = true; break; case VALUE_FIELD: footprint->Value() = PCB_FIELD( *text, VALUE_FIELD ); const_cast( footprint->Value().m_Uuid ) = text->m_Uuid; - delete text; + text_must_be_deleted = true; break; } + + if( text_must_be_deleted ) + { + // We don't want to leave a dangling pointer in the map + if( auto it = m_fontTextMap.find( text ); it != m_fontTextMap.end() ) + { + // TODO: try to keep the font specified in m_fontTextMap + m_fontTextMap.erase( it ); + } + + delete text; + } } else footprint->Add( text, ADD_MODE::APPEND, true );