From 40f3251cf450d988831be9b593afa8c6f2e6e82c Mon Sep 17 00:00:00 2001 From: "nethack.allison" Date: Fri, 5 Dec 2003 12:30:18 +0000 Subject: [PATCH] prevent infinite recursion in impossible --- doc/fixes34.3 | 1 + include/decl.h | 4 ++++ src/files.c | 16 ++++++++++------ src/pline.c | 4 ++++ 4 files changed, 19 insertions(+), 6 deletions(-) diff --git a/doc/fixes34.3 b/doc/fixes34.3 index 1ff7d8241..519b47695 100644 --- a/doc/fixes34.3 +++ b/doc/fixes34.3 @@ -100,6 +100,7 @@ prevent "see it drop from your pack" when figurine monster becomes undetected attempting to drop a subset of a stack of multiple cursed loadstones could corrupt inventory or cause a crash "miss" message was missing for thrown or kicked gold not caught by a monster +prevent recursive impossible() and panic() calls from leading to a stack overflow Platform- and/or Interface-Specific Fixes diff --git a/include/decl.h b/include/decl.h index 734e8ec02..dac11660e 100644 --- a/include/decl.h +++ b/include/decl.h @@ -152,6 +152,10 @@ E NEARDATA struct sinfo { int panicking; /* `panic' is in progress */ #if defined(VMS) || defined(WIN32) int exiting; /* an exit handler is executing */ +#endif + int in_impossible; +#ifdef PANICLOG + int in_paniclog; #endif } program_state; diff --git a/src/files.c b/src/files.c index 1cca1ca06..d2e3086dc 100644 --- a/src/files.c +++ b/src/files.c @@ -2275,12 +2275,16 @@ const char *reason; /* explanation */ FILE *lfile; char buf[BUFSZ]; - lfile = fopen_datafile(PANICLOG, "a", TROUBLEPREFIX); - if (lfile) { - (void) fprintf(lfile, "%s %08ld: %s %s\n", - version_string(buf), yyyymmdd((time_t)0L), - type, reason); - (void) fclose(lfile); + if (!program_state.in_paniclog) { + program_state.in_paniclog = 1; + lfile = fopen_datafile(PANICLOG, "a", TROUBLEPREFIX); + if (lfile) { + (void) fprintf(lfile, "%s %08ld: %s %s\n", + version_string(buf), yyyymmdd((time_t)0L), + type, reason); + (void) fclose(lfile); + } + program_state.in_paniclog = 0; } #endif /* PANICLOG */ return; diff --git a/src/pline.c b/src/pline.c index 2febc95f0..60218fe15 100644 --- a/src/pline.c +++ b/src/pline.c @@ -252,6 +252,9 @@ void impossible VA_DECL(const char *, s) VA_START(s); VA_INIT(s, const char *); + if (program_state.in_impossible) + panic("impossible called impossible"); + program_state.in_impossible = 1; { char pbuf[BUFSZ]; Vsprintf(pbuf,s,VA_ARGS); @@ -259,6 +262,7 @@ impossible VA_DECL(const char *, s) } vpline(s,VA_ARGS); pline("Program in disorder - perhaps you'd better #quit."); + program_state.in_impossible = 0; VA_END(); } -- 2.40.0