Sim: Don't crash when parsing unknown model in a Spice file

This commit is contained in:
Mikolaj Wielgus 2022-11-27 08:59:54 +01:00
parent f2fb734e06
commit bbcb0c04fc
2 changed files with 14 additions and 12 deletions

View File

@ -22,8 +22,6 @@
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/ */
#include <confirm.h>
// Include simulator headers after wxWidgets headers to avoid conflicts with Windows headers // Include simulator headers after wxWidgets headers to avoid conflicts with Windows headers
// (especially on msys2 + wxWidgets 3.0.x) // (especially on msys2 + wxWidgets 3.0.x)
#include <sim/sim_model_spice.h> #include <sim/sim_model_spice.h>
@ -59,18 +57,13 @@ std::string SPICE_GENERATOR_SPICE::Preview( const SPICE_ITEM& aItem ) const
std::unique_ptr<SIM_MODEL_SPICE> SIM_MODEL_SPICE::Create( const SIM_LIBRARY_SPICE& aLibrary, std::unique_ptr<SIM_MODEL_SPICE> SIM_MODEL_SPICE::Create( const SIM_LIBRARY_SPICE& aLibrary,
const std::string& aSpiceCode ) const std::string& aSpiceCode )
{ {
auto model = static_cast<SIM_MODEL_SPICE*>( auto model = dynamic_cast<SIM_MODEL_SPICE*>(
SIM_MODEL::Create( SPICE_MODEL_PARSER::ReadType( aLibrary, aSpiceCode ) ).release() ); SIM_MODEL::Create( SPICE_MODEL_PARSER::ReadType( aLibrary, aSpiceCode ) ).release() );
try if( !model )
{ THROW_IO_ERROR( "Could not determine Spice model type" );
model->m_spiceModelParser->ReadModel( aLibrary, aSpiceCode );
}
catch( const IO_ERROR& e )
{
DisplayErrorMessage( nullptr, e.What() );
}
model->m_spiceModelParser->ReadModel( aLibrary, aSpiceCode );
return std::unique_ptr<SIM_MODEL_SPICE>( model ); return std::unique_ptr<SIM_MODEL_SPICE>( model );
} }

View File

@ -27,6 +27,7 @@
#include <sim/spice_grammar.h> #include <sim/spice_grammar.h>
#include <sim/sim_model_spice.h> #include <sim/sim_model_spice.h>
#include <ki_exception.h> #include <ki_exception.h>
#include <confirm.h>
#include <pegtl.hpp> #include <pegtl.hpp>
#include <pegtl/contrib/parse_tree.hpp> #include <pegtl/contrib/parse_tree.hpp>
@ -66,8 +67,16 @@ void SPICE_LIBRARY_PARSER::ReadFile( const std::string& aFilePath )
for( const auto& node : root->children ) for( const auto& node : root->children )
{ {
if( node->is_type<SIM_LIBRARY_SPICE_PARSER::modelUnit>() ) if( node->is_type<SIM_LIBRARY_SPICE_PARSER::modelUnit>() )
{
try
{ {
m_library.m_models.push_back( SIM_MODEL_SPICE::Create( m_library, node->string() ) ); m_library.m_models.push_back( SIM_MODEL_SPICE::Create( m_library, node->string() ) );
}
catch( const IO_ERROR& e )
{
DisplayErrorMessage( nullptr, e.What() );
}
m_library.m_modelNames.emplace_back( node->children.at( 0 )->string() ); m_library.m_modelNames.emplace_back( node->children.at( 0 )->string() );
} }
else if( node->is_type<SIM_LIBRARY_SPICE_PARSER::unknownLine>() ) else if( node->is_type<SIM_LIBRARY_SPICE_PARSER::unknownLine>() )