From 4f15e134fc7b12fa9e44f1efb1a521ced8d9631e Mon Sep 17 00:00:00 2001 From: Mika Kuoppala Date: Wed, 13 Dec 2023 19:15:49 +0200 Subject: [PATCH] src/mon: Avoid addressing with invalid corpsenm in mstoning corpsenm can be -1 (for EGGs and TINs) so touch_petrifies using this as index would point to bad entry. Fix this by making mstoning as helper function and bailing out early if corpsenm <= LOW_PM (while keeping MEDUSA as is) --- src/mon.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/mon.c b/src/mon.c index 6e9c8ae47..d9e7067c4 100644 --- a/src/mon.c +++ b/src/mon.c @@ -11,6 +11,7 @@ static void sanity_check_single_mon(struct monst *, boolean, const char *); static struct obj *make_corpse(struct monst *, unsigned); static int minliquid_core(struct monst *); static void m_calcdistress(struct monst *); +static boolean mstoning(const struct obj *obj); static boolean monlineu(struct monst *, int, int); static long mm_2way_aggression(struct monst *, struct monst *); static long mm_aggression(struct monst *, struct monst *); @@ -1156,9 +1157,19 @@ meatbox(struct monst *mon, struct obj *otmp) } } -#define mstoning(obj) \ - (ofood(obj) && (touch_petrifies(&mons[(obj)->corpsenm]) \ - || (obj)->corpsenm == PM_MEDUSA)) +static boolean +mstoning(const struct obj *obj) +{ + const int nm = obj->corpsenm; + + if (nm < LOW_PM) + return FALSE; + + if (nm == PM_MEDUSA) + return TRUE; + + return ofood(obj) && touch_petrifies(&mons[nm]); +} /* monster consumes an object. @@ -1418,8 +1429,6 @@ meatobj(struct monst* mtmp) /* for gelatinous cubes */ return (count > 0 || ecount > 0) ? 1 : 0; } -#undef mstoning - /* Monster eats a corpse off the ground. * Return value is 0 = nothing eaten, 1 = ate a corpse, 2 = died */ int