From f4535346fb65bf25a5682db9b067a3efd17d965b Mon Sep 17 00:00:00 2001 From: punkrockguy318 Date: Tue, 20 Jul 2010 01:58:13 +0000 Subject: [PATCH] sdl: fixed segfault when loadstating 0-9 when state did not exist --- src/state.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/state.cpp b/src/state.cpp index 0c56db22..fe067870 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -890,6 +890,15 @@ void FCEUI_SaveState(const char *fname) int loadStateFailed = 0; // hack, this function should return a value instead +bool file_exists(const char * filename) +{ + if (FILE * file = fopen(filename, "r")) //I'm sure, you meant for READING =) + { + fclose(file); + return true; + } + return false; +} void FCEUI_LoadState(const char *fname) { if(!FCEU_IsValidUI(FCEUI_LOADSTATE)) return; @@ -908,6 +917,11 @@ void FCEUI_LoadState(const char *fname) { FCEUI_MakeBackupMovie(false); //Backup the movie before the contents get altered, but do not display messages } + if (!file_exists(fname)) + { + loadStateFailed = 1; + return; // state doesn't exist; exit cleanly + } if(FCEUSS_Load(fname)) { //mbg todo netplay