diff --git a/command.h b/command.h index fce1ac2392..2725022923 100644 --- a/command.h +++ b/command.h @@ -48,6 +48,7 @@ enum event_command CMD_EVENT_LOAD_CORE_PERSIST, CMD_EVENT_UNLOAD_CORE, CMD_EVENT_LOAD_STATE, + /* Swaps the current state with what's on the undo load buffer */ CMD_EVENT_UNDO_LOAD_STATE, /* Rewrites a savestate on disk */ CMD_EVENT_UNDO_SAVE_STATE, diff --git a/tasks/task_save_state.c b/tasks/task_save_state.c index 366eb92d62..b7384e955e 100644 --- a/tasks/task_save_state.c +++ b/tasks/task_save_state.c @@ -59,7 +59,7 @@ struct sram_block /** * undo_load_state: - * Revert to the state before a state was loaded. + * Revert to the state before a state was loaded. * * Returns: true if successful, false otherwise. **/ @@ -186,7 +186,7 @@ bool content_undo_load_state() /** * undo_save_state: - * Reverts the last save operation + * Reverts the last save operation * * Returns: true if successful, false otherwise. **/ @@ -196,13 +196,12 @@ bool content_undo_save_state() /* Wipe the save file buffer as it's intended to be one use only */ undo_save_buf.path[0] = '\0'; + undo_save_buf.size = 0; if (undo_save_buf.data) { free(undo_save_buf.data); undo_save_buf.data = NULL; } - undo_save_buf.data = 0; - if (!ret) { RARCH_ERR("%s \"%s\".\n", msg_hash_to_str(MSG_FAILED_TO_UNDO_SAVE_STATE), @@ -280,6 +279,11 @@ bool content_save_state(const char *path, bool save_to_disk) } undo_load_buf.data = malloc(info.size); + if (!undo_load_buf.data) { + free(data); + return false; + } + memcpy(undo_load_buf.data, data, info.size); undo_load_buf.size = info.size; strcpy(undo_load_buf.path, path); @@ -334,8 +338,7 @@ bool content_load_state(const char *path, bool load_to_backup_buffer) /* This means we're backing up the file in memory, so content_undo_save_state() can restore it */ if (load_to_backup_buffer) { - strcpy(undo_save_buf.path, path); - + /* If we were previously backing up a file, let go of it first */ if (undo_save_buf.data) { free(undo_save_buf.data); @@ -343,9 +346,12 @@ bool content_load_state(const char *path, bool load_to_backup_buffer) } undo_save_buf.data = malloc(size); - memcpy(undo_save_buf.data, buf, size); + if (!undo_save_buf.data) + goto error; + memcpy(undo_save_buf.data, buf, size); undo_save_buf.size = size; + strcpy(undo_save_buf.path, path); free(buf); return true; @@ -460,7 +466,8 @@ bool content_rename_state(const char *origin, const char *dest) /* * -* TODO/FIXME: Figure out when and where this should be called +* TODO/FIXME: Figure out when and where this should be called. +* As it is, when e.g. closing Gambatte, we get the same printf message 4 times. * */ bool content_reset_savestate_backups()