From 70dcab833d630c7ca54d4182ed9771384e14ef82 Mon Sep 17 00:00:00 2001 From: nhmall Date: Sat, 16 Dec 2023 07:58:44 -0500 Subject: [PATCH] remove obj guard from stone_missile(obj) macro Checking the callers: toss_up() would have segfaulted prior to use of stone_missile() if obj were NULL. thitu() now has a guard prior to use of stone_missile() ohitmon() would have crashed from earlier dereference otmp->dknown if it were NULL, otmp arg is declared nonnull thitm() now has a guard prior to use of stone_missile(). hmon_hitmon_do_hit() null obj takes a different code path than the code path using stone_missile(); comment asserting that added --- include/obj.h | 2 +- src/artifact.c | 6 ++---- src/dothrow.c | 2 +- src/mthrowu.c | 4 +++- src/trap.c | 4 ++-- src/uhitm.c | 2 ++ 6 files changed, 11 insertions(+), 9 deletions(-) diff --git a/include/obj.h b/include/obj.h index f92a0b02c..2b40a2ee9 100644 --- a/include/obj.h +++ b/include/obj.h @@ -257,7 +257,7 @@ struct obj { /* 'missile' aspect is up to the caller and does not imply is_missile(); rings might be launched as missiles when being scattered by an explosion */ #define stone_missile(o) \ - ((o) && (objects[(o)->otyp].oc_material == GEMSTONE \ + ((objects[(o)->otyp].oc_material == GEMSTONE \ || (objects[(o)->otyp].oc_material == MINERAL)) \ && (o)->oclass != RING_CLASS) diff --git a/src/artifact.c b/src/artifact.c index a1fe0e2d3..282f7e511 100644 --- a/src/artifact.c +++ b/src/artifact.c @@ -17,8 +17,7 @@ static struct artifact *get_artifact(struct obj *) NONNULL; /* never returns null */ /* #define get_artifact(o) \ - (((o) && ((o)->artifact > 0 && (o)->artifact < AFTER_LAST_ARTIFACT)) \ - ? &artilist[(int) (o)->oartifact] \ + (((o) && (o)->oartifact) ? &artilist[(int) (o)->oartifact] \ : &artilist[ART_NONARTIFACT]) */ static boolean bane_applies(const struct artifact *, struct monst *) NONNULLARG12; @@ -2546,8 +2545,7 @@ is_art(struct obj *obj, int art) } /* #define get_artifact(o) \ - (((o) && ((o)->artifact > 0 && (o)->artifact < AFTER_LAST_ARTIFACT)) \ - ? &artilist[(int) (o)->oartifact] \ + (((o) && (o)->oartifact) ? &artilist[(int) (o)->oartifact] \ : &artilist[ART_NONARTIFACT]) */ static struct artifact * diff --git a/src/dothrow.c b/src/dothrow.c index e04f22e4e..3fa52381e 100644 --- a/src/dothrow.c +++ b/src/dothrow.c @@ -13,7 +13,7 @@ static int throw_ok(struct obj *); static void autoquiver(void); static struct obj *find_launcher(struct obj *); static int gem_accept(struct monst *, struct obj *); -static boolean toss_up(struct obj *, boolean); +static boolean toss_up(struct obj *, boolean) NONNULLARG1; static void sho_obj_return_to_u(struct obj * obj); static struct obj *return_throw_to_inv(struct obj *, long, boolean, struct obj *); diff --git a/src/mthrowu.c b/src/mthrowu.c index 5829f05e0..5261cd780 100644 --- a/src/mthrowu.c +++ b/src/mthrowu.c @@ -120,7 +120,8 @@ thitu( if (is_acid && Acid_resistance) { pline("It doesn't seem to hurt you."); monstseesu(M_SEEN_ACID); - } else if (stone_missile(obj) && passes_rocks(gy.youmonst.data)) { + } else if (obj && stone_missile(obj) && passes_rocks(gy.youmonst.data)) { + /* use 'named' as an approximation for "hitting from above"; we avoid "passes through you" for horizontal flight path because missile stops and that wording would suggest that @@ -323,6 +324,7 @@ ohitmon( boolean vis, ismimic, objgone; struct obj *mon_launcher = gm.marcher ? MON_WEP(gm.marcher) : NULL; + /* assert(otmp != NULL); */ gn.notonhead = (gb.bhitpos.x != mtmp->mx || gb.bhitpos.y != mtmp->my); ismimic = M_AP_TYPE(mtmp) && M_AP_TYPE(mtmp) != M_AP_MONSTER; vis = cansee(gb.bhitpos.x, gb.bhitpos.y); diff --git a/src/trap.c b/src/trap.c index d3cead5b6..a77201f7d 100644 --- a/src/trap.c +++ b/src/trap.c @@ -67,7 +67,7 @@ static void untrap_box(struct obj *, boolean, boolean); #if 0 static void join_adjacent_pits(struct trap *); #endif -static boolean thitm(int, struct monst *, struct obj *, int, boolean); +static boolean thitm(int, struct monst *, struct obj *, int, boolean) NONNULLARG2; static void maybe_finish_sokoban(void); static const char *const a_your[2] = { "a", "your" }; @@ -6345,7 +6345,7 @@ thitm( pline("%s is almost hit by %s!", Monnam(mon), doname(obj)); } else { int dam = 1; - boolean harmless = (stone_missile(obj) && passes_rocks(mon->data)); + boolean harmless = (obj && stone_missile(obj) && passes_rocks(mon->data)); if (obj && cansee(mon->mx, mon->my)) pline("%s is hit by %s%s", Monnam(mon), doname(obj), diff --git a/src/uhitm.c b/src/uhitm.c index 465f676b4..d8e0ca3ee 100644 --- a/src/uhitm.c +++ b/src/uhitm.c @@ -1324,6 +1324,8 @@ hmon_hitmon_do_hit( if (!obj) { /* attack with bare hands */ hmon_hitmon_barehands(hmd, mon); } else { + /* obj is not NULL here because of the !obj check in this if block, + , so no guard is needed ahead of stone_missile(obj) */ /* stone missile does not hurt xorn or earth elemental, but doesn't pass all the way through and continue on to some further target */ if ((hmd->thrown == HMON_THROWN