From 5c0e7a944fe5c22770aedff771430a02145f63b9 Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Sun, 28 Jan 2024 18:08:32 +0100 Subject: [PATCH] Utility: fix bug in CreateDirHierarchy Fix bug where even the last path would be processed in the loop, making the return call always return an error as the path would've been created by that time. This ensures that we always return the value returned by mkdir when creating the last path --- src/tier0/utility.cpp | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/tier0/utility.cpp b/src/tier0/utility.cpp index 8774d371..4e1745ea 100644 --- a/src/tier0/utility.cpp +++ b/src/tier0/utility.cpp @@ -28,9 +28,20 @@ int CreateDirHierarchy(const char* const filePath) int results; snprintf(fullPath, sizeof(fullPath), "%s", filePath); - V_FixSlashes(fullPath); + const size_t pathLen = strlen(fullPath); + char* const pathEnd = &fullPath[pathLen - 1]; + + // Strip the trailing slash if there's one + if (*pathEnd == CORRECT_PATH_SEPARATOR) + *pathEnd = '\0'; + + // Get the pointer to the last dir separator, the last dir is what we want + // to create and return the value of which is why we run that outside the + // loop + const char* const lastDir = strrchr(fullPath, CORRECT_PATH_SEPARATOR); + char* pFullPath = fullPath; while ((pFullPath = strchr(pFullPath, CORRECT_PATH_SEPARATOR)) != NULL) { @@ -43,7 +54,14 @@ int CreateDirHierarchy(const char* const filePath) if (results && errno != EEXIST) return results; - *pFullPath++ = CORRECT_PATH_SEPARATOR; + *pFullPath = CORRECT_PATH_SEPARATOR; + + // Last dir should be created separately, and its return value should + // be returned + if (pFullPath == lastDir) + break; + + pFullPath++; } // Try to create the final directory in the path.