add a pair of shorthand macros to validate an index into an array

Two variations:

IndexOk(idx, array)       validate that idx is a valid index into the array

IndexOkT(idx, array)      validate that idx is a valid index into the
                          array, excluding the final Terminator element
This commit is contained in:
nhmall
2023-12-23 13:46:54 -05:00
parent 415f8c559b
commit e9e05db113
7 changed files with 20 additions and 11 deletions

View File

@@ -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) \

View File

@@ -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"

View File

@@ -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;

View File

@@ -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;

View File

@@ -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;

View File

@@ -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)

View File

@@ -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));