From b0ebb24eb6b396b0888f00ef4b164237d973b076 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/d_netcmd.c    |  3 ++-
 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 +---
 6 files changed, 15 insertions(+), 6 deletions(-)

diff --git a/src/d_netcmd.c b/src/d_netcmd.c
index d278643bd2..ede51e84ae 100644
--- a/src/d_netcmd.c
+++ b/src/d_netcmd.c
@@ -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;
diff --git a/src/deh_lua.c b/src/deh_lua.c
index ea94c32a9d..4d3be4cbe3 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 61d3d558a8..719420500c 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 e0af9496d4..0e03c006c7 100644
--- a/src/g_game.c
+++ b/src/g_game.c
@@ -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;
diff --git a/src/g_game.h b/src/g_game.h
index fa7936747b..62fc7b50d5 100644
--- a/src/g_game.h
+++ b/src/g_game.h
@@ -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);
diff --git a/src/lua_infolib.c b/src/lua_infolib.c
index 0b4ae4735c..0ea2440d19 100644
--- a/src/lua_infolib.c
+++ b/src/lua_infolib.c
@@ -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)) {
-- 
GitLab