From 059cfd3c24e1b0c37048e90e79d7c30d1808628d Mon Sep 17 00:00:00 2001 From: Toad King Date: Wed, 22 Aug 2012 16:05:03 -0400 Subject: [PATCH] (GX) fix potential null-pointer exceptions --- gx/frontend/main.c | 9 +++++++-- gx/salamander/main.c | 9 +++++++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/gx/frontend/main.c b/gx/frontend/main.c index e8421b422a..164629f6d7 100644 --- a/gx/frontend/main.c +++ b/gx/frontend/main.c @@ -314,9 +314,14 @@ int rarch_main(int argc, char **argv); static void get_environment_settings(void) { getcwd(default_paths.core_dir, MAXPATHLEN); - *(strrchr(default_paths.core_dir, '/')) = 0; + char *last_slash = strrchr(default_paths.core_dir, '/'); + if (last_slash) + *last_slash = 0; char *device_end = strchr(default_paths.core_dir, '/'); - snprintf(default_paths.port_dir, sizeof(default_paths.port_dir), "%.*s/retroarch", device_end - default_paths.core_dir, default_paths.core_dir); + if (device_end) + snprintf(default_paths.port_dir, sizeof(default_paths.port_dir), "%.*s/retroarch", device_end - default_paths.core_dir, default_paths.core_dir); + else + snprintf(default_paths.port_dir, sizeof(default_paths.port_dir), "/retroarch"); snprintf(default_paths.config_file, sizeof(default_paths.config_file), "%s/retroarch.cfg", default_paths.port_dir); snprintf(default_paths.system_dir, sizeof(default_paths.system_dir), "%s/system", default_paths.port_dir); snprintf(default_paths.savestate_dir, sizeof(default_paths.savestate_dir), "%s/savestates", default_paths.port_dir); diff --git a/gx/salamander/main.c b/gx/salamander/main.c index 761ed4e97f..73540fdea3 100644 --- a/gx/salamander/main.c +++ b/gx/salamander/main.c @@ -115,9 +115,14 @@ static void init_settings(void) static void get_environment_settings(void) { getcwd(default_paths.core_dir, MAXPATHLEN); - *(strrchr(default_paths.core_dir, '/')) = 0; + char *last_slash = strrchr(default_paths.core_dir, '/'); + if (last_slash) + *last_slash = 0; char *device_end = strchr(default_paths.core_dir, '/'); - snprintf(default_paths.port_dir, sizeof(default_paths.port_dir), "%.*s/retroarch", device_end - default_paths.core_dir, default_paths.core_dir); + if (device_end) + snprintf(default_paths.port_dir, sizeof(default_paths.port_dir), "%.*s/retroarch", device_end - default_paths.core_dir, default_paths.core_dir); + else + snprintf(default_paths.port_dir, sizeof(default_paths.port_dir), "/retroarch"); snprintf(default_paths.config_file, sizeof(default_paths.config_file), "%s/retroarch.cfg", default_paths.port_dir); snprintf(default_paths.system_dir, sizeof(default_paths.system_dir), "%s/system", default_paths.port_dir); snprintf(default_paths.savestate_dir, sizeof(default_paths.savestate_dir), "%s/savestates", default_paths.port_dir);