From 2b4f3ab19e181e5b9be0a224b83e8f57bdc8b573 Mon Sep 17 00:00:00 2001 From: Misa Date: Sun, 18 Apr 2021 14:26:29 -0700 Subject: [PATCH] Pass size of output through instead of hardcoding it Previously, this function had a bug due to failing to account for array decay. My solution was to just repeat the MAX_PATH again. But in hindsight I realize that's bad because it hardcodes it, and introduces the opportunity for an error where we update the size of the original path but not the size in the function. So instead, just pass the size through to the function. --- desktop_version/src/FileSystemUtils.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index 304e7322..00412744 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -39,7 +39,7 @@ static char levelDir[MAX_PATH] = {'\0'}; static char assetDir[MAX_PATH] = {'\0'}; static char virtualMountPath[MAX_PATH] = {'\0'}; -static void PLATFORM_getOSDirectory(char* output); +static void PLATFORM_getOSDirectory(char* output, const size_t output_size); static void PLATFORM_migrateSaveData(char* output); static void PLATFORM_copyFile(const char *oldLocation, const char *newLocation); @@ -86,7 +86,7 @@ int FILESYSTEM_init(char *argvZero, char* baseDir, char *assetsPath) } else { - PLATFORM_getOSDirectory(output); + PLATFORM_getOSDirectory(output, sizeof(output)); } /* Mount our base user directory */ @@ -707,17 +707,17 @@ void FILESYSTEM_enumerateLevelDirFileNames( } } -static void PLATFORM_getOSDirectory(char* output) +static void PLATFORM_getOSDirectory(char* output, const size_t output_size) { #ifdef _WIN32 /* This block is here for compatibility, do not touch it! */ WCHAR utf16_path[MAX_PATH]; SHGetFolderPathW(NULL, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, utf16_path); - WideCharToMultiByte(CP_UTF8, 0, utf16_path, -1, output, MAX_PATH, NULL, NULL); + WideCharToMultiByte(CP_UTF8, 0, utf16_path, -1, output, output_size, NULL, NULL); SDL_strlcat(output, "\\VVVVVV\\", MAX_PATH); mkdir(output, 0777); #else - SDL_strlcpy(output, PHYSFS_getPrefDir("distractionware", "VVVVVV"), MAX_PATH); + SDL_strlcpy(output, PHYSFS_getPrefDir("distractionware", "VVVVVV"), output_size); #endif }