From: Poul Sander Date: Sun, 3 Apr 2022 18:56:44 +0200 Subject: Replace Q_strncpy to be safe even if source and destination overlaps and remove a lot of bad usage of strncpy. Origin: backport, oaxB53-r2, commit:5a9476eeb65ade97999d7b1bf80813cac6569f4e --- code/cgame/cg_servercmds.c | 9 ++--- code/cgame/cg_weapons.c | 2 +- code/game/ai_chat.c | 3 +- code/game/ai_cmd.c | 6 ++-- code/game/ai_dmq3.c | 18 ++++------ code/game/ai_team.c | 3 +- code/game/g_killspree.c | 52 ++++++++++++++-------------- code/game/g_local.h | 6 ++-- code/game/g_vote.c | 84 +++++++++++++++++++++------------------------- code/q3_ui/ui_challenges.c | 2 +- code/qcommon/q_shared.c | 26 ++++++++++++-- code/ui/ui_shared.c | 3 +- 12 files changed, 108 insertions(+), 106 deletions(-) diff --git a/code/cgame/cg_servercmds.c b/code/cgame/cg_servercmds.c index 7295d9a..6d08eb4 100644 --- a/code/cgame/cg_servercmds.c +++ b/code/cgame/cg_servercmds.c @@ -454,21 +454,18 @@ void CG_ShaderStateChanged(void) { while (o && *o) { n = strstr(o, "="); if (n && *n) { - strncpy(originalShader, o, n-o); - originalShader[n-o] = 0; + Q_strncpyz(originalShader, o, n-o+1); n++; t = strstr(n, ":"); if (t && *t) { - strncpy(newShader, n, t-n); - newShader[t-n] = 0; + Q_strncpyz(newShader, n, t-n+1); } else { break; } t++; o = strstr(t, "@"); if (o) { - strncpy(timeOffset, t, o-t); - timeOffset[o-t] = 0; + Q_strncpyz(timeOffset, t, o-t+1); o++; trap_R_RemapShader( originalShader, newShader, timeOffset ); } diff --git a/code/cgame/cg_weapons.c b/code/cgame/cg_weapons.c index a75707e..efb90ab 100644 --- a/code/cgame/cg_weapons.c +++ b/code/cgame/cg_weapons.c @@ -1009,7 +1009,7 @@ void CG_RegisterItemVisuals( int itemNum ) { item = &bg_itemlist[ itemNum ]; - memset( itemInfo, 0, sizeof( &itemInfo ) ); + memset( itemInfo, 0, sizeof( *itemInfo ) ); itemInfo->registered = qtrue; itemInfo->models[0] = trap_R_RegisterModel( item->world_model[0] ); diff --git a/code/game/ai_chat.c b/code/game/ai_chat.c index cf21d5e..922aca9 100644 --- a/code/game/ai_chat.c +++ b/code/game/ai_chat.c @@ -265,8 +265,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 8b4bb87..1b0b961 100644 --- a/code/game/ai_cmd.c +++ b/code/game/ai_cmd.c @@ -1208,8 +1208,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, 32); // trap_BotMatchVariable(match, NETNAME, netname, sizeof(netname)); BotAI_BotInitialChat(bs, "joinedteam", teammate, NULL); @@ -1401,8 +1400,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 f8c4173..4bd394a 100644 --- a/code/game/ai_dmq3.c +++ b/code/game/ai_dmq3.c @@ -1507,10 +1507,9 @@ char *ClientName(int client, char *name, int size) { BotAI_Print(PRT_ERROR, "ClientName: client out of range\n"); 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_CleanStr( name ); + trap_GetConfigstring(CS_PLAYERS + client, buf, sizeof (buf)); + Q_strncpyz(name, Info_ValueForKey(buf, "n"), size); + Q_CleanStr(name); return name; } @@ -1526,9 +1525,8 @@ char *ClientSkin(int client, char *skin, int size) { BotAI_Print(PRT_ERROR, "ClientSkin: client out of range\n"); 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'; + trap_GetConfigstring(CS_PLAYERS + client, buf, sizeof (buf)); + Q_strncpyz(skin, Info_ValueForKey(buf, "model"), size); return skin; } @@ -1636,8 +1634,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; } @@ -3787,8 +3784,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")) { vec3_t mins = {700, 204, 672}, maxs = {964, 468, 680}; diff --git a/code/game/ai_team.c b/code/game/ai_team.c index c1d0394..94fc92b 100644 --- a/code/game/ai_team.c +++ b/code/game/ai_team.c @@ -2138,8 +2138,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/game/g_killspree.c b/code/game/g_killspree.c index 9a13303..32a9cc2 100644 --- a/code/game/g_killspree.c +++ b/code/game/g_killspree.c @@ -223,37 +223,35 @@ static char *fillPlaceHolder( char *stringToSearch, char *placeHolder, char *rep static char output[ MAX_SAY_TEXT ]; char *p; - if( !( p = strstr( stringToSearch, placeHolder ) ) ) - return stringToSearch; - - strncpy( output, stringToSearch, p - stringToSearch ); - output[ p - stringToSearch ] = '\0'; - - Q_snprintf( output + ( p - stringToSearch ), output - stringToSearch, "%s%s", replaceWith, p + strlen( placeHolder ) ); - - return output; + if( !( p = strstr( stringToSearch, placeHolder ) ) ) + return stringToSearch; + + Q_snprintf( output, MAX_SAY_TEXT, "%.*s%s%s", (int)(p - stringToSearch), stringToSearch, replaceWith, p+strlen(placeHolder)); + + return output; } //This concatenate's the message to broadcast to the clients. static char *CreateMessage( gentity_t *ent, char *message, char *spreeNumber ) { - static char output[ MAX_SAY_TEXT ] = { "" }; - - char name[ MAX_NAME_LENGTH ]; - //Do some sanity checks - if( !ent ) { - return output; - } else if ( !*message ) { - return output; - } else if ( !spreeNumber ) { - return output; - } - //Get the player name. - Q_strncpyz( name, ent->client->pers.netname, sizeof( name ) ); - //Do Our Replacements - Q_strncpyz( output, fillPlaceHolder( message, "[n]", name ), sizeof( output ) ); - Q_strncpyz( output, fillPlaceHolder( output, "[k]", spreeNumber ), sizeof( output ) ); - return output; + static char output[ MAX_SAY_TEXT ] = { "" }; + + char name[ MAX_NAME_LENGTH ]; + //Do some sanity checks + if( !ent ) { + return output; + } else if ( !*message ) { + return output; + } else if ( !spreeNumber ) { + return output; + } + //Get the player name. + Q_strncpyz( name, ent->client->pers.netname, sizeof( name ) ); + //Do Our Replacements + Q_strncpyz( output, fillPlaceHolder( message, "[n]", name ), sizeof( output ) ); + //The following line has a risk of copying output into output. Q_strncpyz can handle the case. + Q_strncpyz( output, fillPlaceHolder( output, "[k]", spreeNumber ), sizeof( output ) ); + return output; } @@ -514,4 +512,4 @@ void G_checkForMultiKill( gentity_t *ent ) { } - \ No newline at end of file + diff --git a/code/game/g_local.h b/code/game/g_local.h index 4b82323..05643ca 100644 --- a/code/game/g_local.h +++ b/code/game/g_local.h @@ -923,7 +923,7 @@ void PlayerStore_restore(char* guid, playerState_t *ps); // // g_vote.c // -int allowedVote(char *commandStr); +int allowedVote(const char *commandStr); void CheckVote( void ); void CountVotes( void ); void ClientLeaving(int clientNumber); @@ -950,8 +950,8 @@ typedef struct { extern char custom_vote_info[1024]; extern t_mappage getMappage(int page); -extern int allowedMap(char *mapname); -extern int allowedGametype(char *gametypeStr); +extern int allowedMap(const char *mapname); +extern int allowedGametype(const char *gametypeStr); extern int allowedTimelimit(int limit); extern int allowedFraglimit(int limit); extern int VoteParseCustomVotes( void ); diff --git a/code/game/g_vote.c b/code/game/g_vote.c index 3695674..7e0d37a 100644 --- a/code/game/g_vote.c +++ b/code/game/g_vote.c @@ -29,28 +29,25 @@ allowedVote ================== */ #define MAX_VOTENAME_LENGTH 14 //currently the longest string is "/map_restart/\0" (14 chars) -int allowedVote(char *commandStr) { - char tempStr[MAX_VOTENAME_LENGTH]; - int length; - char voteNames[MAX_CVAR_VALUE_STRING]; - trap_Cvar_VariableStringBuffer( "g_voteNames", voteNames, sizeof( voteNames ) ); - if(!Q_stricmp(voteNames, "*" )) - return qtrue; //if star, everything is allowed - length = strlen(commandStr); - if(length>MAX_VOTENAME_LENGTH-3) - { - //Error: too long - return qfalse; - } - //Now constructing a string that starts and ends with '/' like: "/clientkick/" - tempStr[0] = '/'; - strncpy(&tempStr[1],commandStr,length); - tempStr[length+1] = '/'; - tempStr[length+2] = '\0'; - if(Q_stristr(voteNames,tempStr) != NULL) - return qtrue; - else - return qfalse; +int allowedVote(const char *commandStr) { + char tempStr[MAX_VOTENAME_LENGTH]; + int length; + char voteNames[MAX_CVAR_VALUE_STRING]; + trap_Cvar_VariableStringBuffer( "g_voteNames", voteNames, sizeof( voteNames ) ); + if(Q_strequal(voteNames, "*" )) + return qtrue; //if star, everything is allowed + length = strlen(commandStr); + if(length>MAX_VOTENAME_LENGTH-3) + { + //Error: too long + return qfalse; + } + //Now constructing a string that starts and ends with '/' like: "/clientkick/" + Q_snprintf(tempStr, sizeof(tempStr), "/%s/", commandStr); + if(Q_stristr(voteNames,tempStr) != NULL) + return qtrue; + else + return qfalse; } /* @@ -126,7 +123,7 @@ allowedMap ================== */ -int allowedMap(char *mapname) { +int allowedMap(const char *mapname) { int length; fileHandle_t file; //To check that the map actually exists. char buffer[MAX_MAPS_TEXT]; @@ -173,28 +170,25 @@ allowedGametype ================== */ #define MAX_GAMETYPENAME_LENGTH 5 //currently the longest string is "/12/\0" (5 chars) -int allowedGametype(char *gametypeStr) { - char tempStr[MAX_GAMETYPENAME_LENGTH]; - int length; - char voteGametypes[MAX_CVAR_VALUE_STRING]; - trap_Cvar_VariableStringBuffer( "g_voteGametypes", voteGametypes, sizeof( voteGametypes ) ); - if(!Q_stricmp(voteGametypes, "*" )) - return qtrue; //if star, everything is allowed - length = strlen(gametypeStr); - if(length>MAX_GAMETYPENAME_LENGTH-3) - { - //Error: too long - return qfalse; - } - tempStr[0] = '/'; - strncpy(&tempStr[1],gametypeStr,length); - tempStr[length+1] = '/'; - tempStr[length+2] = '\0'; - if(Q_stristr(voteGametypes,tempStr) != NULL) - return qtrue; - else { - return qfalse; - } +int allowedGametype(const char *gametypeStr) { + char tempStr[MAX_GAMETYPENAME_LENGTH]; + int length; + char voteGametypes[MAX_CVAR_VALUE_STRING]; + trap_Cvar_VariableStringBuffer( "g_voteGametypes", voteGametypes, sizeof( voteGametypes ) ); + if(Q_strequal(voteGametypes, "*" )) + return qtrue; //if star, everything is allowed + length = strlen(gametypeStr); + if(length>MAX_GAMETYPENAME_LENGTH-3) + { + //Error: too long + return qfalse; + } + Q_snprintf(tempStr, sizeof(tempStr),"/%s/", gametypeStr); + if(Q_stristr(voteGametypes,tempStr) != NULL) + return qtrue; + else { + return qfalse; + } } /* diff --git a/code/q3_ui/ui_challenges.c b/code/q3_ui/ui_challenges.c index c2ed62c..3033c97 100644 --- a/code/q3_ui/ui_challenges.c +++ b/code/q3_ui/ui_challenges.c @@ -373,7 +373,7 @@ static void UI_Challenges_Init( void ) { challenges.entryIntText[i].style = UI_RIGHT; challenges.entryIntText[i].string = challenges.entryIntString[i]; - strncpy(challenges.entryIntString[i],va("%u",challenges.entryInt[i]),MAX_INT_AS_STRING); + Q_snprintf(challenges.entryIntString[i], MAX_INT_AS_STRING, "%u", challenges.entryInt[i]); y += BIGCHAR_HEIGHT+2; } diff --git a/code/qcommon/q_shared.c b/code/qcommon/q_shared.c index d1ead36..2832ea3 100644 --- a/code/qcommon/q_shared.c +++ b/code/qcommon/q_shared.c @@ -727,11 +727,31 @@ char* Q_strrchr( const char* string, int c ) return sp; } +/* + Copied from code/game/bg_lib.c + It is here to avoid undifined behavior when source and destination overlap. + This is used quite a lot in the code. + */ +static char *qstrncpy(char *strDest, const char *strSource, size_t count) { + char *s; + + s = strDest; + while (*strSource && count) { + *s++ = *strSource++; + count--; + } + while (count--) { + *s++ = 0; + } + return strDest; +} + /* ============= Q_strncpyz Safe strncpy that ensures a trailing zero +Does not result in undefiend behavior if source=destination ============= */ void Q_strncpyz( char *dest, const char *src, int destsize ) { @@ -745,8 +765,10 @@ void Q_strncpyz( char *dest, const char *src, int destsize ) { Com_Error(ERR_FATAL,"Q_strncpyz: destsize < 1" ); } - strncpy( dest, src, destsize-1 ); - dest[destsize-1] = 0; + if (dest != src) { + qstrncpy(dest, src, destsize - 1); + } + dest[destsize - 1] = 0; } int Q_stricmpn (const char *s1, const char *s2, int n) { diff --git a/code/ui/ui_shared.c b/code/ui/ui_shared.c index d63a796..08eab51 100644 --- a/code/ui/ui_shared.c +++ b/code/ui/ui_shared.c @@ -2936,8 +2936,7 @@ void Item_Text_Wrapped_Paint(itemDef_t *item) { start = textPtr; p = strchr(textPtr, '\r'); while (p && *p) { - strncpy(buff, start, p-start+1); - buff[p-start] = '\0'; + Q_strncpyz(buff, start, p - start + 1); DC->drawText(x, y, item->textscale, color, buff, 0, 0, item->textStyle); y += height + 5; start += p - start + 1;