From 9f20816b249470fbaf15bf6991de90b5848b3bb4 Mon Sep 17 00:00:00 2001 From: Roberto Fernandez Bautista Date: Sat, 10 Feb 2024 17:50:53 +0100 Subject: [PATCH] Duplicate field names not allowed - throw exception on load Fixes https://gitlab.com/kicad/code/kicad/-/issues/16902 --- eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp | 9 +++++++++ .../legacy/sch_legacy_lib_plugin_cache.cpp | 14 +++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp b/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp index b9b1427f48..67b10696dd 100644 --- a/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp +++ b/eeschema/sch_plugins/kicad/sch_sexpr_parser.cpp @@ -790,6 +790,15 @@ LIB_FIELD* SCH_SEXPR_PARSER::parseProperty( std::unique_ptr& aSymbol CurOffset() ); } + if( LIB_FIELD* existingName = aSymbol->FindField( name ) ) + { + if( existingName->GetId() > MANDATORY_FIELDS ) + { + THROW_PARSE_ERROR( wxString::Format( _( "Duplicate field '%s'" ), name ), + CurSource(), CurLine(), CurLineNumber(), CurOffset() ); + } + } + field->SetName( name ); // Correctly set the ID based on canonical (untranslated) field name diff --git a/eeschema/sch_plugins/legacy/sch_legacy_lib_plugin_cache.cpp b/eeschema/sch_plugins/legacy/sch_legacy_lib_plugin_cache.cpp index ff9f2d994d..c3acea63c3 100644 --- a/eeschema/sch_plugins/legacy/sch_legacy_lib_plugin_cache.cpp +++ b/eeschema/sch_plugins/legacy/sch_legacy_lib_plugin_cache.cpp @@ -669,7 +669,19 @@ void SCH_LEGACY_PLUGIN_CACHE::loadField( std::unique_ptr& aSymbol, } else { - parseQuotedString( field->m_name, aReader, line, &line, true ); // Optional. + wxString fieldName = wxEmptyString; + parseQuotedString( fieldName, aReader, line, &line, true ); // Optional. + + if( fieldName.IsEmpty() ) + return; + + if( aSymbol->FindField( fieldName ) != nullptr ) + { + SCH_PARSE_ERROR( wxString::Format( _( "Duplicate field '%s'" ), fieldName ), + aReader, line ); + } + + field->m_name = fieldName; } }