]> granicus.if.org Git - nethack/commitdiff
fix github issue #309 - random role segfault
authorPatR <rankin@nethack.org>
Sun, 1 Mar 2020 22:36:28 +0000 (14:36 -0800)
committerPatR <rankin@nethack.org>
Sun, 1 Mar 2020 22:36:28 +0000 (14:36 -0800)
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.

doc/fixes37.0
src/role.c

index b2155813b311e9a314b954ead110a47ee92fa72b..1c65f3f3d5635523839c2569a8328a8be420bd23 100644 (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
index f93859da1cbcac29a1ac857fddab1f4d415ef219..39d964d3a2d15eee85d27ea0c16a553c9954090b 100644 (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).