Fix crash when database lib config is not valid

This commit is contained in:
Jon Evans 2023-05-07 09:06:23 -04:00
parent f169f7a009
commit 7a7a23c83a
5 changed files with 17 additions and 7 deletions

View File

@ -96,6 +96,7 @@ void DIALOG_DATABASE_LIB_SETTINGS::OnBtnTest( wxCommandEvent& aEvent )
if( m_plugin->TestConnection( &errorMsg ) ) if( m_plugin->TestConnection( &errorMsg ) )
{ {
m_stConnectionTestStatus->SetLabel( _( "Connected to database successfully" ) ); m_stConnectionTestStatus->SetLabel( _( "Connected to database successfully" ) );
m_stConnectionTestStatus->SetToolTip( wxEmptyString );
wxCommandEvent dummy; wxCommandEvent dummy;
OnBtnReloadConfig( dummy ); OnBtnReloadConfig( dummy );
@ -104,6 +105,7 @@ void DIALOG_DATABASE_LIB_SETTINGS::OnBtnTest( wxCommandEvent& aEvent )
{ {
m_stConnectionTestStatus->SetLabel( wxString::Format( _( "Database connection failed: %s" ), m_stConnectionTestStatus->SetLabel( wxString::Format( _( "Database connection failed: %s" ),
errorMsg ) ); errorMsg ) );
m_stConnectionTestStatus->SetToolTip( errorMsg );
} }
} }

View File

@ -85,7 +85,7 @@ DIALOG_DATABASE_LIB_SETTINGS_BASE::DIALOG_DATABASE_LIB_SETTINGS_BASE( wxWindow*
m_btnTest = new wxButton( sbSizer4->GetStaticBox(), wxID_ANY, _("Test"), wxDefaultPosition, wxDefaultSize, 0 ); m_btnTest = new wxButton( sbSizer4->GetStaticBox(), wxID_ANY, _("Test"), wxDefaultPosition, wxDefaultSize, 0 );
bSizer5->Add( m_btnTest, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 ); bSizer5->Add( m_btnTest, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5 );
m_stConnectionTestStatus = new wxStaticText( sbSizer4->GetStaticBox(), wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, 0 ); m_stConnectionTestStatus = new wxStaticText( sbSizer4->GetStaticBox(), wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxST_ELLIPSIZE_MIDDLE );
m_stConnectionTestStatus->Wrap( -1 ); m_stConnectionTestStatus->Wrap( -1 );
bSizer5->Add( m_stConnectionTestStatus, 1, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT, 5 ); bSizer5->Add( m_stConnectionTestStatus, 1, wxALIGN_CENTER_VERTICAL|wxLEFT|wxRIGHT, 5 );

View File

@ -831,7 +831,7 @@
<property name="resize">Resizable</property> <property name="resize">Resizable</property>
<property name="show">1</property> <property name="show">1</property>
<property name="size"></property> <property name="size"></property>
<property name="style"></property> <property name="style">wxST_ELLIPSIZE_MIDDLE</property>
<property name="subclass">; ; forward_declare</property> <property name="subclass">; ; forward_declare</property>
<property name="toolbar_pane">0</property> <property name="toolbar_pane">0</property>
<property name="tooltip"></property> <property name="tooltip"></property>

View File

@ -66,6 +66,9 @@ void SCH_DATABASE_PLUGIN::EnumerateSymbolLib( std::vector<LIB_SYMBOL*>& aSymbolL
ensureSettings( aLibraryPath ); ensureSettings( aLibraryPath );
ensureConnection(); ensureConnection();
if( !m_conn )
THROW_IO_ERROR( m_lastError );
bool powerSymbolsOnly = ( aProperties && bool powerSymbolsOnly = ( aProperties &&
aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) != aProperties->find( SYMBOL_LIB_TABLE::PropPowerSymsOnly ) !=
aProperties->end() ); aProperties->end() );
@ -112,6 +115,9 @@ LIB_SYMBOL* SCH_DATABASE_PLUGIN::LoadSymbol( const wxString& aLibraryPath,
ensureSettings( aLibraryPath ); ensureSettings( aLibraryPath );
ensureConnection(); ensureConnection();
if( !m_conn )
THROW_IO_ERROR( m_lastError );
/* /*
* Table names are tricky, in order to allow maximum flexibility to the user. * Table names are tricky, in order to allow maximum flexibility to the user.
* The slash character is used as a separator between a table name and symbol name, but symbol * The slash character is used as a separator between a table name and symbol name, but symbol
@ -215,8 +221,8 @@ bool SCH_DATABASE_PLUGIN::TestConnection( wxString* aErrorMsg )
connect(); connect();
if( aErrorMsg && m_conn && !m_conn->IsConnected() ) if( aErrorMsg && ( !m_conn || !m_conn->IsConnected() ) )
*aErrorMsg = m_conn->GetLastError(); *aErrorMsg = m_lastError;
return m_conn && m_conn->IsConnected(); return m_conn && m_conn->IsConnected();
} }
@ -272,8 +278,7 @@ void SCH_DATABASE_PLUGIN::ensureConnection()
{ {
wxString msg = wxString::Format( wxString msg = wxString::Format(
_( "Could not load database library: could not connect to database %s (%s)" ), _( "Could not load database library: could not connect to database %s (%s)" ),
m_settings->m_Source.dsn, m_settings->m_Source.dsn, m_lastError );
m_conn->GetLastError() );
THROW_IO_ERROR( msg ); THROW_IO_ERROR( msg );
} }
@ -302,7 +307,7 @@ void SCH_DATABASE_PLUGIN::connect()
std::string basePath( wxFileName( m_settings->GetFilename() ).GetPath().ToUTF8() ); std::string basePath( wxFileName( m_settings->GetFilename() ).GetPath().ToUTF8() );
// Database drivers that use files operate on absolute paths, so provide a mechanism // Database drivers that use files operate on absolute paths, so provide a mechanism
// for specifing on-disk databases that live next to the kicad_dbl file // for specifying on-disk databases that live next to the kicad_dbl file
boost::replace_all( cs, "${CWD}", basePath ); boost::replace_all( cs, "${CWD}", basePath );
m_conn = std::make_unique<DATABASE_CONNECTION>( cs, m_settings->m_Source.timeout ); m_conn = std::make_unique<DATABASE_CONNECTION>( cs, m_settings->m_Source.timeout );
@ -310,6 +315,7 @@ void SCH_DATABASE_PLUGIN::connect()
if( !m_conn->IsConnected() ) if( !m_conn->IsConnected() )
{ {
m_lastError = m_conn->GetLastError();
m_conn.reset(); m_conn.reset();
return; return;
} }

View File

@ -115,12 +115,14 @@ private:
std::unique_ptr<DATABASE_LIB_SETTINGS> m_settings; std::unique_ptr<DATABASE_LIB_SETTINGS> m_settings;
/// Generally will be null if no valid connection is established
std::unique_ptr<DATABASE_CONNECTION> m_conn; std::unique_ptr<DATABASE_CONNECTION> m_conn;
std::set<wxString> m_customFields; std::set<wxString> m_customFields;
std::set<wxString> m_defaultShownFields; std::set<wxString> m_defaultShownFields;
wxString m_lastError;
}; };
#endif //KICAD_SCH_DATABASE_PLUGIN_H #endif //KICAD_SCH_DATABASE_PLUGIN_H