From 31862b95b033ceba382f1b7188b033c20f860563 Mon Sep 17 00:00:00 2001 From: Ray Chason Date: Tue, 27 Sep 2022 20:57:09 -0400 Subject: [PATCH] Fix undefined behavior when exiting Curses * When saving: curses_exit_nhwindows calls curses_uncurse_terminal, which calls endwin. curses_exit_nhwindows then calls raw_print, which calls more Curses functions after endwin has been called. Fix this by having curses_raw_print use puts if window_inited is false. * When dying, quitting, etc.: really_done opens the "Goodbye" window, which refreshes the other windows when it closes. But the status window (and possibly the map and message windows) are gone by that point. The window pointers are properly NULLed, but the NULL is then passed to touchwin. Fix this by checking window pointers for NULL. --- win/curses/cursmain.c | 6 +++++- win/curses/curswins.c | 18 ++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/win/curses/cursmain.c b/win/curses/cursmain.c index 72979d5b2..40c20ae3c 100644 --- a/win/curses/cursmain.c +++ b/win/curses/cursmain.c @@ -855,7 +855,11 @@ curses_raw_print(const char *str) #ifdef PDCURSES /* WINDOW *win = curses_get_nhwin(MESSAGE_WIN); */ - curses_message_win_puts(str, FALSE); + if (iflags.window_inited) { + curses_message_win_puts(str, FALSE); + } else { + puts(str); + } #else puts(str); #endif diff --git a/win/curses/curswins.c b/win/curses/curswins.c index 1d19427a9..8491a1e35 100644 --- a/win/curses/curswins.c +++ b/win/curses/curswins.c @@ -176,12 +176,18 @@ curses_refresh_nethack_windows(void) touchwin(stdscr); refresh(); } else { - touchwin(status_window); - wnoutrefresh(status_window); - touchwin(map_window); - wnoutrefresh(map_window); - touchwin(message_window); - wnoutrefresh(message_window); + if (status_window != NULL) { + touchwin(status_window); + wnoutrefresh(status_window); + } + if (map_window != NULL) { + touchwin(map_window); + wnoutrefresh(map_window); + } + if (message_window != NULL) { + touchwin(message_window); + wnoutrefresh(message_window); + } if (inv_window) { touchwin(inv_window); wnoutrefresh(inv_window); -- 2.50.0