From 67ccf769c7b46e47e67dafe23790354b8ec146ff Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Fri, 16 Feb 2018 11:59:22 +0100 Subject: [PATCH] Symbol Editor: fix a crash when saving/reverting a library not present in sym-lib-table --- eeschema/lib_manager.cpp | 20 ++++++++++++++++---- eeschema/lib_manager.h | 2 +- eeschema/libedit.cpp | 3 ++- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/eeschema/lib_manager.cpp b/eeschema/lib_manager.cpp index 6a5aa1cc1f..e3f9a8a8f3 100644 --- a/eeschema/lib_manager.cpp +++ b/eeschema/lib_manager.cpp @@ -73,7 +73,7 @@ int LIB_MANAGER::GetLibraryHash( const wxString& aLibrary ) const if( libBufIt != m_libs.end() ) return libBufIt->second.GetHash(); - auto row = symTable()->FindRow( aLibrary ); + auto row = GetLibrary( aLibrary ); // return -1 if library does not exist or 0 if not modified return row ? std::hash{}( aLibrary.ToStdString() + row->GetFullURI( true ).ToStdString() ) : -1; @@ -91,9 +91,21 @@ wxArrayString LIB_MANAGER::GetLibraryNames() const } -SYMBOL_LIB_TABLE_ROW* LIB_MANAGER::GetLibrary( const wxString& aLibrary ) +SYMBOL_LIB_TABLE_ROW* LIB_MANAGER::GetLibrary( const wxString& aLibrary ) const { - return symTable()->FindRow( aLibrary ); + SYMBOL_LIB_TABLE_ROW* row = nullptr; + + try + { + row = symTable()->FindRow( aLibrary ); + } + catch( const IO_ERROR& e ) + { + DisplayErrorMessage( &m_frame, wxString::Format( _( "Cannot find library \"%s\" in " + "the Symbol Library Table" ), aLibrary ), e.What() ); + } + + return row; } @@ -179,7 +191,7 @@ bool LIB_MANAGER::SaveLibrary( const wxString& aLibrary, const wxString& aFileNa // clear the deleted parts buffer only if data is saved to the original file wxFileName original, destination( aFileName ); - auto row = symTable()->FindRow( aLibrary ); + auto row = GetLibrary( aLibrary ); if( row ) { diff --git a/eeschema/lib_manager.h b/eeschema/lib_manager.h index c8499721db..7426e6667a 100644 --- a/eeschema/lib_manager.h +++ b/eeschema/lib_manager.h @@ -76,7 +76,7 @@ public: /** * Finds a single library within the (aggregate) library table. */ - SYMBOL_LIB_TABLE_ROW* GetLibrary( const wxString& aLibrary ); + SYMBOL_LIB_TABLE_ROW* GetLibrary( const wxString& aLibrary ) const; /** * Returns a set containing all part names for a specific library. diff --git a/eeschema/libedit.cpp b/eeschema/libedit.cpp index a317952229..39ae033afd 100644 --- a/eeschema/libedit.cpp +++ b/eeschema/libedit.cpp @@ -249,7 +249,8 @@ void LIB_EDIT_FRAME::OnSaveAllLibraries( wxCommandEvent& event ) void LIB_EDIT_FRAME::OnRevertLibrary( wxCommandEvent& aEvent ) { wxString libName = getTargetLib(); - bool currentLib = ( libName == GetCurLib() ); + wxString curLib = GetCurLib(); + bool currentLib = ( libName == curLib || curLib.IsEmpty() ); // Save the current part name/unit to reload after revert wxString alias = m_aliasName;