fix github issue #309 - random role segfault

Role selection is insanely complex.  I had to use a debugger to force
the relevant routine to be executed.

The analysis was correct:  it could use rn2(14) to pick a role (valid
values 0 through 12) and randomly getting 13 would lead to a crash.

The terminating element of roles[] passes all the ok_role(), ok_race(),
etc tests.  Explicitly exclude that element when collecting the roles
to choose from.
This commit is contained in:
PatR
2020-03-01 14:36:28 -08:00
parent 0f3719d15e
commit ffcd471ef7
2 changed files with 5 additions and 4 deletions

View File

@@ -1,4 +1,4 @@
$NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.114 $ $NHDT-Date: 1583073988 2020/03/01 14:46:28 $
$NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.115 $ $NHDT-Date: 1583102142 2020/03/01 22:35:42 $
General Fixes and Modified Features
-----------------------------------
@@ -60,6 +60,7 @@ prevent ravens from blinding other ravens: /corvus oculum corvi non eruit/
have ^X provide more information when held or swallowed
avoid divide by 0 crash if 'bogusmon' (file of bogus monster types) is empty
display wasn't updating immediately after toggling hilite_pet option
randomly choosing role could lead to crash via segfault
Fixes to 3.7.0-x Problems that Were Exposed Via git Repository

View File

@@ -1,4 +1,4 @@
/* NetHack 3.6 role.c $NHDT-Date: 1578947634 2020/01/13 20:33:54 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.68 $ */
/* NetHack 3.6 role.c $NHDT-Date: 1583102142 2020/03/01 22:35:42 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.69 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985-1999. */
/*-Copyright (c) Robert Patrick Rankin, 2012. */
/* NetHack may be freely redistributed. See license for details. */
@@ -749,7 +749,7 @@ randrole_filtered()
/* this doesn't rule out impossible combinations but attempts to
honor all the filter masks */
for (i = 0; i < SIZE(roles); ++i)
for (i = 0; i < SIZE(roles) - 1; ++i) /* -1: avoid terminating element */
if (ok_role(i, ROLE_NONE, ROLE_NONE, ROLE_NONE)
&& ok_race(i, ROLE_RANDOM, ROLE_NONE, ROLE_NONE)
&& ok_gend(i, ROLE_NONE, ROLE_RANDOM, ROLE_NONE)
@@ -1939,7 +1939,7 @@ boolean preselect;
* 1 - The Rogue Leader is the Tourist Nemesis.
* 2 - Priests start with a random alignment - convert the leader and
* guardians here.
* 3 - Priests also get their of deities from a randomly chosen role.
* 3 - Priests also get their set of deities from a randomly chosen role.
* 4 - [obsolete] Elves can have one of two different leaders,
* but can't work it out here because it requires hacking the
* level file data (see sp_lev.c).