Skip to content

Commit

Permalink
- sanitized exit code a bit
Browse files Browse the repository at this point in the history
Instead of trying a homegrown way to avoid recursive exceptions, let's do it with the defined procedure C++ has for this case: call std::terminate.

This allowed removing some old hackery inherited from Boom and will now hopefully allow sanitizing the exit procedure to the point that it can be done without depending on exit handlers.
  • Loading branch information
coelckers committed Sep 30, 2019
1 parent ff40bcd commit 338ae15
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 124 deletions.
11 changes: 11 additions & 0 deletions src/d_main.cpp
Expand Up @@ -108,6 +108,7 @@ EXTERN_CVAR(Bool, hud_althud)
EXTERN_CVAR(Int, vr_mode)
void DrawHUD();
void D_DoAnonStats();
void I_DetectOS;


// MACROS ------------------------------------------------------------------
Expand Down Expand Up @@ -2267,6 +2268,15 @@ static void CheckCmdLine()
}
}

void I_Quit()
{
if (demorecording)
{
G_CheckDemoStatus();
}

C_DeinitConsole();
}

//==========================================================================
//
Expand All @@ -2287,6 +2297,7 @@ void D_DoomMain (void)

C_InitConsole(80*8, 25*8, false);
I_DetectOS();
atterm(I_Quit);

// +logfile gets checked too late to catch the full startup log in the logfile so do some extra check for it here.
FString logfile = Args->TakeValue("+logfile");
Expand Down
17 changes: 0 additions & 17 deletions src/posix/cocoa/i_main.mm
Expand Up @@ -149,24 +149,7 @@ void OriginalMainTry(int argc, char** argv)
{
Args = new FArgs(argc, argv);

/*
killough 1/98:
This fixes some problems with exit handling
during abnormal situations.
The old code called I_Quit() to end program,
while now I_Quit() is installed as an exit
handler and exit() is called to exit, either
normally or abnormally. Seg faults are caught
and the error handler is used, to prevent
being left in graphics mode or having very
loud SFX noise because the sound card is
left in an unstable state.
*/

atexit(call_terms);
atterm(I_Quit);

NSString* exePath = [[NSBundle mainBundle] executablePath];
progdir = [[exePath stringByDeletingLastPathComponent] UTF8String];
Expand Down
21 changes: 1 addition & 20 deletions src/posix/cocoa/i_system.mm
Expand Up @@ -93,20 +93,6 @@ void I_Init(void)
I_InitSound();
}

static int has_exited;

void I_Quit()
{
has_exited = 1; // Prevent infinitely recursive exits -- killough

if (demorecording)
{
G_CheckDemoStatus();
}

C_DeinitConsole();
}


extern FILE* Logfile;
bool gameisdead;
Expand Down Expand Up @@ -137,12 +123,7 @@ static void I_FatalError(const char* const error, va_list ap)
fprintf(stderr, "%s\n", errortext);
exit(-1);
}

if (!has_exited) // If it hasn't exited yet, exit now -- killough
{
has_exited = 1; // Prevent infinitely recursive exits -- killough
exit(-1);
}
std::terminate();
}

void I_FatalError(const char* const error, ...)
Expand Down
6 changes: 0 additions & 6 deletions src/posix/i_system.h
Expand Up @@ -81,12 +81,6 @@ void I_StartTic (void);
// for normal input.
ticcmd_t *I_BaseTiccmd (void);


// Called by M_Responder when quit is selected.
// Clean exit, displays sell blurb.
void I_Quit (void);


void I_Tactile (int on, int off, int total);

