From 72ae9470d3e8218471cf3b10dcb804b8f2ca5267 Mon Sep 17 00:00:00 2001 From: Jonathan Moore Liles Date: Sat, 3 May 2008 12:15:02 -0500 Subject: [PATCH] Don't crash when song cannot be loaded via LASH. Consider invalid song path given via command line a fatal error. --- lash.C | 3 ++- main.C | 15 +++++++++++++-- sequence.C | 8 +++++++- smf.C | 2 +- 4 files changed, 23 insertions(+), 5 deletions(-) diff --git a/lash.C b/lash.C index 1cb9153..0b98dc7 100644 --- a/lash.C +++ b/lash.C @@ -85,7 +85,8 @@ Lash::process ( void ) { MESSAGE( "LASH wants us to load \"%s\"", name ); - load_song( name ); + if ( ! load_song( name ) ) + /* FIXME: should we tell lash that we couldn't load the song? */; lash_send_event( _client, lash_event_new_with_type( LASH_Restore_File ) ); diff --git a/main.C b/main.C index 72c2600..3d5a1af 100644 --- a/main.C +++ b/main.C @@ -100,13 +100,16 @@ load_song ( const char *name ) { MESSAGE( "loading song \"%s\"", name ); + Grid *pattern_grid = pattern_c->grid(); + Grid *phrase_grid = phrase_c->grid(); + pattern_c->grid( NULL ); phrase_c->grid( NULL ); if ( ! playlist->load( name ) ) { WARNING( "failed to load song file" ); - return false; + goto failed; } pattern_c->grid( pattern::pattern_by_number( 1 ) ); @@ -117,6 +120,13 @@ load_song ( const char *name ) song.dirty( false ); return true; + +failed: + + pattern_c->grid( pattern_grid ); + phrase_c->grid( phrase_grid ); + + return false; } bool @@ -163,7 +173,8 @@ main ( int argc, char **argv ) if ( argc > 1 ) { /* maybe a filename on the commandline */ - load_song( argv[1] ); + if ( ! load_song( argv[ 1 ] ) ) + ASSERTION( "Could not load song \"%s\" specified on command line", argv[ 1 ] ); } if ( ! midi_init() ) diff --git a/sequence.C b/sequence.C index 8b34c37..044df7e 100644 --- a/sequence.C +++ b/sequence.C @@ -24,6 +24,8 @@ #include "non.H" +#include + /* #include */ /* using std::string; */ @@ -281,7 +283,11 @@ sequence::load ( const char *name ) { smf f; - f.open( name, smf::READ ); + if ( ! f.open( name, smf::READ ) ) + { + WARNING( "error opening file: %s", strerror( errno ) ); + return false; + } f.read_header(); diff --git a/smf.C b/smf.C index b713dbf..50f0943 100644 --- a/smf.C +++ b/smf.C @@ -64,7 +64,7 @@ smf::open ( const char *name, int mode ) _fp = fopen( _name, mode == smf::WRITE ? "w" : "r" ); - return _fp > 0; + return _fp != NULL; } /*************************/