From 3c6303b34e43faf2f8044d84fbc22b418ce8d89f Mon Sep 17 00:00:00 2001 From: PatR Date: Tue, 8 Oct 2019 14:23:27 -0700 Subject: [PATCH] fix #H9266 - redundant obj init Sword given to angels used obj->spe = max(obj->spe, rn2(4)) [except using a temporary to sanely work with max() macro]. But the obj was explicitly created as no-init, so obj->spe was always 0 and the max() was pointless. Shield given to angels was manipulating bless/curse state directly instead of using the functions intended for that, a no-no and also pointless to be clearing 'cursed' for a no-init item. Mace for priests had useless handling for object creation failure. Object creation failure could only happen if the mksobj() call had a valid entry in objects[] (or out of bounds access that didn't crash) for an object class that it doesn't know how to handle. That can't happen unless somebody screws up big time. If it ever did happen, it would have produced a memory leak. --- src/makemon.c | 19 ++++++++----------- src/mkobj.c | 3 ++- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/src/makemon.c b/src/makemon.c index 2350e77a0..6d3635cd2 100644 --- a/src/makemon.c +++ b/src/makemon.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 makemon.c $NHDT-Date: 1561236435 2019/06/22 20:47:15 $ $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.138 $ */ +/* NetHack 3.6 makemon.c $NHDT-Date: 1570569787 2019/10/08 21:23:07 $ $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.140 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /*-Copyright (c) Robert Patrick Rankin, 2012. */ /* NetHack may be freely redistributed. See license for details. */ @@ -166,7 +166,7 @@ register struct monst *mtmp; register struct permonst *ptr = mtmp->data; register int mm = monsndx(ptr); struct obj *otmp; - int bias, spe2, w1, w2; + int bias, w1, w2; if (Is_rogue_level(&u.uz)) return; @@ -259,12 +259,10 @@ register struct monst *mtmp; } else if (ptr->msound == MS_PRIEST || quest_mon_represents_role(ptr, PM_PRIEST)) { otmp = mksobj(MACE, FALSE, FALSE); - if (otmp) { - otmp->spe = rnd(3); - if (!rn2(2)) - curse(otmp); - (void) mpickobj(mtmp, otmp); - } + otmp->spe = rnd(3); + if (!rn2(2)) + curse(otmp); + (void) mpickobj(mtmp, otmp); } else if (mm == PM_NINJA) { /* extra quest villains */ (void) mongets(mtmp, rn2(4) ? SHURIKEN : DART); (void) mongets(mtmp, rn2(4) ? SHORT_SWORD : AXE); @@ -336,14 +334,13 @@ register struct monst *mtmp; artiname(rn2(2) ? ART_DEMONBANE : ART_SUNSWORD)); bless(otmp); otmp->oerodeproof = TRUE; - spe2 = rn2(4); - otmp->spe = max(otmp->spe, spe2); + otmp->spe = rn2(4); (void) mpickobj(mtmp, otmp); otmp = mksobj(!rn2(4) || is_lord(ptr) ? SHIELD_OF_REFLECTION : LARGE_SHIELD, FALSE, FALSE); - otmp->cursed = FALSE; + /* uncurse(otmp); -- mksobj(,FALSE,) item is always uncursed */ otmp->oerodeproof = TRUE; otmp->spe = 0; (void) mpickobj(mtmp, otmp); diff --git a/src/mkobj.c b/src/mkobj.c index d3cede639..24124f706 100644 --- a/src/mkobj.c +++ b/src/mkobj.c @@ -1,4 +1,4 @@ -/* NetHack 3.6 mkobj.c $NHDT-Date: 1570566379 2019/10/08 20:26:19 $ $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.152 $ */ +/* NetHack 3.6 mkobj.c $NHDT-Date: 1570569798 2019/10/08 21:23:18 $ $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.153 $ */ /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ /*-Copyright (c) Derek S. Ray, 2015. */ /* NetHack may be freely redistributed. See license for details. */ @@ -1064,6 +1064,7 @@ boolean artif; default: impossible("impossible mkobj %d, sym '%c'.", otmp->otyp, objects[otmp->otyp].oc_class); + dealloc_obj(otmp); /* free() would suffice here */ return (struct obj *) 0; } }