void I_DebugPrint (const char *cp);
Expand Down
17 changes: 0 additions & 17 deletions src/posix/sdl/i_main.cpp
Expand Up @@ -192,24 +192,7 @@ int main (int argc, char **argv)
{
Args = new FArgs(argc, argv);

/*
killough 1/98:
This fixes some problems with exit handling
during abnormal situations.
The old code called I_Quit() to end program,
while now I_Quit() is installed as an exit
handler and exit() is called to exit, either
normally or abnormally. Seg faults are caught
and the error handler is used, to prevent
being left in graphics mode or having very
loud SFX noise because the sound card is
left in an unstable state.
*/

atexit (call_terms);
atterm (I_Quit);

// Should we even be doing anything with progdir on Unix systems?
char program[PATH_MAX];
Expand Down
23 changes: 1 addition & 22 deletions src/posix/sdl/i_system.cpp
Expand Up @@ -102,22 +102,6 @@ void I_Init (void)
I_InitSound ();
}

//
// I_Quit
//
static int has_exited;

void I_Quit (void)
{
has_exited = 1; /* Prevent infinitely recursive exits -- killough */

if (demorecording)
G_CheckDemoStatus();

C_DeinitConsole();
}


//
// I_Error
//
Expand Down Expand Up @@ -189,12 +173,7 @@ void I_FatalError (const char *error, va_list ap)
fprintf (stderr, "%s\n", errortext);
exit (-1);
}

if (!has_exited) // If it hasn't exited yet, exit now -- killough
{
has_exited = 1; // Prevent infinitely recursive exits -- killough
exit(-1);
}
std::terminate();
}

void I_FatalError(const char* const error, ...)
Expand Down
2 changes: 1 addition & 1 deletion src/utility/files_decompress.cpp
Expand Up @@ -63,7 +63,7 @@ void DecompressorBase::DecompressionError(const char *error, ...) const
va_end(argptr);

if (ErrorCallback != nullptr) ErrorCallback(errortext);
else std::terminate();
else throw std::runtime_error(errortext);
}

long DecompressorBase::Tell () const
Expand Down
20 changes: 6 additions & 14 deletions src/win32/i_main.cpp
Expand Up @@ -749,6 +749,11 @@ void PeekThreadedErrorPane()
PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE);
}

static void UnTbp()
{
timeEndPeriod(TimerPeriod);
}

//==========================================================================
//
// DoMain
Expand Down Expand Up @@ -857,23 +862,10 @@ void DoMain (HINSTANCE hInstance)
TimerPeriod = tc.wPeriodMin;

timeBeginPeriod (TimerPeriod);

/*
killough 1/98:
This fixes some problems with exit handling
during abnormal situations.
The old code called I_Quit() to end program,
while now I_Quit() is installed as an exit
handler and exit() is called to exit, either
normally or abnormally.
*/
atexit(UnTbp);

atexit (call_terms);

atterm (I_Quit);

// Figure out what directory the program resides in.
WCHAR progbuff[1024];
if (GetModuleFileNameW(nullptr, progbuff, sizeof progbuff) == 0)
Expand Down
28 changes: 1 addition & 27 deletions src/win32/i_system.cpp
Expand Up @@ -143,7 +143,6 @@ int sys_ostype = 0;
// PRIVATE DATA DEFINITIONS ------------------------------------------------

static ticcmd_t emptycmd;
static bool HasExited;

static WadStuff *WadList;
static int NumWads;
Expand Down Expand Up @@ -342,26 +341,6 @@ void I_Init()
I_InitSound ();
}

//==========================================================================
//
// I_Quit
//
//==========================================================================

void I_Quit()
{
HasExited = true; /* Prevent infinitely recursive exits -- killough */

timeEndPeriod(TimerPeriod);

if (demorecording)
{
G_CheckDemoStatus();
}

C_DeinitConsole();
}


//==========================================================================
//
Expand Down Expand Up @@ -395,12 +374,7 @@ void I_FatalError(const char *error, ...)

throw CFatalError(errortext);
}

if (!HasExited) // If it hasn't exited yet, exit now -- killough
{
HasExited = 1; // Prevent infinitely recursive exits -- killough
exit(-1);
}
std::terminate();
}

//==========================================================================
Expand Down

0 comments on commit 338ae15

Please sign in to comment.