From: PatR Date: Sat, 22 Feb 2020 09:41:04 +0000 (-0800) Subject: fix github issue #302 - divide by 0 crash X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=29321c6f80d67c89f174ac75d9d54e104109e3ec;p=nethack fix github issue #302 - divide by 0 crash The traceback points directly to the problem: divide by 0 happens if the 'bogusmon' file only contains the "do not edit" line, which would happen if 'bogusmon.txt' is empty. makedefs probably ought to complain about that. There is now one hardcoded bogus monster to fall back to: 'bogon'. Random tombstone epitaphs report divide by 0 if their text source is empty, but it is done by rn2() rather than rn2_for_display_rng() so is just a warning for pre-release code. It would crash for release version though. I tried placing an empty engravings file and expected similar results but didn't see any response. Not sure what that means. After the fix, empty epitaph file yields blank result so graves that want a random epitaph won't have any epitaph. Fixes #302 --- diff --git a/doc/fixes37.0 b/doc/fixes37.0 index dbea1b6b3..31a154c27 100644 --- a/doc/fixes37.0 +++ b/doc/fixes37.0 @@ -1,4 +1,4 @@ -$NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.106 $ $NHDT-Date: 1582321542 2020/02/21 21:45:42 $ +$NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.107 $ $NHDT-Date: 1582364458 2020/02/22 09:40:58 $ General Fixes and Modified Features ----------------------------------- @@ -58,6 +58,7 @@ give feedback if controlled level teleport attempt fails because hero is unseen pet that drowned didn't give "you have a sad feeling" message 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 Fixes to 3.7.0-x Problems that Were Exposed Via git Repository diff --git a/src/do_name.c b/src/do_name.c index 7e01e5878..a44374a9c 100644 --- a/src/do_name.c +++ b/src/do_name.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 do_name.c $NHDT-Date: 1581562587 2020/02/13 02:56:27 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.172 $ */ +/* NetHack 3.6 do_name.c $NHDT-Date: 1582364431 2020/02/22 09:40:31 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.174 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /*-Copyright (c) Pasi Kallinen, 2018. */ /* NetHack may be freely redistributed. See license for details. */ @@ -2054,15 +2054,16 @@ char *buf, *code; static const char bogon_codes[] = "-_+|="; /* see dat/bonusmon.txt */ char *mname = buf; + if (code) + *code = '\0'; + /* might fail (return empty buf[]) if the file isn't available */ get_rnd_text(BOGUSMONFILE, buf, rn2_on_display_rng); - /* strip prefix if present */ - if (index(bogon_codes, *mname)) { + if (!*mname) { + Strcpy(buf, "bogon"); + } else if (index(bogon_codes, *mname)) { /* strip prefix if present */ if (code) *code = *mname; ++mname; - } else { - if (code) - *code = '\0'; } return mname; } diff --git a/src/rumors.c b/src/rumors.c index 8d022388b..9a37dd220 100644 --- a/src/rumors.c +++ b/src/rumors.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 rumors.c $NHDT-Date: 1545132266 2018/12/18 11:24:26 $ $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.34 $ */ +/* NetHack 3.6 rumors.c $NHDT-Date: 1582364450 2020/02/22 09:40:50 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.51 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /*-Copyright (c) Robert Patrick Rankin, 2012. */ /* NetHack may be freely redistributed. See license for details. */ @@ -279,23 +279,26 @@ int FDECL((*rng), (int)); dlb *fh; buf[0] = '\0'; - fh = dlb_fopen(fname, "r"); - if (fh) { - /* TODO: cache sizetxt, starttxt, endtxt. maybe cache file contents? - */ - long sizetxt = 0, starttxt = 0, endtxt = 0, tidbit = 0; + /* TODO: cache sizetxt, starttxt, endtxt. maybe cache file contents? */ + long sizetxt = 0L, starttxt = 0L, endtxt = 0L, tidbit = 0L; char *endp, line[BUFSZ], xbuf[BUFSZ]; - (void) dlb_fgets(line, sizeof line, - fh); /* skip "don't edit" comment */ + + /* skip "don't edit" comment */ + (void) dlb_fgets(line, sizeof line, fh); (void) dlb_fseek(fh, 0L, SEEK_CUR); starttxt = dlb_ftell(fh); (void) dlb_fseek(fh, 0L, SEEK_END); endtxt = dlb_ftell(fh); sizetxt = endtxt - starttxt; - tidbit = rng(sizetxt); + /* might be zero (only if file is empty); should complain in that + case but if could happen over and over, also the suggestion + that save and restore might fix the problem wouldn't be useful */ + if (sizetxt < 1L) + return buf; + tidbit = (*rng)(sizetxt); (void) dlb_fseek(fh, starttxt + tidbit, SEEK_SET); (void) dlb_fgets(line, sizeof line, fh); @@ -387,10 +390,12 @@ NHFILE *nhfp; { if (perform_bwrite(nhfp)) { if (nhfp->structlevel) - bwrite(nhfp->fd, (genericptr_t) &g.oracle_cnt, sizeof g.oracle_cnt); + bwrite(nhfp->fd, (genericptr_t) &g.oracle_cnt, + sizeof g.oracle_cnt); if (g.oracle_cnt) { if (nhfp->structlevel) { - bwrite(nhfp->fd, (genericptr_t)g.oracle_loc, g.oracle_cnt * sizeof (long)); + bwrite(nhfp->fd, (genericptr_t) g.oracle_loc, + g.oracle_cnt * sizeof (long)); } } } @@ -412,7 +417,8 @@ NHFILE *nhfp; if (g.oracle_cnt) { g.oracle_loc = (unsigned long *) alloc(g.oracle_cnt * sizeof(long)); if (nhfp->structlevel) { - mread(nhfp->fd, (genericptr_t) g.oracle_loc, g.oracle_cnt * sizeof (long)); + mread(nhfp->fd, (genericptr_t) g.oracle_loc, + g.oracle_cnt * sizeof (long)); } g.oracle_flg = 1; /* no need to call init_oracles() */ }