Fix some signed integer UB in RNG and related code
This commit is contained in:
parent
26a17c4e1f
commit
a5e179e530
@ -35,7 +35,7 @@ unsigned int RNG::operator()()
|
|||||||
int RNG::between(int lower, int upper)
|
int RNG::between(int lower, int upper)
|
||||||
{
|
{
|
||||||
unsigned int r = next();
|
unsigned int r = next();
|
||||||
return static_cast<int>(r % (upper - lower + 1)) + lower;
|
return static_cast<int>(r % ((unsigned int)(upper) - (unsigned int)(lower) + 1U)) + lower;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RNG::chance(int nominator, unsigned int denominator)
|
bool RNG::chance(int nominator, unsigned int denominator)
|
||||||
|
@ -200,7 +200,7 @@ static int mathRandom(lua_State *l)
|
|||||||
{
|
{
|
||||||
luaL_error(l, "interval is empty");
|
luaL_error(l, "interval is empty");
|
||||||
}
|
}
|
||||||
if (upper - lower + 1)
|
if ((unsigned int)(upper) - (unsigned int)(lower) + 1U)
|
||||||
{
|
{
|
||||||
lua_pushinteger(l, rng.between(lower, upper));
|
lua_pushinteger(l, rng.between(lower, upper));
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user