From 1359168f6de7bf31086de190d841dfba937ed9ef Mon Sep 17 00:00:00 2001 From: "seer-by-sentry[bot]" <157164994+seer-by-sentry[bot]@users.noreply.github.com> Date: Sun, 4 Jan 2026 00:06:15 +0000 Subject: [PATCH] Fix potential issues when determining application working directory on startup --- Core/Tools/MapCacheBuilder/Source/WinMain.cpp | 12 ++++++++---- Generals/Code/Main/WinMain.cpp | 12 ++++++++---- Generals/Code/Tools/GUIEdit/Source/WinMain.cpp | 12 ++++++++---- GeneralsMD/Code/Main/WinMain.cpp | 12 ++++++++---- GeneralsMD/Code/Tools/GUIEdit/Source/WinMain.cpp | 12 ++++++++---- 5 files changed, 40 insertions(+), 20 deletions(-) diff --git a/Core/Tools/MapCacheBuilder/Source/WinMain.cpp b/Core/Tools/MapCacheBuilder/Source/WinMain.cpp index 74fe99edab1..3471ca76fc2 100644 --- a/Core/Tools/MapCacheBuilder/Source/WinMain.cpp +++ b/Core/Tools/MapCacheBuilder/Source/WinMain.cpp @@ -223,11 +223,15 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, // Set the current directory to the app directory. char buf[_MAX_PATH]; - GetModuleFileName(NULL, buf, sizeof(buf)); - if (char *pEnd = strrchr(buf, '\\')) { - *pEnd = 0; + buf[0] = '\0'; // Initialize buffer to empty string + DWORD result = GetModuleFileName(NULL, buf, sizeof(buf)); + if (result > 0 && result < sizeof(buf)) + { + if (char *pEnd = strrchr(buf, '\\')) { + *pEnd = 0; + ::SetCurrentDirectory(buf); + } } - ::SetCurrentDirectory(buf); /* ** Convert WinMain arguments to simple main argc and argv diff --git a/Generals/Code/Main/WinMain.cpp b/Generals/Code/Main/WinMain.cpp index 3c34a3e81cf..737e32a711c 100644 --- a/Generals/Code/Main/WinMain.cpp +++ b/Generals/Code/Main/WinMain.cpp @@ -795,12 +795,16 @@ Int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, /// @todo remove this force set of working directory later Char buffer[ _MAX_PATH ]; - GetModuleFileName( NULL, buffer, sizeof( buffer ) ); - if (Char *pEnd = strrchr(buffer, '\\')) + buffer[0] = '\0'; // Initialize buffer to empty string + DWORD result = GetModuleFileName( NULL, buffer, sizeof( buffer ) ); + if (result > 0 && result < sizeof(buffer)) { - *pEnd = 0; + if (Char *pEnd = strrchr(buffer, '\\')) + { + *pEnd = 0; + ::SetCurrentDirectory(buffer); + } } - ::SetCurrentDirectory(buffer); #ifdef RTS_DEBUG diff --git a/Generals/Code/Tools/GUIEdit/Source/WinMain.cpp b/Generals/Code/Tools/GUIEdit/Source/WinMain.cpp index bc6a055d5ea..a03a285ecd7 100644 --- a/Generals/Code/Tools/GUIEdit/Source/WinMain.cpp +++ b/Generals/Code/Tools/GUIEdit/Source/WinMain.cpp @@ -186,12 +186,16 @@ Int APIENTRY WinMain(HINSTANCE hInstance, /// @todo remove this force set of working directory later Char buffer[ _MAX_PATH ]; - GetModuleFileName( NULL, buffer, sizeof( buffer ) ); - if (Char *pEnd = strrchr(buffer, '\\')) + buffer[0] = '\0'; // Initialize buffer to empty string + DWORD result = GetModuleFileName( NULL, buffer, sizeof( buffer ) ); + if (result > 0 && result < sizeof(buffer)) { - *pEnd = 0; + if (Char *pEnd = strrchr(buffer, '\\')) + { + *pEnd = 0; + ::SetCurrentDirectory(buffer); + } } - ::SetCurrentDirectory(buffer); // initialize the memory manager early initMemoryManager(); diff --git a/GeneralsMD/Code/Main/WinMain.cpp b/GeneralsMD/Code/Main/WinMain.cpp index 3277a00b4ff..4f86efa59fd 100644 --- a/GeneralsMD/Code/Main/WinMain.cpp +++ b/GeneralsMD/Code/Main/WinMain.cpp @@ -856,12 +856,16 @@ Int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, /// @todo remove this force set of working directory later Char buffer[_MAX_PATH]; - GetModuleFileName(NULL, buffer, sizeof(buffer)); - if (Char* pEnd = strrchr(buffer, '\\')) + buffer[0] = '\0'; // Initialize buffer to empty string + DWORD result = GetModuleFileName(NULL, buffer, sizeof(buffer)); + if (result > 0 && result < sizeof(buffer)) { - *pEnd = 0; + if (Char* pEnd = strrchr(buffer, '\\')) + { + *pEnd = 0; + ::SetCurrentDirectory(buffer); + } } - ::SetCurrentDirectory(buffer); #ifdef RTS_DEBUG diff --git a/GeneralsMD/Code/Tools/GUIEdit/Source/WinMain.cpp b/GeneralsMD/Code/Tools/GUIEdit/Source/WinMain.cpp index 52ea538ae19..56a353380de 100644 --- a/GeneralsMD/Code/Tools/GUIEdit/Source/WinMain.cpp +++ b/GeneralsMD/Code/Tools/GUIEdit/Source/WinMain.cpp @@ -186,12 +186,16 @@ Int APIENTRY WinMain(HINSTANCE hInstance, /// @todo remove this force set of working directory later Char buffer[ _MAX_PATH ]; - GetModuleFileName( NULL, buffer, sizeof( buffer ) ); - if (Char *pEnd = strrchr(buffer, '\\')) + buffer[0] = '\0'; // Initialize buffer to empty string + DWORD result = GetModuleFileName( NULL, buffer, sizeof( buffer ) ); + if (result > 0 && result < sizeof(buffer)) { - *pEnd = 0; + if (Char *pEnd = strrchr(buffer, '\\')) + { + *pEnd = 0; + ::SetCurrentDirectory(buffer); + } } - ::SetCurrentDirectory(buffer); // initialize the memory manager early initMemoryManager();