Protect new math.random against division by 0

This commit is contained in:
Tamás Bálint Misius 2023-04-15 23:20:25 +02:00
parent 34e4d90dac
commit 54d08f3496
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2

View File

@ -175,13 +175,29 @@ static int mathRandom(lua_State *l)
{
// only thing that matters is that the rng not be luacon_sim->rng when !inSimEvent
auto &rng = inSimEvent ? luacon_sim->rng : interfaceRng;
int lower, upper;
switch (lua_gettop(l))
{
case 0: lua_pushnumber (l, rng.uniform01()); break;
case 1: lua_pushinteger(l, rng.between( 1, luaL_checkinteger(l, 1))); break;
default: lua_pushinteger(l, rng.between(luaL_checkinteger(l, 1), luaL_checkinteger(l, 2))); break;
}
case 0:
lua_pushnumber(l, rng.uniform01());
return 1;
case 1:
lower = 1;
upper = luaL_checkinteger(l, 1);
break;
default:
lower = luaL_checkinteger(l, 1);
upper = luaL_checkinteger(l, 2);
break;
}
if (upper < lower)
{
luaL_error(l, "interval is empty");
}
lua_pushinteger(l, rng.between(lower, upper));
return 1;
}
static int mathRandomseed(lua_State *l)