From 7478b68dd7f0fb06222ece24b9b12eeafb28ee4c Mon Sep 17 00:00:00 2001 From: Misa Date: Thu, 6 Aug 2020 23:08:09 -0700 Subject: [PATCH] Fix basedir trailing path separator check The previous check by mwpenny had a few issues: (a) It was completely overcomplicated for no good reason, and was basically a Rube Goldberg machine. The original check was... (1) Creating an std::string of the last char of 'output'... (2) ...except instead of using the normal std::string constructor, it was using the one where you pass in a number and a char to create a string that's just that char repeated N times... except this was only used to create a 1-length string. (3) Converted that std::string to a C string. (4) Then passed it to strcmp(), despite the string at this point being only one byte and you could just compare the char values directly. The original check could've just been: output[SDL_strlen(output) - 1] == *pathSep (b) Use of libc strcmp() and strlen() instead of SDL_strcmp() and SDL_strlen(). Now, actually, PHYSFS_getDirSeparator() happens to be a char array and not a single char, so mwpenny was going in the right direction by using strcmp() after all. Except it doesn't seem like he thought about the fact that PHYSFS_getDirSeparator() could be multiple bytes instead of one, and so he ended up making the first argument to strcmp() always be a one-byte char array. So there's issue (c), which is that it assumes the path separator is one byte instead of multiple. This commit fixes all of these issues with the trailing path separator check. --- desktop_version/src/FileSystemUtils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/desktop_version/src/FileSystemUtils.cpp b/desktop_version/src/FileSystemUtils.cpp index 3709a7bb..6f71f13d 100644 --- a/desktop_version/src/FileSystemUtils.cpp +++ b/desktop_version/src/FileSystemUtils.cpp @@ -50,7 +50,7 @@ int FILESYSTEM_init(char *argvZero, char* baseDir, char *assetsPath) strcpy(output, baseDir); /* We later append to this path and assume it ends in a slash */ - if (strcmp(std::string(1, output[strlen(output) - 1]).c_str(), pathSep) != 0) + if (SDL_strcmp(output + SDL_strlen(output) - SDL_strlen(pathSep), pathSep) != 0) { strcat(output, pathSep); }