diff --git a/include/hack.h b/include/hack.h index d973ac6d6..ebf9b1ee6 100644 --- a/include/hack.h +++ b/include/hack.h @@ -1406,6 +1406,15 @@ typedef uint32_t mmflags_nht; /* makemon MM_ flags */ /* pick a random entry from array */ #define ROLL_FROM(array) array[rn2(SIZE(array))] +/* array with terminator variation */ +/* #define ROLL_FROMT(array) array[rn2(SIZE(array) - 1)] */ + +/* validate index of array */ +#define IndexOk(idx, array) \ + ((idx) >= 0 && (idx) < SIZE(array)) +/* array with terminator variation */ +#define IndexOkT(idx, array) \ + ((idx) >= 0 && (idx) < (SIZE(array) - 1)) #define FEATURE_NOTICE_VER(major, minor, patch) \ (((unsigned long) major << 24) | ((unsigned long) minor << 16) \ diff --git a/src/cmd.c b/src/cmd.c index a2c623da3..1272b87c7 100644 --- a/src/cmd.c +++ b/src/cmd.c @@ -4141,7 +4141,7 @@ wiz_display_macros(void) putstr(win, 0, buf); } /* check against defsyms array subscripts */ - if (test < 0 || test >= SIZE(defsyms)) { + if (!IndexOk(test, defsyms)) { if (!trouble++) putstr(win, 0, display_issues); Sprintf(buf, "glyph_to_cmap(glyph=%d) returns %d" diff --git a/src/do_name.c b/src/do_name.c index 03cd077cb..da25e345d 100644 --- a/src/do_name.c +++ b/src/do_name.c @@ -2591,7 +2591,7 @@ hliquid( if (liquidpref && *liquidpref) ++count; indx = rn2_on_display_rng(count); - if (indx < SIZE(hliquids)) + if (IndexOk(indx, hliquids)) return hliquids[indx]; } return liquidpref; @@ -2742,7 +2742,7 @@ lookup_novel(const char *lookname, int *idx) } } /* name not found; if novelidx is already set, override the name */ - if (idx && *idx >= 0 && *idx < SIZE(sir_Terry_novels)) + if (idx && IndexOk(*idx, sir_Terry_novels)) return sir_Terry_novels[*idx]; return (const char *) 0; diff --git a/src/monmove.c b/src/monmove.c index 253ff3f45..147063f92 100644 --- a/src/monmove.c +++ b/src/monmove.c @@ -1817,7 +1817,7 @@ m_move(register struct monst *mtmp, int after) * mfndpos) has no effect for normal attacks, though it lets a * confused monster attack you by accident. */ - assert(chi >= 0 && chi < SIZE(info)); + assert(IndexOk(chi, info)); if (info[chi] & ALLOW_U) { nix = mtmp->mux; niy = mtmp->muy; diff --git a/src/sounds.c b/src/sounds.c index cfeea58a8..2b2d68932 100644 --- a/src/sounds.c +++ b/src/sounds.c @@ -1767,7 +1767,7 @@ activate_chosen_soundlib(void) { int idx = gc.chosen_soundlib; - if (idx < soundlib_nosound || idx >= SIZE(soundlib_choices)) + if (!IndexOk(idx, soundlib_choices)) panic("activate_chosen_soundlib: invalid soundlib (%d)", idx); if (ga.active_soundlib != soundlib_nosound || idx != soundlib_nosound) { @@ -1784,7 +1784,7 @@ activate_chosen_soundlib(void) void assign_soundlib(int idx) { - if (idx < soundlib_nosound || idx >= SIZE(soundlib_choices)) + if (!IndexOk(idx, soundlib_choices)) panic("assign_soundlib: invalid soundlib (%d)", idx); gc.chosen_soundlib @@ -1854,7 +1854,7 @@ get_soundlib_name(char *dest, int maxlen) const char *src; idx = ga.active_soundlib; - if (idx < soundlib_nosound || idx >= SIZE(soundlib_choices)) + if (!IndexOk(idx, soundlib_choices)) panic("get_soundlib_name: invalid active_soundlib (%d)", idx); src = soundlib_choices[idx].sndprocs->soundname; diff --git a/src/timeout.c b/src/timeout.c index 315e82167..1ae644098 100644 --- a/src/timeout.c +++ b/src/timeout.c @@ -112,7 +112,7 @@ static const struct propname { const char * property_by_index(int idx, int *propertynum) { - if (!(idx >= 0 && idx < SIZE(propertynames) - 1)) + if (!IndexOkT(idx, propertynames)) idx = SIZE(propertynames) - 1; if (propertynum) diff --git a/src/trap.c b/src/trap.c index af158c373..2ffca0de5 100644 --- a/src/trap.c +++ b/src/trap.c @@ -1283,7 +1283,7 @@ trapeffect_sqky_board( } } else { seetrap(trap); - if (trap->tnote >= 0 && trap->tnote < SIZE(tsnds)) { + if (IndexOk(trap->tnote, tsnds)) { Soundeffect(tsnds[trap->tnote], 50); } pline("A board beneath you %s%s%s.", @@ -1300,7 +1300,7 @@ trapeffect_sqky_board( /* stepped on a squeaky board */ if (in_sight) { if (!Deaf) { - if (trap->tnote >= 0 && trap->tnote < SIZE(tsnds)) { + if (IndexOk(trap->tnote, tsnds)) { Soundeffect(tsnds[trap->tnote], 50); } pline("A board beneath %s squeaks %s loudly.", @@ -1315,7 +1315,7 @@ trapeffect_sqky_board( int range = couldsee(mtmp->mx, mtmp->my) /* 9 or 5 */ ? (BOLT_LIM + 1) : (BOLT_LIM - 3); - if (trap->tnote >= 0 && trap->tnote < SIZE(tsnds)) { + if (IndexOk(trap->tnote, tsnds)) { Soundeffect(tsnds[trap->tnote], ((mdistu(mtmp) <= range * range) ? 40 : 20));