rnd_class()
Guard against potential bad arguments: first index greaer than last. Return STRANGE_OBJECT instead of hardcoded 0 if it ever fails (which should be impossible with good arguments).
This commit is contained in:
25
src/objnam.c
25
src/objnam.c
@@ -1,4 +1,4 @@
|
|||||||
/* NetHack 3.7 objnam.c $NHDT-Date: 1578535246 2020/01/09 02:00:46 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.285 $ */
|
/* NetHack 3.7 objnam.c $NHDT-Date: 1578896203 2020/01/13 06:16:43 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.287 $ */
|
||||||
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
|
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
|
||||||
/*-Copyright (c) Robert Patrick Rankin, 2011. */
|
/*-Copyright (c) Robert Patrick Rankin, 2011. */
|
||||||
/* NetHack may be freely redistributed. See license for details. */
|
/* NetHack may be freely redistributed. See license for details. */
|
||||||
@@ -4237,17 +4237,18 @@ int first, last;
|
|||||||
{
|
{
|
||||||
int i, x, sum = 0;
|
int i, x, sum = 0;
|
||||||
|
|
||||||
if (first == last)
|
if (last > first) {
|
||||||
return first;
|
for (i = first; i <= last; i++)
|
||||||
for (i = first; i <= last; i++)
|
sum += objects[i].oc_prob;
|
||||||
sum += objects[i].oc_prob;
|
if (!sum) /* all zero, so equal probability */
|
||||||
if (!sum) /* all zero */
|
return rn1(last - first + 1, first);
|
||||||
return first + rn2(last - first + 1);
|
|
||||||
x = rnd(sum);
|
x = rnd(sum);
|
||||||
for (i = first; i <= last; i++)
|
for (i = first; i <= last; i++)
|
||||||
if (objects[i].oc_prob && (x -= objects[i].oc_prob) <= 0)
|
if ((x -= objects[i].oc_prob) <= 0)
|
||||||
return i;
|
return i;
|
||||||
return 0;
|
}
|
||||||
|
return (first == last) ? first : STRANGE_OBJECT;
|
||||||
}
|
}
|
||||||
|
|
||||||
static const char *
|
static const char *
|
||||||
|
|||||||
Reference in New Issue
Block a user