From 537fd6dd74432804d27dde08934a51e2913edff5 Mon Sep 17 00:00:00 2001 From: PatR Date: Sat, 25 May 2024 13:49:12 -0700 Subject: [PATCH] fix gremlin cloning fix The revised code returned an uninitialized variable if the new test failed. The cloneu() case was ok but this changes it to keep parallel to the clone_mon() case. --- src/makemon.c | 5 +++-- src/potion.c | 19 +++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/makemon.c b/src/makemon.c index 5027efcaa..cd085da67 100644 --- a/src/makemon.c +++ b/src/makemon.c @@ -821,8 +821,9 @@ m_initinv(struct monst *mtmp) /* Note: for long worms, always call cutworm (cutworm calls clone_mon) */ struct monst * -clone_mon(struct monst *mon, - coordxy x, coordxy y) /* clone's preferred location or 0 (near mon) */ +clone_mon( + struct monst *mon, + coordxy x, coordxy y) /* clone's preferred location or 0 (near mon) */ { coord mm; struct monst *m2; diff --git a/src/potion.c b/src/potion.c index 9a4731e2c..c0c4df819 100644 --- a/src/potion.c +++ b/src/potion.c @@ -1,4 +1,4 @@ -/* NetHack 3.7 potion.c $NHDT-Date: 1704316448 2024/01/03 21:14:08 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.256 $ */ +/* NetHack 3.7 potion.c $NHDT-Date: 1716668700 2024/05/25 20:25:00 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.265 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /*-Copyright (c) Robert Patrick Rankin, 2013. */ /* NetHack may be freely redistributed. See license for details. */ @@ -2827,16 +2827,27 @@ split_mon( : (const char *) s_suffix(mon_nam(mtmp))); if (mon == &gy.youmonst) { - mtmp2 = cloneu(); + if (u.mh > u.mhmax) /* sanity precaution */ + u.mh = u.mhmax; + mtmp2 = (u.mh > 1) ? cloneu() : (struct monst *) 0; if (mtmp2) { + /* mtmp2 has been created with mhpmax = u.mhmax, mhp = u.mh / 2, + and u.mh -= mtmp2->mhpmax; these reductions for both max hp + can't make either of them exceed corresponding current hp */ mtmp2->mhpmax = u.mhmax / 2; u.mhmax -= mtmp2->mhpmax; disp.botl = TRUE; You("multiply%s!", reason); } - } else if (mon->mhpmax > 1) { - mtmp2 = clone_mon(mon, 0, 0); + } else { + if (mon->mhp > mon->mhpmax) /* sanity precaution */ + mon->mhp = mon->mhpmax; + mtmp2 = (mon->mhp > 1) ? clone_mon(mon, 0, 0) : (struct monst *) 0; if (mtmp2) { + assert(mon->mhpmax >= mon->mhp); /* mon->mhpmax > 1 */ + /* mtmp2 has been created with mhpmax = mon->mhpmax, + mhp = mon->mhp / 2, and mon->mh -= mtmp2->mhp; + dividing max by 2 can't result in it exceeding current */ mtmp2->mhpmax = mon->mhpmax / 2; mon->mhpmax -= mtmp2->mhpmax; if (canspotmon(mon))