- fixed potential crash on startup, Windows only

At least one version of Windows SDK (10.0.17134.0) has broken _pgmptr/_get_pgmptr()
It points to an empty string for multi-byte character set applications
GetModuleFileName() is now used instead regardless of compiler/toolchain
Added extra guard against unexpected program paths to avoid crashes

https://forum.zdoom.org/viewtopic.php?t=60598
(cherry picked from commit e1e441091d)
This commit is contained in:
alexey.lysiuk 2018-05-16 11:44:49 +03:00 committed by drfrag666
parent cc397295b6
commit a66686225d

View file

@ -928,23 +928,20 @@ void DoMain (HINSTANCE hInstance)
atterm (I_Quit);
// Figure out what directory the program resides in.
char *program;
#ifdef _MSC_VER
if (_get_pgmptr(&program) != 0)
char progbuff[1024];
if (GetModuleFileName(nullptr, progbuff, sizeof progbuff) == 0)
{
I_FatalError("Could not determine program location.");
}
#else
char progbuff[1024];
GetModuleFileName(0, progbuff, sizeof(progbuff));
progbuff[1023] = '\0';
program = progbuff;
#endif
char *program = progbuff;
progdir = program;
program = progdir.LockBuffer();
*(strrchr(program, '\\') + 1) = '\0';
if (char *lastsep = strrchr(program, '\\'))
{
lastsep[1] = '\0';
}
FixPathSeperator(program);
progdir.Truncate((long)strlen(program));
progdir.UnlockBuffer();