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
This commit is contained in:
nhmall
2023-12-16 07:58:44 -05:00
parent 0099098d35
commit 70dcab833d
6 changed files with 11 additions and 9 deletions
+1 -1
View File
@@ -257,7 +257,7 @@ struct obj {
/* 'missile' aspect is up to the caller and does not imply is_missile(); /* '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 */ rings might be launched as missiles when being scattered by an explosion */
#define stone_missile(o) \ #define stone_missile(o) \
((o) && (objects[(o)->otyp].oc_material == GEMSTONE \ ((objects[(o)->otyp].oc_material == GEMSTONE \
|| (objects[(o)->otyp].oc_material == MINERAL)) \ || (objects[(o)->otyp].oc_material == MINERAL)) \
&& (o)->oclass != RING_CLASS) && (o)->oclass != RING_CLASS)
+2 -4
View File
@@ -17,8 +17,7 @@
static struct artifact *get_artifact(struct obj *) NONNULL; /* never returns null */ static struct artifact *get_artifact(struct obj *) NONNULL; /* never returns null */
/* #define get_artifact(o) \ /* #define get_artifact(o) \
(((o) && ((o)->artifact > 0 && (o)->artifact < AFTER_LAST_ARTIFACT)) \ (((o) && (o)->oartifact) ? &artilist[(int) (o)->oartifact] \
? &artilist[(int) (o)->oartifact] \
: &artilist[ART_NONARTIFACT]) */ : &artilist[ART_NONARTIFACT]) */
static boolean bane_applies(const struct artifact *, struct monst *) NONNULLARG12; 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) \ /* #define get_artifact(o) \
(((o) && ((o)->artifact > 0 && (o)->artifact < AFTER_LAST_ARTIFACT)) \ (((o) && (o)->oartifact) ? &artilist[(int) (o)->oartifact] \
? &artilist[(int) (o)->oartifact] \
: &artilist[ART_NONARTIFACT]) */ : &artilist[ART_NONARTIFACT]) */
static struct artifact * static struct artifact *
+1 -1
View File
@@ -13,7 +13,7 @@ static int throw_ok(struct obj *);
static void autoquiver(void); static void autoquiver(void);
static struct obj *find_launcher(struct obj *); static struct obj *find_launcher(struct obj *);
static int gem_accept(struct monst *, 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 void sho_obj_return_to_u(struct obj * obj);
static struct obj *return_throw_to_inv(struct obj *, long, boolean, static struct obj *return_throw_to_inv(struct obj *, long, boolean,
struct obj *); struct obj *);
+3 -1
View File
@@ -120,7 +120,8 @@ thitu(
if (is_acid && Acid_resistance) { if (is_acid && Acid_resistance) {
pline("It doesn't seem to hurt you."); pline("It doesn't seem to hurt you.");
monstseesu(M_SEEN_ACID); 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"; /* use 'named' as an approximation for "hitting from above";
we avoid "passes through you" for horizontal flight path we avoid "passes through you" for horizontal flight path
because missile stops and that wording would suggest that because missile stops and that wording would suggest that
@@ -323,6 +324,7 @@ ohitmon(
boolean vis, ismimic, objgone; boolean vis, ismimic, objgone;
struct obj *mon_launcher = gm.marcher ? MON_WEP(gm.marcher) : NULL; 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); gn.notonhead = (gb.bhitpos.x != mtmp->mx || gb.bhitpos.y != mtmp->my);
ismimic = M_AP_TYPE(mtmp) && M_AP_TYPE(mtmp) != M_AP_MONSTER; ismimic = M_AP_TYPE(mtmp) && M_AP_TYPE(mtmp) != M_AP_MONSTER;
vis = cansee(gb.bhitpos.x, gb.bhitpos.y); vis = cansee(gb.bhitpos.x, gb.bhitpos.y);
+2 -2
View File
@@ -67,7 +67,7 @@ static void untrap_box(struct obj *, boolean, boolean);
#if 0 #if 0
static void join_adjacent_pits(struct trap *); static void join_adjacent_pits(struct trap *);
#endif #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 void maybe_finish_sokoban(void);
static const char *const a_your[2] = { "a", "your" }; 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)); pline("%s is almost hit by %s!", Monnam(mon), doname(obj));
} else { } else {
int dam = 1; 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)) if (obj && cansee(mon->mx, mon->my))
pline("%s is hit by %s%s", Monnam(mon), doname(obj), pline("%s is hit by %s%s", Monnam(mon), doname(obj),
+2
View File
@@ -1324,6 +1324,8 @@ hmon_hitmon_do_hit(
if (!obj) { /* attack with bare hands */ if (!obj) { /* attack with bare hands */
hmon_hitmon_barehands(hmd, mon); hmon_hitmon_barehands(hmd, mon);
} else { } 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 /* stone missile does not hurt xorn or earth elemental, but doesn't
pass all the way through and continue on to some further target */ pass all the way through and continue on to some further target */
if ((hmd->thrown == HMON_THROWN if ((hmd->thrown == HMON_THROWN