From 609ceb782cbdb940b00c40b7866a93cdf597a96c Mon Sep 17 00:00:00 2001 From: Misa Date: Fri, 7 Aug 2020 00:08:16 -0700 Subject: [PATCH] Clean up strcat()s/strcpy()s, EXCEPT for migrateSaveData() strcat()s and strcpy()s have been replaced with SDL_snprintf() where possible, to clearly convey the intent of just building a string that looks a certain way, instead of spanning it out over multiple lines. Where there's not really a good way to avoid strcat()/strcpy() (e.g. in PLATFORM_getOSDirectory()), they will at least be replaced with SDL_strlcat() and SDL_strlcpy(), which are safer functions and are less likely to have issues with null termination. I decided not to bother with PLATFORM_migrateSaveData(), because it's going to be axed in 2.4 anyways. --- desktop_version/src/FileSystemUtils.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index d9ec2829..496d4247 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -47,13 +47,13 @@ int FILESYSTEM_init(char *argvZero, char* baseDir, char *assetsPath) /* Determine the OS user directory */ if (baseDir && baseDir[0] != '\0') { - strcpy(output, baseDir); - /* We later append to this path and assume it ends in a slash */ - if (SDL_strcmp(output + SDL_strlen(output) - SDL_strlen(pathSep), pathSep) != 0) - { - strcat(output, pathSep); - } + bool trailing_pathsep = SDL_strcmp(baseDir + SDL_strlen(baseDir) - SDL_strlen(pathSep), pathSep) == 0; + + SDL_snprintf(output, sizeof(output), "%s%s", + baseDir, + !trailing_pathsep ? pathSep : "" + ); } else { @@ -97,12 +97,14 @@ int FILESYSTEM_init(char *argvZero, char* baseDir, char *assetsPath) /* Mount the stock content last */ if (assetsPath) { - strcpy(output, assetsPath); + SDL_strlcpy(output, assetsPath, sizeof(output)); } else { - strcpy(output, PHYSFS_getBaseDir()); - strcat(output, "data.zip"); + SDL_snprintf(output, sizeof(output), "%s%s", + PHYSFS_getBaseDir(), + "data.zip" + ); } if (!PHYSFS_mount(output, NULL, 1)) { @@ -350,9 +352,9 @@ void PLATFORM_getOSDirectory(char* output) 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); - strcat(output, "\\VVVVVV\\"); + SDL_strlcat(output, "\\VVVVVV\\", sizeof(output)); #else - strcpy(output, PHYSFS_getPrefDir("distractionware", "VVVVVV")); + SDL_strlcpy(output, PHYSFS_getPrefDir("distractionware", "VVVVVV"), sizeof(output)); #endif }