From 670adf41511b9c04ef73e8acf40867c5b11c01f2 Mon Sep 17 00:00:00 2001 From: neonloop Date: Sun, 5 Feb 2023 23:57:20 +0000 Subject: Uses file size instead of core-reported size for state loads Fixes dosbox-pure and other cores with varying state sizes --- core.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/core.c b/core.c index b758e7c..61a14b5 100644 --- a/core.c +++ b/core.c @@ -133,26 +133,30 @@ bool state_exists(int slot) { int state_read(void) { char filename[MAX_PATH]; + struct stat stat; + size_t state_size; FILE *state_file = NULL; void *state = NULL; int ret = -1; - size_t state_size = current_core.retro_serialize_size(); - if (!state_size) { - return 0; + state_file_name(filename, MAX_PATH, state_slot); + + state_file = fopen(filename, "r"); + if (!state_file) { + PA_ERROR("Error opening state file: %s\n", strerror(errno)); + goto error; } - state = calloc(1, state_size); - if (!state) { - PA_ERROR("Couldn't allocate memory for state\n"); + if (fstat(fileno(state_file), &stat) == -1) { + PA_ERROR("Couldn't read state file size: %s\n", strerror(errno)); goto error; } - state_file_name(filename, MAX_PATH, state_slot); + state_size = stat.st_size; - state_file = fopen(filename, "r"); - if (!state_file) { - PA_ERROR("Error opening state file: %s\n", strerror(errno)); + state = calloc(1, state_size); + if (!state) { + PA_ERROR("Couldn't allocate memory for state\n"); goto error; } -- cgit v1.2.3