From c12b81a2734dc661d68e384c7188349eb1ef58a9 Mon Sep 17 00:00:00 2001 From: Zack Middleton Date: Wed, 7 Jun 2017 21:44:59 -0500 Subject: [PATCH] Fix strncpy usage in botlib All usage of strncpy in botlib should now either set string terminator or use Q_strncpyz. --- code/botlib/be_aas_main.c | 2 +- code/botlib/be_ai_chat.c | 8 ++++---- code/botlib/be_ai_goal.c | 7 +++---- code/botlib/be_ai_weap.c | 2 +- code/botlib/l_log.c | 2 +- code/botlib/l_precomp.c | 2 +- code/botlib/l_script.c | 2 +- code/botlib/l_struct.c | 2 +- 8 files changed, 13 insertions(+), 14 deletions(-) diff --git a/code/botlib/be_aas_main.c b/code/botlib/be_aas_main.c index 08f82135..f4ef606e 100644 --- a/code/botlib/be_aas_main.c +++ b/code/botlib/be_aas_main.c @@ -238,7 +238,7 @@ int AAS_LoadFiles(const char *mapname) return errnum; botimport.Print(PRT_MESSAGE, "loaded %s\n", aasfile); - strncpy(aasworld.filename, aasfile, MAX_PATH); + Q_strncpyz(aasworld.filename, aasfile, sizeof(aasworld.filename)); return BLERR_NOERROR; } //end of the function AAS_LoadFiles //=========================================================================== diff --git a/code/botlib/be_ai_chat.c b/code/botlib/be_ai_chat.c index e90aaddc..2413706b 100644 --- a/code/botlib/be_ai_chat.c +++ b/code/botlib/be_ai_chat.c @@ -342,7 +342,7 @@ void BotQueueConsoleMessage(int chatstate, int type, char *message) m->handle = cs->handle; m->time = AAS_Time(); m->type = type; - strncpy(m->message, message, MAX_MESSAGE_SIZE); + Q_strncpyz(m->message, message, MAX_MESSAGE_SIZE); m->next = NULL; if (cs->lastmessage) { @@ -1456,7 +1456,7 @@ int BotFindMatch(char *str, bot_match_t *match, unsigned long int context) int i; bot_matchtemplate_t *ms; - strncpy(match->string, str, MAX_MESSAGE_SIZE); + Q_strncpyz(match->string, str, MAX_MESSAGE_SIZE); //remove any trailing enters while(strlen(match->string) && match->string[strlen(match->string)-1] == '\n') @@ -2114,7 +2114,7 @@ bot_chat_t *BotLoadInitialChat(char *chatfile, char *chatname) if (pass && ptr) { chattype = (bot_chattype_t *) ptr; - strncpy(chattype->name, token.string, MAX_CHATTYPE_NAME); + Q_strncpyz(chattype->name, token.string, MAX_CHATTYPE_NAME); chattype->firstchatmessage = NULL; //add the chat type to the chat chattype->next = chat->types; @@ -2884,7 +2884,7 @@ void BotSetChatName(int chatstate, char *name, int client) if (!cs) return; cs->client = client; Com_Memset(cs->name, 0, sizeof(cs->name)); - strncpy(cs->name, name, sizeof(cs->name)); + strncpy(cs->name, name, sizeof(cs->name)-1); cs->name[sizeof(cs->name)-1] = '\0'; } //end of the function BotSetChatName //=========================================================================== diff --git a/code/botlib/be_ai_goal.c b/code/botlib/be_ai_goal.c index 3a5d01ab..7c56b56e 100644 --- a/code/botlib/be_ai_goal.c +++ b/code/botlib/be_ai_goal.c @@ -281,7 +281,7 @@ itemconfig_t *LoadItemConfig(char *filename) LibVarSet( "max_iteminfo", "256" ); } - strncpy( path, filename, MAX_PATH ); + Q_strncpyz(path, filename, sizeof(path)); PC_SetBaseFolder(BOTFILESBASEFOLDER); source = LoadSourceFile( path ); if( !source ) { @@ -314,7 +314,7 @@ itemconfig_t *LoadItemConfig(char *filename) return NULL; } //end if StripDoubleQuotes(token.string); - strncpy(ii->classname, token.string, sizeof(ii->classname)-1); + Q_strncpyz(ii->classname, token.string, sizeof(ii->classname)); if (!ReadStructure(source, &iteminfo_struct, (char *) ii)) { FreeMemory(ic); @@ -685,8 +685,7 @@ void BotGoalName(int number, char *name, int size) { if (li->number == number) { - strncpy(name, itemconfig->iteminfo[li->iteminfo].name, size-1); - name[size-1] = '\0'; + Q_strncpyz(name, itemconfig->iteminfo[li->iteminfo].name, size); return; } //end for } //end for diff --git a/code/botlib/be_ai_weap.c b/code/botlib/be_ai_weap.c index 8fab4d79..88fb197c 100644 --- a/code/botlib/be_ai_weap.c +++ b/code/botlib/be_ai_weap.c @@ -219,7 +219,7 @@ weaponconfig_t *LoadWeaponConfig(char *filename) max_projectileinfo = 32; LibVarSet("max_projectileinfo", "32"); } //end if - strncpy(path, filename, MAX_PATH); + Q_strncpyz(path, filename, sizeof(path)); PC_SetBaseFolder(BOTFILESBASEFOLDER); source = LoadSourceFile(path); if (!source) diff --git a/code/botlib/l_log.c b/code/botlib/l_log.c index ee25604e..ba51e008 100644 --- a/code/botlib/l_log.c +++ b/code/botlib/l_log.c @@ -75,7 +75,7 @@ void Log_Open(char *filename) botimport.Print(PRT_ERROR, "can't open the log file %s\n", filename); return; } //end if - strncpy(logfile.filename, filename, MAX_LOGFILENAMESIZE); + Q_strncpyz(logfile.filename, filename, MAX_LOGFILENAMESIZE); botimport.Print(PRT_MESSAGE, "Opened log %s\n", logfile.filename); } //end of the function Log_Create //=========================================================================== diff --git a/code/botlib/l_precomp.c b/code/botlib/l_precomp.c index 4ffacad9..5c866b2d 100644 --- a/code/botlib/l_precomp.c +++ b/code/botlib/l_precomp.c @@ -1323,7 +1323,7 @@ define_t *PC_DefineFromString(char *string) script = LoadScriptMemory(string, strlen(string), "*extern"); //create a new source Com_Memset(&src, 0, sizeof(source_t)); - strncpy(src.filename, "*extern", sizeof(src.filename) - 1); + Q_strncpyz(src.filename, "*extern", sizeof(src.filename)); src.scriptstack = script; #if DEFINEHASHING src.definehash = GetClearedMemory(DEFINEHASHSIZE * sizeof(define_t *)); diff --git a/code/botlib/l_script.c b/code/botlib/l_script.c index ee9cddc0..23694386 100644 --- a/code/botlib/l_script.c +++ b/code/botlib/l_script.c @@ -804,7 +804,7 @@ int PS_ReadPunctuation(script_t *script, token_t *token) //if the script contains the punctuation if (!strncmp(script->script_p, p, len)) { - strncpy(token->string, p, MAX_TOKEN); + Q_strncpyz(token->string, p, MAX_TOKEN); script->script_p += len; token->type = TT_PUNCTUATION; //sub type is the number of the punctuation diff --git a/code/botlib/l_struct.c b/code/botlib/l_struct.c index 0983b4d4..0d5d6b01 100644 --- a/code/botlib/l_struct.c +++ b/code/botlib/l_struct.c @@ -221,7 +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); + strncpy((char *) p, token.string, MAX_STRINGFIELD-1); //make sure the string is closed with a zero ((char *)p)[MAX_STRINGFIELD-1] = '\0'; //