Try/catch gerber file loading in attempt to catch oom

Potentially help with #7444
This commit is contained in:
Marek Roszko 2021-05-01 12:46:18 -04:00
parent eb343d3f8f
commit 850a22c3ae
2 changed files with 51 additions and 36 deletions

View File

@ -38,6 +38,7 @@
// HTML Messages used more than one time: // HTML Messages used more than one time:
#define MSG_NO_MORE_LAYER _( "<b>No more available layers</b> in GerbView to load files" ) #define MSG_NO_MORE_LAYER _( "<b>No more available layers</b> in GerbView to load files" )
#define MSG_NOT_LOADED _( "\n<b>Not loaded:</b> <i>%s</i>" ) #define MSG_NOT_LOADED _( "\n<b>Not loaded:</b> <i>%s</i>" )
#define MSG_OOM _( "\n<b>Memory was exhausted reading:</b> <i>%s</i>" )
void GERBVIEW_FRAME::OnGbrFileHistory( wxCommandEvent& event ) void GERBVIEW_FRAME::OnGbrFileHistory( wxCommandEvent& event )
@ -253,6 +254,8 @@ bool GERBVIEW_FRAME::LoadListOfGerberAndDrillFiles( const wxString& aPath,
visibility[ layer ] = true; visibility[ layer ] = true;
try
{
if( aFileType && ( *aFileType )[ii] == 1 ) if( aFileType && ( *aFileType )[ii] == 1 )
{ {
LoadExcellonFiles( filename.GetFullPath() ); LoadExcellonFiles( filename.GetFullPath() );
@ -264,8 +267,8 @@ bool GERBVIEW_FRAME::LoadListOfGerberAndDrillFiles( const wxString& aPath,
{ {
//We cannot read a gerber job file as a gerber plot file: skip it //We cannot read a gerber job file as a gerber plot file: skip it
wxString txt; wxString txt;
txt.Printf( txt.Printf( _( "<b>A gerber job file cannot be loaded as a plot file</b> "
_( "<b>A gerber job file cannot be loaded as a plot file</b> <i>%s</i>" ), "<i>%s</i>" ),
filename.GetFullName() ); filename.GetFullName() );
success = false; success = false;
reporter.Report( txt, RPT_SEVERITY_ERROR ); reporter.Report( txt, RPT_SEVERITY_ERROR );
@ -286,7 +289,8 @@ bool GERBVIEW_FRAME::LoadListOfGerberAndDrillFiles( const wxString& aPath,
while( ii < aFilenameList.GetCount() ) while( ii < aFilenameList.GetCount() )
{ {
filename = aFilenameList[ii++]; filename = aFilenameList[ii++];
wxString txt = wxString::Format( MSG_NOT_LOADED, filename.GetFullName() ); wxString txt =
wxString::Format( MSG_NOT_LOADED, filename.GetFullName() );
reporter.Report( txt, RPT_SEVERITY_ERROR ); reporter.Report( txt, RPT_SEVERITY_ERROR );
} }
break; break;
@ -295,6 +299,14 @@ bool GERBVIEW_FRAME::LoadListOfGerberAndDrillFiles( const wxString& aPath,
SetActiveLayer( layer, false ); SetActiveLayer( layer, false );
} }
} }
}
catch( const std::bad_alloc& )
{
wxString txt = wxString::Format( MSG_OOM, filename.GetFullName() );
reporter.Report( txt, RPT_SEVERITY_ERROR );
success = false;
continue;
}
if( progress ) if( progress )
progress->AdvanceProgress(); progress->AdvanceProgress();

View File

@ -50,20 +50,23 @@ bool GERBVIEW_FRAME::Read_GERBER_File( const wxString& GERBER_FullFileName )
Erase_Current_DrawLayer( false ); Erase_Current_DrawLayer( false );
} }
gerber = new GERBER_FILE_IMAGE( layer ); // use an unique ptr while we load to free on exception properly
std::unique_ptr<GERBER_FILE_IMAGE> gerber_uptr = std::make_unique<GERBER_FILE_IMAGE>( layer );
// Read the gerber file. The image will be added only if it can be read // Read the gerber file. The image will be added only if it can be read
// to avoid broken data. // to avoid broken data.
bool success = gerber->LoadGerberFile( GERBER_FullFileName ); bool success = gerber_uptr->LoadGerberFile( GERBER_FullFileName );
if( !success ) if( !success )
{ {
delete gerber; gerber_uptr.reset();
msg.Printf( _( "File \"%s\" not found" ), GERBER_FullFileName ); msg.Printf( _( "File \"%s\" not found" ), GERBER_FullFileName );
ShowInfoBarError( msg ); ShowInfoBarError( msg );
return false; return false;
} }
gerber = gerber_uptr.release();
wxASSERT( gerber != nullptr );
images->AddGbrImage( gerber, layer ); images->AddGbrImage( gerber, layer );
// Display errors list // Display errors list