From 4d9fe0002aa427e7186a05aabce5c3158e33f388 Mon Sep 17 00:00:00 2001 From: icex2 Date: Mon, 27 Mar 2023 19:36:00 +0200 Subject: [PATCH] fix(iidxhook-util/settings): Bugfix broken paths with mixed / and \ Applies to iidx18 and iidx19 when using the recently extended redirect feature for settings paths, mixing / and \ crashes the game in some operator sub-menus when trying to save settings. --- src/main/iidxhook-util/settings.c | 24 ++++++++++++++++++------ src/main/util/str.c | 11 +++++++++++ src/main/util/str.h | 1 + 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/main/iidxhook-util/settings.c b/src/main/iidxhook-util/settings.c index 761b3f8..5b6b3c8 100644 --- a/src/main/iidxhook-util/settings.c +++ b/src/main/iidxhook-util/settings.c @@ -37,9 +37,11 @@ static char settings_path[MAX_PATH] = ".\\"; /* ------------------------------------------------------------------------- */ -static void settings_build_new_path(const char* orig_path, char* new_path, size_t new_path_len) +static void settings_build_new_path( + const char *orig_path, char *new_path, size_t new_path_len) { size_t settings_path_len; + char* orig_appended_path; settings_path_len = strlen(settings_path); @@ -48,8 +50,18 @@ static void settings_build_new_path(const char* orig_path, char* new_path, size_ strcpy(new_path, settings_path); strcat(new_path, orig_path); - /* Remove : of drive letter. Multiple \ are fine and handled by windows */ - new_path[settings_path_len + 1] = '\\'; + /* Fix only the part of the original path which comes after the leading settings path */ + orig_appended_path = new_path + settings_path_len; + + /* Duplicate \ in paths is fine because the win32 file API applies path + normalization */ + + /* Fixes some paths resulting in a mix of / and \ leading to crashes + on iidx 18 and 19, e.g. settings.bin saving in operator menu */ + str_replace(orig_appended_path, '/', '\\'); + + /* Remove : of drive letter */ + str_replace(orig_appended_path, ':', '\\'); } /* ------------------------------------------------------------------------- */ @@ -65,7 +77,7 @@ BOOL WINAPI my_CreateDirectoryA( settings_build_new_path(lpPathName, new_path, sizeof(new_path)); - log_misc("(CreateDir) Remapped settings path %s", new_path); + log_misc("(CreateDir) Remapped settings path %s -> %s", lpPathName, new_path); return real_CreateDirectoryA(new_path, lpSecurityAttributes); } @@ -122,9 +134,9 @@ settings_hook_dispatch_irp(struct irp *irp) settings_build_new_path(filename_cstr, new_path, sizeof(new_path)); - free(filename_cstr); + log_misc("Remapped settings path %s -> %s", filename_cstr, new_path); - log_misc("Remapped settings path to %s", new_path); + free(filename_cstr); filename_wstr = str_widen(new_path); diff --git a/src/main/util/str.c b/src/main/util/str.c index 7455176..83febf6 100644 --- a/src/main/util/str.c +++ b/src/main/util/str.c @@ -194,6 +194,17 @@ wchar_t *str_widen(const char *src) return result; } +void str_replace(char *str, char cur, char new) +{ + while (*str != '\0') { + if (*str == cur) { + *str = new; + } + + str++; + } +} + wchar_t *wstr_dup(const wchar_t *wstr) { wchar_t *dest; diff --git a/src/main/util/str.h b/src/main/util/str.h index fbfc481..5b36d54 100644 --- a/src/main/util/str.h +++ b/src/main/util/str.h @@ -15,6 +15,7 @@ size_t str_format(char *buf, size_t nchars, const char *fmt, ...); void str_trim(char *str); size_t str_vformat(char *buf, size_t nchars, const char *fmt, va_list ap); wchar_t *str_widen(const char *src); +void str_replace(char *str, char cur, char new); void wstr_cat(wchar_t *dest, size_t dnchars, const wchar_t *src); void wstr_cpy(wchar_t *dest, size_t dnchars, const wchar_t *src);