From 6499b5f2966e49530770aa9243bb0ac921c787bf Mon Sep 17 00:00:00 2001 From: jean-pierre charras Date: Thu, 1 Apr 2021 11:27:14 +0200 Subject: [PATCH] Eeschema: protect lib symbols against duplicate field names. If a field name is already in use, try to rename the new field name (adding the suffix "_1" or "_2" or ... ) This is especially critical when a non mandatory field has the same name as a mandatory field (was crashing before) Fixes #8081 https://gitlab.com/kicad/code/kicad/issues/8081 --- .../sch_plugins/kicad/sch_sexpr_parser.cpp | 26 +++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp b/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp index 1117238ec0..5e667a536d 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp +++ b/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp @@ -844,8 +844,30 @@ LIB_FIELD* SCH_SEXPR_PARSER::parseProperty( std::unique_ptr& aSymbol ) } else { + // At this point, a user field is read. existingField = aSymbol->FindField( field->GetCanonicalName() ); +#if 1 // Enable it to modify the name of the field to add if already existing + // Disable it to skip the field having the same name as previous field + if( existingField ) + { + // We cannot handle 2 fields with the same name, so because the field name + // is already in use, try to build a new name (oldname_x) + wxString base_name = field->GetCanonicalName(); + + // Arbitrary limit 10 attempts to find a new name + for( int ii = 1; ii < 10 && existingField ; ii++ ) + { + wxString newname = base_name; + newname << '_' << ii; + + existingField = aSymbol->FindField( newname ); + + if( !existingField ) // the modified name is not found, use it + field->SetName( newname ); + } + } + if( !existingField ) { aSymbol->AddDrawItem( field.get(), false ); @@ -853,8 +875,8 @@ LIB_FIELD* SCH_SEXPR_PARSER::parseProperty( std::unique_ptr& aSymbol ) } else { - *existingField = *field; - return existingField; + // We cannot handle 2 fields with the same name, so skip this one + return nullptr; } } }