From 340d1b7fad48e0ab621afe6d9e701601c2fcf4ad Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Wed, 24 Jan 2024 23:48:28 +0000 Subject: [PATCH] Guard against nullptr access. --- eeschema/sim/sim_model.cpp | 87 ++++++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 37 deletions(-) diff --git a/eeschema/sim/sim_model.cpp b/eeschema/sim/sim_model.cpp index 9c816d90af..dcb7d78b51 100644 --- a/eeschema/sim/sim_model.cpp +++ b/eeschema/sim/sim_model.cpp @@ -1486,6 +1486,41 @@ template bool SIM_MODEL::InferSimModel( LIB_SYMBOL& aSymb template void SIM_MODEL::MigrateSimModel( T_symbol& aSymbol, const PROJECT* aProject ) { + class FIELD_INFO + { + public: + FIELD_INFO() + { + m_Attributes.m_Visible = false; + m_Attributes.m_Size = VECTOR2I( DEFAULT_SIZE_TEXT * schIUScale.IU_PER_MILS, + DEFAULT_SIZE_TEXT * schIUScale.IU_PER_MILS ); + }; + + FIELD_INFO( const wxString& aText, T_field* aField ) : + m_Text( aText ), + m_Attributes( aField->GetAttributes() ), + m_Pos( aField->GetPosition() ) + {} + + bool IsEmpty() { return m_Text.IsEmpty(); } + + T_field CreateField( T_symbol* aSymbol, const wxString& aFieldName ) + { + T_field field( aSymbol, -1, aFieldName ); + + field.SetText( m_Text ); + field.SetAttributes( m_Attributes ); + field.SetPosition( m_Pos ); + + return field; + } + + public: + wxString m_Text; + TEXT_ATTRIBUTES m_Attributes; + VECTOR2I m_Pos; + }; + T_field* existing_deviceField = aSymbol.FindField( SIM_DEVICE_FIELD ); T_field* existing_deviceSubtypeField = aSymbol.FindField( SIM_DEVICE_SUBTYPE_FIELD ); T_field* existing_pinsField = aSymbol.FindField( SIM_PINS_FIELD ); @@ -1550,48 +1585,26 @@ void SIM_MODEL::MigrateSimModel( T_symbol& aSymbol, const PROJECT* aProject ) // which is confusing because it doesn't represent a device at all. if( existing_deviceSubtype == wxS( "MUTUAL" ) ) { - aSymbol.RemoveField( existing_deviceSubtypeField ); - existing_deviceField->SetText( "K" ); + if( existing_deviceSubtypeField ) // Can't be null, but Coverity doesn't know that + aSymbol.RemoveField( existing_deviceSubtypeField ); + + if( existing_deviceField ) + { + existing_deviceField->SetText( wxS( "K" ) ); + } + else + { + FIELD_INFO deviceFieldInfo; + deviceFieldInfo.m_Text = wxS( "K" ); + + T_field deviceField = deviceFieldInfo.CreateField( &aSymbol, SIM_DEVICE_FIELD ); + aSymbol.AddField( deviceField ); + } } return; } - class FIELD_INFO - { - public: - FIELD_INFO() - { - m_Attributes.m_Visible = false; - m_Attributes.m_Size = VECTOR2I( DEFAULT_SIZE_TEXT * schIUScale.IU_PER_MILS, - DEFAULT_SIZE_TEXT * schIUScale.IU_PER_MILS ); - }; - - FIELD_INFO( const wxString& aText, T_field* aField ) : - m_Text( aText ), - m_Attributes( aField->GetAttributes() ), - m_Pos( aField->GetPosition() ) - {} - - bool IsEmpty() { return m_Text.IsEmpty(); } - - T_field CreateField( T_symbol* aSymbol, const wxString& aFieldName ) - { - T_field field( aSymbol, -1, aFieldName ); - - field.SetText( m_Text ); - field.SetAttributes( m_Attributes ); - field.SetPosition( m_Pos ); - - return field; - } - - public: - wxString m_Text; - TEXT_ATTRIBUTES m_Attributes; - VECTOR2I m_Pos; - }; - auto getSIValue = []( T_field* aField ) {