Ensure teamscores[TEAM_NONE] can't be written to or read

Push nil if a player start is NULL
Make lib_teamstartslen push numteams - 1 instead of MAXTEAMS
This commit is contained in:
Lactozilla 2023-08-13 22:01:34 -03:00
parent 82f5c42e94
commit 626aee965c

View file

@ -2431,27 +2431,27 @@ static int teamlist_len(lua_State *L)
static int teamscores_get(lua_State *L)
{
UINT32 *scoreslist = *((UINT32 **)luaL_checkudata(L, 1, META_TEAMSCORES));
int i = luaL_checkinteger(L, 2);
if (i < 0 || i >= numteams)
return luaL_error(L, "array index %d out of range (0 - %d)", i, max(0, (int)numteams - 1));
lua_pushinteger(L, scoreslist[i]);
int team = luaL_checkinteger(L, 2);
if (team <= 0 || team >= numteams)
return luaL_error(L, "team index %d out of range (1 - %d)", team, max(0, (int)numteams - 1));
lua_pushinteger(L, scoreslist[team]);
return 1;
}
static int teamscores_set(lua_State *L)
{
UINT32 *scoreslist = *((UINT32 **)luaL_checkudata(L, 1, META_TEAMSCORES));
int i = luaL_checkinteger(L, 2);
int team = luaL_checkinteger(L, 2);
UINT32 score = (UINT32)luaL_checkinteger(L, 3);
if (i < 0 || i >= numteams)
return luaL_error(L, "array index %d out of range (0 - %d)", i, max(0, (int)numteams - 1));
scoreslist[i] = score;
if (team <= 0 || team >= numteams)
return luaL_error(L, "array index %d out of range (1 - %d)", team, max(0, (int)numteams - 1));
scoreslist[team] = score;
return 0;
}
static int teamscores_len(lua_State *L)
{
lua_pushinteger(L, numteams);
lua_pushinteger(L, max(0, numteams - 1));
return 1;
}
@ -2462,23 +2462,26 @@ static int teamscores_len(lua_State *L)
static int playerstarts_get(lua_State *L)
{
playerstarts_t *starts = *((playerstarts_t **)luaL_checkudata(L, 1, META_PLAYERSTARTS));
int i = luaL_checkinteger(L, 2);
if (i < 0 || i >= (signed)starts->count)
return luaL_error(L, "player start index %d out of range (0 - %d)", i, max(0, (int)starts->count - 1));
LUA_PushUserdata(L, starts->list[i], META_MAPTHING);
int index = luaL_checkinteger(L, 2);
if (index < 0 || index >= (signed)starts->count)
return luaL_error(L, "player start index %d out of range (0 - %d)", index, max(0, (int)starts->count - 1));
if (starts->list[index])
LUA_PushUserdata(L, starts->list[index], META_MAPTHING);
else
lua_pushnil(L);
return 1;
}
static int playerstarts_set(lua_State *L)
{
playerstarts_t *starts = *((playerstarts_t **)luaL_checkudata(L, 1, META_PLAYERSTARTS));
int i = luaL_checkinteger(L, 2);
int index = luaL_checkinteger(L, 2);
mapthing_t *mthing = *((mapthing_t **)luaL_checkudata(L, 3, META_MAPTHING));
if (i < 0 || i >= (signed)starts->count)
return luaL_error(L, "player start index %d out of range (0 - %d)", i, max(0, (int)starts->count - 1));
if (index < 0 || index >= (signed)starts->count)
return luaL_error(L, "player start index %d out of range (0 - %d)", index, max(0, (int)starts->count - 1));
if (!mthing)
return LUA_ErrInvalid(L, "mapthing_t");
starts->list[i] = mthing;
starts->list[index] = mthing;
return 0;
}
@ -2491,22 +2494,19 @@ static int playerstarts_len(lua_State *L)
static int lib_getTeamstarts(lua_State *L)
{
int i;
lua_remove(L, 1);
i = luaL_checkinteger(L, 1);
int i = luaL_checkinteger(L, 1);
if (i <= 0 || i >= numteams)
return luaL_error(L, "team index %d out of range (1 - %d)", i, numteams - 1);
LUA_PushUserdata(L, &teamstarts[i], META_PLAYERSTARTS);
return 1;
}
// #teamstarts -> MAXTEAMS
static int lib_teamstartslen(lua_State *L)
{
lua_pushinteger(L, MAXTEAMS);
lua_pushinteger(L, max(0, numteams - 1));
return 1;
}