Fix some signed integer UB in RNG and related code

This commit is contained in:
Tamás Bálint Misius 2023-07-20 21:38:19 +02:00
parent 26a17c4e1f
commit a5e179e530
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2
2 changed files with 2 additions and 2 deletions

View File

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

View File

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