fix reported crash of TTY_PERM_INVENT segfaulting

Options processing can be early, even before ttyDisplay is allocated.
If we find that TTY_PERM_INVENT initialization is happening too early,
just set a marker (iflags.perm_invent_pending) to try again a bit later.

The changes in win/share are just to be able to sucessfully
reproduce the original issue on Windows. It was easily reproduced
on Unix, just by building with TTY_PERM_INVENT in include/config.h
and setting OPTIONS=perm_invent in config file.
This commit is contained in:
nhmall
2025-01-02 11:46:15 -05:00
parent 2d4f9893ad
commit d3c57e1b42
8 changed files with 74 additions and 16 deletions

View File

@@ -2262,6 +2262,9 @@ extern int msgtype_type(const char *, boolean) NONNULLARG1;
extern void hide_unhide_msgtypes(boolean, int);
extern void msgtype_free(void);
extern void options_free_window_colors(void);
#ifdef TTY_PERM_INVENT
extern void check_perm_invent_again(void);
#endif
/* ### pager.c ### */

View File

@@ -319,6 +319,7 @@ struct instance_flags {
boolean news; /* print news */
boolean num_pad; /* use numbers for movement commands */
boolean perm_invent; /* display persistent inventory window */
boolean perm_invent_pending; /* need to try again */
boolean renameallowed; /* can change hero name during role selection */
boolean renameinprogress; /* we are changing hero name */
boolean sounds; /* master on/off switch for using soundlib */

View File

@@ -205,6 +205,7 @@ enum to_core_flags {
too_small = 0x002,
prohibited = 0x004,
no_init_done = 0x008,
too_early = 0x010,
};
enum from_core_requests {

View File

@@ -720,6 +720,10 @@ init_sound_disp_gamewindows(void)
display_nhwindow(WIN_MESSAGE, FALSE);
clear_glyph_buffer();
display_nhwindow(WIN_MAP, FALSE);
#ifdef TTY_PERM_INVENT
if (iflags.perm_invent_pending)
check_perm_invent_again();
#endif
}
void

View File

@@ -6149,6 +6149,12 @@ sync_perminvent(void)
|| in_perm_invent_toggled) {
wri = ctrl_nhwindow(WIN_INVEN, request_settings, &wri_info);
if (wri != 0) {
if ((wri->tocore.tocore_flags & (too_early)) != 0) {
/* don't be too noisy about this as it's really
* a startup timing issue. Just set a marker. */
iflags.perm_invent_pending = TRUE;
return;
}
if ((wri->tocore.tocore_flags & (too_small | prohibited))
!= 0) {
/* sizes aren't good enough */

View File

@@ -5384,7 +5384,11 @@ can_set_perm_invent(void)
iflags.perminv_mode = InvOptOn;
#ifdef TTY_PERM_INVENT
if (WINDOWPORT(tty) && !go.opt_initial) {
if ((WINDOWPORT(tty)
#ifdef WIN32
|| WINDOWPORT(safestartup)
#endif
) && !go.opt_initial) {
perm_invent_toggled(FALSE);
/* perm_invent_toggled()
-> sync_perminvent()
@@ -5401,6 +5405,20 @@ can_set_perm_invent(void)
return TRUE;
}
#ifdef TTY_PERM_INVENT
void
check_perm_invent_again(void)
{
if (iflags.perm_invent_pending) {
iflags.perm_invent = FALSE;
if (can_set_perm_invent())
iflags.perm_invent = TRUE;
iflags.perm_invent_pending = FALSE;
}
}
#endif
staticfn int
handler_menustyle(void)
{

View File

@@ -510,13 +510,23 @@ safe_update_inventory(int arg UNUSED)
return;
}
#ifdef WIN32CON
extern win_request_info *tty_ctrl_nhwindow(winid window UNUSED,
int request UNUSED,
win_request_info *wri UNUSED);
#endif
win_request_info *
safe_ctrl_nhwindow(
winid window UNUSED,
int request UNUSED,
win_request_info *wri UNUSED)
{
#ifdef WIN32CON
return (*tty_ctrl_nhwindow)(window, request, wri);
#else
return (win_request_info *) 0;
#endif
}
/**************************************************************

View File

@@ -2861,22 +2861,29 @@ tty_ctrl_nhwindow(
wri->tocore = zero_tocore;
tty_ok = assesstty(ttyinvmode, &offx, &offy, &rows, &cols, &maxcol,
&minrow, &maxrow);
wri->tocore.needrows = (int) (minrow + 1 + ROWNO + StatusRows());
wri->tocore.needcols = (int) tty_perminv_mincol;
wri->tocore.haverows = (int) ttyDisplay->rows;
wri->tocore.havecols = (int) ttyDisplay->cols;
if (!tty_ok) {
#ifdef RESIZABLE
/* terminal isn't big enough right now but player might resize it
and then use 'm O' to try to set 'perm_invent' again */
wri->tocore.tocore_flags |= too_small;
#else
wri->tocore.tocore_flags |= prohibited;
#endif
if (!tty_ok && rows == 0 && cols == 0) {
/* something is terribly wrong, possibly too early in startup */
wri->tocore.tocore_flags |= too_early;
} else {
maxslot = (maxrow - 2) * (!inuse_only ? 2 : 1);
wri->tocore.maxslot = maxslot;
}
wri->tocore.needrows = (int) (minrow + 1 + ROWNO + StatusRows());
wri->tocore.needcols = (int) tty_perminv_mincol;
wri->tocore.haverows = (int) ttyDisplay->rows;
wri->tocore.havecols = (int) ttyDisplay->cols;
if (!tty_ok) {
#ifdef RESIZABLE
/* terminal isn't big enough right now but player might
* resize it and then use 'm O' to try to set 'perm_invent'
* again
*/
wri->tocore.tocore_flags |= too_small;
#else
wri->tocore.tocore_flags |= prohibited;
#endif
} else {
maxslot = (maxrow - 2) * (!inuse_only ? 2 : 1);
wri->tocore.maxslot = maxslot;
}
}
#endif /* TTY_PERM_INVENT */
break;
case set_menu_promptstyle:
@@ -3542,6 +3549,14 @@ assesstty(
show_gold = (invmode & InvShowGold) != 0 && !inuse_only;
int perminv_minrow = tty_perminv_minrow + (show_gold ? 1 : 0);
if (!ttyDisplay) {
/* too early */
*offx = *offy = *rows = *cols = 0;
*maxcol = 0;
*minrow = *maxrow = 0;
return !(*rows < perminv_minrow || *cols < tty_perminv_mincol);
}
*offx = 0;
/* topline + map rows + status lines */
*offy = 1 + ROWNO + StatusRows(); /* 1 + 21 + (2 or 3) */