Fix a possible crash

This commit is contained in:
Lactozilla 2023-08-06 18:09:14 -03:00
parent 0450d2d914
commit b0ebb24eb6
6 changed files with 15 additions and 6 deletions

View file

@ -2342,7 +2342,8 @@ static UINT8 GetTeamByName(const char *name)
{
for (UINT8 i = 1; i < teamsingame; i++)
{
if (!stricmp(name, G_GetTeamName(G_GetTeam(i))))
const char *team_name = teams[G_GetTeam(i)].name;
if (team_name && !stricmp(name, team_name))
return i;
}
return MAXTEAMS;

View file

@ -140,6 +140,7 @@ static inline int lib_freeslot(lua_State *L)
teamnames[i] = Z_Malloc(strlen(word)+1, PU_STATIC, NULL);
strcpy(teamnames[i],word);
lua_pushinteger(L, i);
G_InitTeam(i);
numteams++;
r++;
break;

View file

@ -479,6 +479,7 @@ void readfreeslots(MYFILE *f)
{
teamnames[numteams] = Z_Malloc(strlen(word)+1, PU_STATIC, NULL);
strcpy(teamnames[numteams],word);
G_InitTeam(numteams);
numteams++;
}
}

View file

@ -3923,6 +3923,13 @@ UINT32 G_TOLFlag(INT32 pgametype)
return gametypes[pgametype].typeoflevel;
}
void G_InitTeam(UINT8 team)
{
G_FreeTeamData(team);
memset(&teams[team], 0, sizeof(team_t));
}
UINT8 G_GetGametypeTeam(UINT8 gtype, UINT8 team)
{
if (team == TEAM_NONE || team >= gametypes[gtype].teams.num + 1)
@ -3968,7 +3975,7 @@ UINT8 G_GetTeamListFromTeamFlags(UINT8 *teamlist, UINT32 flags)
const char *G_GetTeamName(UINT8 team)
{
if (team >= numteams)
if (team >= numteams || !teams[team].name)
return "Unknown";
return teams[team].name;
@ -3976,7 +3983,7 @@ const char *G_GetTeamName(UINT8 team)
const char *G_GetTeamFlagName(UINT8 team)
{
if (team >= numteams)
if (team >= numteams || !teams[team].flag_name)
return "";
return teams[team].flag_name;

View file

@ -222,6 +222,7 @@ void G_UseContinue(void);
void G_AfterIntermission(void);
void G_EndGame(void); // moved from y_inter.c/h and renamed
void G_InitTeam(UINT8 team);
UINT8 G_GetGametypeTeam(UINT8 gtype, UINT8 team);
UINT8 G_GetTeam(UINT8 team);
UINT8 G_GetTeamFromTeamFlag(UINT32 flag);

View file

@ -2241,9 +2241,7 @@ static int lib_setTeams(lua_State *L)
if (hook_cmd_running)
return luaL_error(L, "Do not alter team data in CMD building code!");
G_FreeTeamData(teamnum);
memset(team, 0, sizeof(team_t));
G_InitTeam(teamnum);
lua_pushnil(L);
while (lua_next(L, 1)) {