From d4a5e2caad788bdbe117825f084a406249811660 Mon Sep 17 00:00:00 2001 From: Marek Roszko Date: Sat, 28 Jan 2023 10:38:56 -0500 Subject: [PATCH] Fix crash when .kicad_pcb is renamed to .kicad_mod and load library attempted Pick a random error message used elsewhere --- pcbnew/plugins/kicad/pcb_plugin.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/pcbnew/plugins/kicad/pcb_plugin.cpp b/pcbnew/plugins/kicad/pcb_plugin.cpp index 15332d3887..929458f952 100644 --- a/pcbnew/plugins/kicad/pcb_plugin.cpp +++ b/pcbnew/plugins/kicad/pcb_plugin.cpp @@ -184,8 +184,15 @@ void FP_CACHE::Load() FILE_LINE_READER reader( fn.GetFullPath() ); PCB_PARSER parser( &reader, nullptr, nullptr ); - FOOTPRINT* footprint = (FOOTPRINT*) parser.Parse(); - wxString fpName = fn.GetName(); + // use dynamic cast in case somebody renames a .kicad_pcb as .kicad_mod and chucks it into a library folder + // the parsing definitely fails then + FOOTPRINT* footprint = dynamic_cast( parser.Parse() ); + wxString fpName = fn.GetName(); + + if( !footprint ) + { + THROW_IO_ERROR( wxString::Format( _( "Unable to read file '%s'" ), fn.GetFullPath() ) ); + } footprint->SetFPID( LIB_ID( wxEmptyString, fpName ) ); m_footprints.insert( fpName, new FP_CACHE_ITEM( footprint, fn ) );