From de9d14ede05c0cb277f273aee0fb250daeafa485 Mon Sep 17 00:00:00 2001 From: OBattler Date: Thu, 8 Jun 2017 01:52:23 +0200 Subject: [PATCH] The NVR read and save functions now do more sanity checks and properly initialize the file pointer to NULL on start, fixes the crash on machines without a NVR file. --- src/config.c | 90 ++++++++++++++++++++++++++-------------------------- src/nvr.c | 16 +++++++--- 2 files changed, 57 insertions(+), 49 deletions(-) diff --git a/src/config.c b/src/config.c index 0b4dbce53..912c4aaac 100644 --- a/src/config.c +++ b/src/config.c @@ -773,7 +773,6 @@ static void loadconfig_general(void) { char *cat = "General"; char temps[512]; - wchar_t *wp; char *p; vid_resize = !!config_get_int(cat, "vid_resize", 0); @@ -824,6 +823,42 @@ static void loadconfig_general(void) window_w = window_h = window_x = window_y = 0; } +#ifndef __unix + /* Currently, 86Box is English (US) only, but in the future (version 1.30 at the earliest) other languages will be added, + therefore it is better to future-proof the code. */ + dwLanguage = config_get_hex16(cat, "language", 0x0409); +#endif +} + + +/* Machine */ +static void loadconfig_machine(void) +{ + char *cat = "Machine"; + wchar_t *wp; + char *p; + + p = config_get_string(cat, "model", NULL); + if (p != NULL) + model = model_get_model_from_internal_name(p); + else + model = 0; + if (model >= model_count()) + model = model_count() - 1; + + romset = model_getromset(); + cpu_manufacturer = config_get_int(cat, "cpu_manufacturer", 0); + cpu = config_get_int(cat, "cpu", 0); + cpu_waitstates = config_get_int(cat, "cpu_waitstates", 0); + + mem_size = config_get_int(cat, "mem_size", 4096); + if (mem_size < ((models[model].flags & MODEL_AT) ? models[model].min_ram*1024 : models[model].min_ram)) + mem_size = ((models[model].flags & MODEL_AT) ? models[model].min_ram*1024 : models[model].min_ram); + if (mem_size > 262144) + { + mem_size = 262144; + } + if (read_nvr_path != NULL) { free(read_nvr_path); @@ -857,41 +892,6 @@ static void loadconfig_general(void) path_len = wcslen(nvr_path); -#ifndef __unix - /* Currently, 86Box is English (US) only, but in the future (version 1.30 at the earliest) other languages will be added, - therefore it is better to future-proof the code. */ - dwLanguage = config_get_hex16(cat, "language", 0x0409); -#endif -} - - -/* Machine */ -static void loadconfig_machine(void) -{ - char *cat = "Machine"; - char *p; - - p = config_get_string(cat, "model", NULL); - if (p != NULL) - model = model_get_model_from_internal_name(p); - else - model = 0; - if (model >= model_count()) - model = model_count() - 1; - - romset = model_getromset(); - cpu_manufacturer = config_get_int(cat, "cpu_manufacturer", 0); - cpu = config_get_int(cat, "cpu", 0); - cpu_waitstates = config_get_int(cat, "cpu_waitstates", 0); - - mem_size = config_get_int(cat, "mem_size", 4096); - if (mem_size < ((models[model].flags & MODEL_AT) ? models[model].min_ram*1024 : models[model].min_ram)) - mem_size = ((models[model].flags & MODEL_AT) ? models[model].min_ram*1024 : models[model].min_ram); - if (mem_size > 262144) - { - mem_size = 262144; - } - cpu_use_dynarec = !!config_get_int(cat, "cpu_use_dynarec", 0); enable_external_fpu = !!config_get_int(cat, "cpu_enable_fpu", 0); @@ -1793,15 +1793,6 @@ static void saveconfig_general(void) config_delete_var(cat, "window_coordinates"); } - if (read_nvr_path == NULL) - { - config_delete_var(cat, "nvr_path"); - } - else - { - config_set_wstring(cat, "nvr_path", nvr_path); - } - #ifndef __unix if (dwLanguage == 0x0409) { @@ -1860,6 +1851,15 @@ static void saveconfig_machine(void) config_set_int(cat, "mem_size", mem_size); } + if (read_nvr_path == NULL) + { + config_delete_var(cat, "nvr_path"); + } + else + { + config_set_wstring(cat, "nvr_path", nvr_path); + } + config_set_int(cat, "cpu_use_dynarec", cpu_use_dynarec); if (enable_external_fpu == 0) diff --git a/src/nvr.c b/src/nvr.c index 7e9c171b7..eacb4d664 100644 --- a/src/nvr.c +++ b/src/nvr.c @@ -211,7 +211,7 @@ uint8_t readnvr(uint16_t addr, void *priv) void loadnvr(void) { - FILE *f; + FILE *f = NULL; int c; nvrmask=63; oldmodel = model; @@ -235,8 +235,12 @@ void loadnvr(void) free(nvr_name); free(model_name); - if (!f) + if (!f || (model_get_nvrmask(model) == 0)) { + if (f) + { + fclose(f); + } memset(nvrram,0xFF,128); if (!enable_sync) { @@ -262,7 +266,7 @@ void loadnvr(void) void savenvr(void) { - FILE *f; + FILE *f = NULL; wchar_t *model_name; wchar_t *nvr_name; @@ -282,8 +286,12 @@ void savenvr(void) free(nvr_name); free(model_name); - if (!f) + if (!f || (model_get_nvrmask(oldmodel) == 0)) { + if (f) + { + fclose(f); + } return; }