From 1132abe3dc58ba90a15822188ab3ec1c04f06d45 Mon Sep 17 00:00:00 2001 From: Zack Middleton Date: Wed, 15 May 2024 19:35:37 -0500 Subject: [PATCH] Replace strncpy usage with Q_strncpyz --- code/botlib/be_aas_bspq3.c | 3 +-- code/botlib/be_ai_char.c | 3 +-- code/botlib/be_ai_chat.c | 10 +++------- code/botlib/be_interface.c | 3 +-- code/botlib/l_struct.c | 4 +--- code/game/ai_chat.c | 3 +-- code/game/ai_cmd.c | 6 ++---- code/game/ai_dmq3.c | 12 ++++-------- code/game/ai_team.c | 3 +-- code/server/sv_rankings.c | 3 +-- 10 files changed, 16 insertions(+), 34 deletions(-) diff --git a/code/botlib/be_aas_bspq3.c b/code/botlib/be_aas_bspq3.c index 34862bcb..c6a8b1ef 100644 --- a/code/botlib/be_aas_bspq3.c +++ b/code/botlib/be_aas_bspq3.c @@ -286,8 +286,7 @@ int AAS_ValueForBSPEpairKey(int ent, char *key, char *value, int size) { if (!strcmp(epair->key, key)) { - strncpy(value, epair->value, size-1); - value[size-1] = '\0'; + Q_strncpyz(value, epair->value, size); return qtrue; } //end if } //end for diff --git a/code/botlib/be_ai_char.c b/code/botlib/be_ai_char.c index c2ee7a48..b839cd6b 100644 --- a/code/botlib/be_ai_char.c +++ b/code/botlib/be_ai_char.c @@ -758,8 +758,7 @@ void Characteristic_String(int character, int index, char *buf, int size) //an integer will be converted to a float if (ch->c[index].type == CT_STRING) { - strncpy(buf, ch->c[index].value.string, size-1); - buf[size-1] = '\0'; + Q_strncpyz(buf, ch->c[index].value.string, size); } //end if else { diff --git a/code/botlib/be_ai_chat.c b/code/botlib/be_ai_chat.c index f527da78..591ca378 100644 --- a/code/botlib/be_ai_chat.c +++ b/code/botlib/be_ai_chat.c @@ -1499,8 +1499,7 @@ void BotMatchVariable(bot_match_t *match, int variable, char *buf, int size) if (match->variables[variable].length < size) size = match->variables[variable].length+1; assert( match->variables[variable].offset >= 0 ); - strncpy(buf, &match->string[ (int) match->variables[variable].offset], size-1); - buf[size-1] = '\0'; + Q_strncpyz(buf, &match->string[ (int) match->variables[variable].offset], size); } //end if else { @@ -2846,8 +2845,7 @@ void BotGetChatMessage(int chatstate, char *buf, int size) if (!cs) return; BotRemoveTildes(cs->chatmessage); - strncpy(buf, cs->chatmessage, size-1); - buf[size-1] = '\0'; + Q_strncpyz(buf, cs->chatmessage, size); //clear the chat message from the state strcpy(cs->chatmessage, ""); } //end of the function BotGetChatMessage @@ -2883,9 +2881,7 @@ void BotSetChatName(int chatstate, char *name, int client) cs = BotChatStateFromHandle(chatstate); if (!cs) return; cs->client = client; - Com_Memset(cs->name, 0, sizeof(cs->name)); - strncpy(cs->name, name, sizeof(cs->name)-1); - cs->name[sizeof(cs->name)-1] = '\0'; + Q_strncpyz(cs->name, name, sizeof(cs->name)); } //end of the function BotSetChatName //=========================================================================== // diff --git a/code/botlib/be_interface.c b/code/botlib/be_interface.c index eb0efb95..ffff7628 100644 --- a/code/botlib/be_interface.c +++ b/code/botlib/be_interface.c @@ -235,8 +235,7 @@ int Export_BotLibVarGet(const char *var_name, char *value, int size) char *varvalue; varvalue = LibVarGetString(var_name); - strncpy(value, varvalue, size-1); - value[size-1] = '\0'; + Q_strncpyz(value, varvalue, size); return BLERR_NOERROR; } //end of the function Export_BotLibVarGet //=========================================================================== diff --git a/code/botlib/l_struct.c b/code/botlib/l_struct.c index 0d5d6b01..14dc1169 100644 --- a/code/botlib/l_struct.c +++ b/code/botlib/l_struct.c @@ -221,9 +221,7 @@ int ReadString(source_t *source, fielddef_t *fd, void *p) //remove the double quotes StripDoubleQuotes(token.string); //copy the string - strncpy((char *) p, token.string, MAX_STRINGFIELD-1); - //make sure the string is closed with a zero - ((char *)p)[MAX_STRINGFIELD-1] = '\0'; + Q_strncpyz((char *) p, token.string, MAX_STRINGFIELD); // return 1; } //end of the function ReadString diff --git a/code/game/ai_chat.c b/code/game/ai_chat.c index a65875ff..0e50a630 100644 --- a/code/game/ai_chat.c +++ b/code/game/ai_chat.c @@ -237,8 +237,7 @@ char *BotMapTitle(void) { trap_GetServerinfo(info, sizeof(info)); - strncpy(mapname, Info_ValueForKey( info, "mapname" ), sizeof(mapname)-1); - mapname[sizeof(mapname)-1] = '\0'; + Q_strncpyz(mapname, Info_ValueForKey( info, "mapname" ), sizeof(mapname)); return mapname; } diff --git a/code/game/ai_cmd.c b/code/game/ai_cmd.c index 2944fb06..cb69174e 100644 --- a/code/game/ai_cmd.c +++ b/code/game/ai_cmd.c @@ -1105,8 +1105,7 @@ void BotMatch_JoinSubteam(bot_state_t *bs, bot_match_t *match) { //get the sub team name trap_BotMatchVariable(match, TEAMNAME, teammate, sizeof(teammate)); //set the sub team name - strncpy(bs->subteam, teammate, 32); - bs->subteam[31] = '\0'; + Q_strncpyz(bs->subteam, teammate, sizeof(bs->subteam)); // trap_BotMatchVariable(match, NETNAME, netname, sizeof(netname)); BotAI_BotInitialChat(bs, "joinedteam", teammate, NULL); @@ -1297,8 +1296,7 @@ void BotMatch_StartTeamLeaderShip(bot_state_t *bs, bot_match_t *match) { if (match->subtype & ST_I) { //get the team mate that will be the team leader trap_BotMatchVariable(match, NETNAME, teammate, sizeof(teammate)); - strncpy(bs->teamleader, teammate, sizeof(bs->teamleader)); - bs->teamleader[sizeof(bs->teamleader)-1] = '\0'; + Q_strncpyz(bs->teamleader, teammate, sizeof(bs->teamleader)); } //chats for someone else else { diff --git a/code/game/ai_dmq3.c b/code/game/ai_dmq3.c index 3ecaccb7..eb9b5850 100644 --- a/code/game/ai_dmq3.c +++ b/code/game/ai_dmq3.c @@ -1397,8 +1397,7 @@ char *ClientName(int client, char *name, int size) { return "[client out of range]"; } trap_GetConfigstring(CS_PLAYERS+client, buf, sizeof(buf)); - strncpy(name, Info_ValueForKey(buf, "n"), size-1); - name[size-1] = '\0'; + Q_strncpyz(name, Info_ValueForKey(buf, "n"), size); Q_CleanStr( name ); return name; } @@ -1416,8 +1415,7 @@ char *ClientSkin(int client, char *skin, int size) { return "[client out of range]"; } trap_GetConfigstring(CS_PLAYERS+client, buf, sizeof(buf)); - strncpy(skin, Info_ValueForKey(buf, "model"), size-1); - skin[size-1] = '\0'; + Q_strncpyz(skin, Info_ValueForKey(buf, "model"), size); return skin; } @@ -1520,8 +1518,7 @@ char *EasyClientName(int client, char *buf, int size) { memmove(ptr, ptr+1, strlen(ptr + 1)+1); } } - strncpy(buf, name, size-1); - buf[size-1] = '\0'; + Q_strncpyz(buf, name, size); return buf; } @@ -3692,8 +3689,7 @@ void BotMapScripts(bot_state_t *bs) { trap_GetServerinfo(info, sizeof(info)); - strncpy(mapname, Info_ValueForKey( info, "mapname" ), sizeof(mapname)-1); - mapname[sizeof(mapname)-1] = '\0'; + Q_strncpyz(mapname, Info_ValueForKey( info, "mapname" ), sizeof(mapname)); if (!Q_stricmp(mapname, "q3tourney6") || !Q_stricmp(mapname, "q3tourney6_ctf") || !Q_stricmp(mapname, "mpq3tourney6")) { vec3_t mins = {694, 200, 480}, maxs = {968, 472, 680}; diff --git a/code/game/ai_team.c b/code/game/ai_team.c index 78e25b0c..fba03f22 100644 --- a/code/game/ai_team.c +++ b/code/game/ai_team.c @@ -1956,8 +1956,7 @@ void BotTeamAI(bot_state_t *bs) { trap_BotEnterChat(bs->cs, 0, CHAT_TEAM); BotSayVoiceTeamOrder(bs, -1, VOICECHAT_STARTLEADER); ClientName(bs->client, netname, sizeof(netname)); - strncpy(bs->teamleader, netname, sizeof(bs->teamleader)); - bs->teamleader[sizeof(bs->teamleader)-1] = '\0'; + Q_strncpyz(bs->teamleader, netname, sizeof(bs->teamleader)); bs->becometeamleader_time = 0; } return; diff --git a/code/server/sv_rankings.c b/code/server/sv_rankings.c index 9eb2e9b2..63a38870 100644 --- a/code/server/sv_rankings.c +++ b/code/server/sv_rankings.c @@ -138,8 +138,7 @@ void SV_RankBegin( char *gamekey ) // initialize rankings GRankLogLevel( GRLOG_OFF ); - memset(SV_RankGameKey,0,sizeof(SV_RankGameKey)); - strncpy(SV_RankGameKey,gamekey,sizeof(SV_RankGameKey)-1); + Q_strncpyz(SV_RankGameKey,gamekey,sizeof(SV_RankGameKey)); init = GRankInit( 1, SV_RankGameKey, GR_OPT_POLL, GR_OPT_END ); s_server_context = init.context; s_rankings_contexts++;