From e8014f8ac26c338cfd9c400e5a0ca2dbd3441594 Mon Sep 17 00:00:00 2001 From: Lactozilla <jp6781615@gmail.com> Date: Sun, 6 Aug 2023 18:09:14 -0300 Subject: [PATCH] Fix a possible crash --- src/deh_lua.c | 1 + src/deh_soc.c | 1 + src/g_game.c | 11 +++++++++-- src/g_game.h | 1 + src/lua_infolib.c | 4 +--- src/netcode/d_netcmd.c | 3 ++- 6 files changed, 15 insertions(+), 6 deletions(-) diff --git a/src/deh_lua.c b/src/deh_lua.c index a08477b26d..9696426d25 100644 --- a/src/deh_lua.c +++ b/src/deh_lua.c @@ -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; diff --git a/src/deh_soc.c b/src/deh_soc.c index ad47c535dd..509d56959d 100644 --- a/src/deh_soc.c +++ b/src/deh_soc.c @@ -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++; } } diff --git a/src/g_game.c b/src/g_game.c index 009bc0e784..aa0007d96c 100644 --- a/src/g_game.c +++ b/src/g_game.c @@ -3917,6 +3917,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) @@ -3962,7 +3969,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; @@ -3970,7 +3977,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; diff --git a/src/g_game.h b/src/g_game.h index 1d6ba0cb47..d2021bab58 100644 --- a/src/g_game.h +++ b/src/g_game.h @@ -223,6 +223,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); diff --git a/src/lua_infolib.c b/src/lua_infolib.c index 6bbfb626e7..46ad64672f 100644 --- a/src/lua_infolib.c +++ b/src/lua_infolib.c @@ -2244,9 +2244,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)) { diff --git a/src/netcode/d_netcmd.c b/src/netcode/d_netcmd.c index 0658387752..8ae2d8b373 100644 --- a/src/netcode/d_netcmd.c +++ b/src/netcode/d_netcmd.c @@ -2271,7 +2271,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; -- GitLab