From 338ae15a4c783c07644da53d26d5aa3286b158e9 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Tue, 1 Oct 2019 01:37:21 +0200 Subject: [PATCH] - sanitized exit code a bit 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. --- src/d_main.cpp | 11 +++++++++++ src/posix/cocoa/i_main.mm | 17 ----------------- src/posix/cocoa/i_system.mm | 21 +-------------------- src/posix/i_system.h | 6 ------ src/posix/sdl/i_main.cpp | 17 ----------------- src/posix/sdl/i_system.cpp | 23 +---------------------- src/utility/files_decompress.cpp | 2 +- src/win32/i_main.cpp | 20 ++++++-------------- src/win32/i_system.cpp | 28 +--------------------------- 9 files changed, 21 insertions(+), 124 deletions(-) diff --git a/src/d_main.cpp b/src/d_main.cpp index 23fd6eba7..ffbd20bc7 100644 --- a/src/d_main.cpp +++ b/src/d_main.cpp @@ -108,6 +108,7 @@ EXTERN_CVAR(Bool, hud_althud) EXTERN_CVAR(Int, vr_mode) void DrawHUD(); void D_DoAnonStats(); +void I_DetectOS; // MACROS ------------------------------------------------------------------ @@ -2267,6 +2268,15 @@ static void CheckCmdLine() } } +void I_Quit() +{ + if (demorecording) + { + G_CheckDemoStatus(); + } + + C_DeinitConsole(); +} //========================================================================== // @@ -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"); diff --git a/src/posix/cocoa/i_main.mm b/src/posix/cocoa/i_main.mm index c873c1091..73dd6ee2e 100644 --- a/src/posix/cocoa/i_main.mm +++ b/src/posix/cocoa/i_main.mm @@ -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]; diff --git a/src/posix/cocoa/i_system.mm b/src/posix/cocoa/i_system.mm index f394caa17..5b89d7149 100644 --- a/src/posix/cocoa/i_system.mm +++ b/src/posix/cocoa/i_system.mm @@ -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; @@ -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, ...) diff --git a/src/posix/i_system.h b/src/posix/i_system.h index 0ef217aa1..7e5770b52 100644 --- a/src/posix/i_system.h +++ b/src/posix/i_system.h @@ -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); diff --git a/src/posix/sdl/i_main.cpp b/src/posix/sdl/i_main.cpp index 448215958..34d2d070e 100644 --- a/src/posix/sdl/i_main.cpp +++ b/src/posix/sdl/i_main.cpp @@ -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]; diff --git a/src/posix/sdl/i_system.cpp b/src/posix/sdl/i_system.cpp index 8598fc8f1..faf957601 100644 --- a/src/posix/sdl/i_system.cpp +++ b/src/posix/sdl/i_system.cpp @@ -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 // @@ -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, ...) diff --git a/src/utility/files_decompress.cpp b/src/utility/files_decompress.cpp index 6a5596a0a..03be66b7a 100644 --- a/src/utility/files_decompress.cpp +++ b/src/utility/files_decompress.cpp @@ -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 diff --git a/src/win32/i_main.cpp b/src/win32/i_main.cpp index af9d63164..fdf2f51f9 100644 --- a/src/win32/i_main.cpp +++ b/src/win32/i_main.cpp @@ -749,6 +749,11 @@ void PeekThreadedErrorPane() PeekMessage(&msg, 0, 0, 0, PM_NOREMOVE); } +static void UnTbp() +{ + timeEndPeriod(TimerPeriod); +} + //========================================================================== // // DoMain @@ -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) diff --git a/src/win32/i_system.cpp b/src/win32/i_system.cpp index a004ddd43..9fdf14dd0 100644 --- a/src/win32/i_system.cpp +++ b/src/win32/i_system.cpp @@ -143,7 +143,6 @@ int sys_ostype = 0; // PRIVATE DATA DEFINITIONS ------------------------------------------------ static ticcmd_t emptycmd; -static bool HasExited; static WadStuff *WadList; static int NumWads; @@ -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(); -} - //========================================================================== // @@ -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(); } //==========================================================================