From: PatR Date: Sat, 22 Feb 2020 09:41:04 +0000 (-0800) Subject: fix github issue #302 - divide by 0 crash X-Git-Tag: NetHack-3.6.6_Released~12 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=60bc28154ab63c294bab3bc8ec0c603eb5cfca3f;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/src/do_name.c b/src/do_name.c index ed0eea5a1..f1d8d5bb9 100644 --- a/src/do_name.c +++ b/src/do_name.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 do_name.c $NHDT-Date: 1574419578 2019/11/22 10:46:18 $ $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.152 $ */ +/* 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. */ @@ -2023,15 +2023,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 870f19342..7e5df7062 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. */ @@ -291,23 +291,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); @@ -400,7 +403,8 @@ int fd, mode; if (perform_bwrite(mode)) { bwrite(fd, (genericptr_t) &oracle_cnt, sizeof oracle_cnt); if (oracle_cnt) - bwrite(fd, (genericptr_t) oracle_loc, oracle_cnt * sizeof(long)); + bwrite(fd, (genericptr_t) oracle_loc, + oracle_cnt * sizeof(long)); } if (release_data(mode)) { if (oracle_cnt) {