foxygit / doom Log in
commit 2e6e43c4a706e3670f131c7b2d5a5525f9bf0d7b
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Sat Mar 29 21:25:55 2014 -0400
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Sat Mar 29 21:25:55 2014 -0400

    heretic: Eliminate use of unsafe string functions.

    Eliminate use of strcpy, strcat, strncpy, and use the new safe
    alternatives.
---
 src/hexen/ct_chat.c | 10 +++++++---
 src/hexen/d_net.c   |  3 ++-
 src/hexen/g_game.c  | 12 +++++++-----
 src/hexen/h2_main.c |  7 +++----
 src/hexen/mn_menu.c |  7 ++++---
 src/hexen/p_acs.c   |  7 ++++---
 src/hexen/p_inter.c | 22 ++++------------------
 src/hexen/p_setup.c | 17 ++++++++++-------
 src/hexen/r_data.c  |  3 +--
 src/hexen/s_sound.c |  8 +++++---
 src/hexen/sc_man.c  |  2 +-
 src/hexen/sv_save.c |  2 +-
 12 files changed, 49 insertions(+), 51 deletions(-)

diff --git a/src/hexen/ct_chat.c b/src/hexen/ct_chat.c
index 6ef39e44..815c0948 100644
--- a/src/hexen/ct_chat.c
+++ b/src/hexen/ct_chat.c
@@ -29,6 +29,7 @@
 #include "s_sound.h"
 #include "doomkeys.h"
 #include "m_controls.h"
+#include "m_misc.h"
 #include "p_local.h"
 #include "v_video.h"

@@ -326,12 +327,15 @@ void CT_Ticker(void)
                 CT_AddChar(i, 0);       // set the end of message character
                 if (numplayers > 2)
                 {
-                    strcpy(plr_lastmsg[i], CT_FromPlrText[i]);
-                    strcat(plr_lastmsg[i], chat_msg[i]);
+                    M_StringCopy(plr_lastmsg[i], CT_FromPlrText[i],
+                                 sizeof(plr_lastmsg[i]));
+                    M_StringConcat(plr_lastmsg[i], chat_msg[i],
+                                   sizeof(plr_lastmsg[i]));
                 }
                 else
                 {
-                    strcpy(plr_lastmsg[i], chat_msg[i]);
+                    M_StringCopy(plr_lastmsg[i], chat_msg[i],
+                                 sizeof(plr_lastmsg[i]));
                 }
                 if (i != consoleplayer && (chat_dest[i] == consoleplayer + 1
                                            || chat_dest[i] == CT_PLR_ALL)
diff --git a/src/hexen/d_net.c b/src/hexen/d_net.c
index 5c64f7ed..19a43584 100644
--- a/src/hexen/d_net.c
+++ b/src/hexen/d_net.c
@@ -35,6 +35,7 @@
 #include "i_video.h"
 #include "i_videohr.h"
 #include "h2def.h"
+#include "m_misc.h"
 #include "p_local.h"
 #include "s_sound.h"
 #include "w_checksum.h"
@@ -61,7 +62,7 @@ static void PlayerQuitGame(player_t *player)

     player_num = player - players;

-    strcpy(exitmsg, "PLAYER 1 LEFT THE GAME");
+    M_StringCopy(exitmsg, "PLAYER 1 LEFT THE GAME", sizeof(exitmsg));
     exitmsg[7] += player_num;
     P_SetMessage(&players[consoleplayer], exitmsg, true);
     S_StartSound(NULL, SFX_CHAT);
diff --git a/src/hexen/g_game.c b/src/hexen/g_game.c
index 17ec04b9..c7c7b531 100644
--- a/src/hexen/g_game.c
+++ b/src/hexen/g_game.c
@@ -1017,11 +1017,13 @@ void G_Ticker(void)
                         {
                             if (netgame)
                             {
-                                strcpy(savedescription, "NET GAME");
+                                M_StringCopy(savedescription, "NET GAME",
+                                             sizeof(savedescription));
                             }
                             else
                             {
-                                strcpy(savedescription, "SAVE GAME");
+                                M_StringCopy(savedescription, "SAVE GAME",
+                                             sizeof(savedescription));
                             }
                         }
                         savegameslot =
@@ -1627,7 +1629,7 @@ void G_DoLoadGame(void)
 void G_SaveGame(int slot, char *description)
 {
     savegameslot = slot;
-    strcpy(savedescription, description);
+    M_StringCopy(savedescription, description, sizeof(savedescription));
     sendsave = true;
 }

@@ -1810,8 +1812,8 @@ void G_RecordDemo(skill_t skill, int numplayers, int episode, int map,

     G_InitNew(skill, episode, map);
     usergame = false;
-    strcpy(demoname, name);
-    strcat(demoname, ".lmp");
+    M_StringCopy(demoname, name, sizeof(demoname));
+    M_StringConcat(demoname, ".lmp", sizeof(demoname));
     demobuffer = demo_p = Z_Malloc(0x20000, PU_STATIC, NULL);
     *demo_p++ = skill;
     *demo_p++ = episode;
diff --git a/src/hexen/h2_main.c b/src/hexen/h2_main.c
index 0afdff24..845496b4 100644
--- a/src/hexen/h2_main.c
+++ b/src/hexen/h2_main.c
@@ -551,12 +551,11 @@ static void HandleArgs(void)
     {
         char file[256];

-        strncpy(file, myargv[p+1], sizeof(file));
-        file[sizeof(file) - 6] = '\0';
+        M_StringCopy(file, myargv[p+1], sizeof(file));

-        if (strcasecmp(file + strlen(file) - 4, ".lmp") != 0)
+        if (!M_StringEndsWith(file, ".lmp"))
         {
-            strcat(file, ".lmp");
+            M_StringConcat(file, ".lmp", sizeof(file));
         }

         W_AddFile(file);
diff --git a/src/hexen/mn_menu.c b/src/hexen/mn_menu.c
index 900a9bdb..0456a52f 100644
--- a/src/hexen/mn_menu.c
+++ b/src/hexen/mn_menu.c
@@ -32,6 +32,7 @@
 #include "i_swap.h"
 #include "i_video.h"
 #include "m_controls.h"
+#include "m_misc.h"
 #include "p_local.h"
 #include "r_local.h"
 #include "s_sound.h"
@@ -929,7 +930,7 @@ static void SCSaveGame(int option)
     if (!FileMenuKeySteal)
     {
         FileMenuKeySteal = true;
-        strcpy(oldSlotText, SlotText[option]);
+        M_StringCopy(oldSlotText, SlotText[option], sizeof(oldSlotText));
         ptr = SlotText[option];
         while (*ptr)
         {
@@ -1635,8 +1636,8 @@ boolean MN_Responder(event_t * event)
         }
         if (key == KEY_ESCAPE)
         {
-            memset(SlotText[currentSlot], 0, SLOTTEXTLEN + 2);
-            strcpy(SlotText[currentSlot], oldSlotText);
+            M_StringCopy(SlotText[currentSlot], oldSlotText,
+                         sizeof(SlotText[currentSlot]));
             SlotStatus[currentSlot]--;
             MN_DeactivateMenu();
             return (true);
diff --git a/src/hexen/p_acs.c b/src/hexen/p_acs.c
index 9ca9f6af..27b07682 100644
--- a/src/hexen/p_acs.c
+++ b/src/hexen/p_acs.c
@@ -26,6 +26,7 @@
 // HEADER FILES ------------------------------------------------------------

 #include "h2def.h"
+#include "m_misc.h"
 #include "m_random.h"
 #include "s_sound.h"
 #include "i_swap.h"
@@ -1682,7 +1683,7 @@ static int CmdEndPrintBold(void)

 static int CmdPrintString(void)
 {
-    strcat(PrintBuffer, ACStrings[Pop()]);
+    M_StringConcat(PrintBuffer, ACStrings[Pop()], sizeof(PrintBuffer));
     return SCRIPT_CONTINUE;
 }

@@ -1690,8 +1691,8 @@ static int CmdPrintNumber(void)
 {
     char tempStr[16];

-    sprintf(tempStr, "%d", Pop());
-    strcat(PrintBuffer, tempStr);
+    snprintf(tempStr, sizeof(tempStr), "%d", Pop());
+    M_StringConcat(PrintBuffer, tempStr, sizeof(PrintBuffer));
     return SCRIPT_CONTINUE;
 }

diff --git a/src/hexen/p_inter.c b/src/hexen/p_inter.c
index 063e3c64..4352b2c8 100644
--- a/src/hexen/p_inter.c
+++ b/src/hexen/p_inter.c
@@ -24,6 +24,7 @@


 #include "h2def.h"
+#include "m_misc.h"
 #include "m_random.h"
 #include "i_system.h"
 #include "p_local.h"
@@ -76,15 +77,8 @@ void P_SetMessage(player_t * player, char *message, boolean ultmsg)
     {
         return;
     }
-    if (strlen(message) > 79)
-    {
-        strncpy(player->message, message, 80);
-        player->message[79] = 0;
-    }
-    else
-    {
-        strcpy(player->message, message);
-    }
+
+    M_StringCopy(player->message, message, sizeof(player->message));
 //    strupr(player->message);
     player->messageTics = MESSAGETICS;
     player->yellowMessage = false;
@@ -110,15 +104,7 @@ void P_SetYellowMessage(player_t * player, char *message, boolean ultmsg)
     {
         return;
     }
-    if (strlen(message) > 79)
-    {
-        strncpy(player->message, message, 80);
-        player->message[79] = 0;
-    }
-    else
-    {
-        strcpy(player->message, message);
-    }
+    M_StringCopy(player->message, message, sizeof(player->message));
     player->messageTics = 5 * MESSAGETICS;      // Bold messages last longer
     player->yellowMessage = true;
     if (ultmsg)
diff --git a/src/hexen/p_setup.c b/src/hexen/p_setup.c
index bf4c5359..8940dd32 100644
--- a/src/hexen/p_setup.c
+++ b/src/hexen/p_setup.c
@@ -31,6 +31,7 @@
 #include "i_system.h"
 #include "m_argv.h"
 #include "m_bbox.h"
+#include "m_misc.h"
 #include "i_swap.h"
 #include "s_sound.h"
 #include "p_local.h"
@@ -819,9 +820,9 @@ static void InitMapInfo(void)
     info->doubleSky = false;
     info->lightning = false;
     info->fadetable = W_GetNumForName(DEFAULT_FADE_TABLE);
-    strcpy(info->name, UNKNOWN_MAP_NAME);
+    M_StringCopy(info->name, UNKNOWN_MAP_NAME, sizeof(info->name));

-//      strcpy(info->songLump, DEFAULT_SONG_LUMP);
+//    M_StringCopy(info->songLump, DEFAULT_SONG_LUMP, sizeof(info->songLump));
     SC_Open(MAPINFO_SCRIPT_NAME);
     while (SC_GetString())
     {
@@ -839,20 +840,20 @@ static void InitMapInfo(void)
         info = &MapInfo[map];

         // Save song lump name
-        strcpy(songMulch, info->songLump);
+        M_StringCopy(songMulch, info->songLump, sizeof(songMulch));

         // Copy defaults to current map definition
         memcpy(info, &MapInfo[0], sizeof(*info));

         // Restore song lump name
-        strcpy(info->songLump, songMulch);
+        M_StringCopy(info->songLump, songMulch, sizeof(info->songLump));

         // The warp translation defaults to the map number
         info->warpTrans = map;

         // Map name must follow the number
         SC_MustGetString();
-        strcpy(info->name, sc_String);
+        M_StringCopy(info->name, sc_String, sizeof(info->name));

         // Process optional tokens
         while (SC_GetString())
@@ -1106,7 +1107,8 @@ void P_PutMapSongLump(int map, char *lumpName)
     {
         return;
     }
-    strcpy(MapInfo[map].songLump, lumpName);
+    M_StringCopy(MapInfo[map].songLump, lumpName,
+                 sizeof(MapInfo[map].songLump));
 }

 //==========================================================================
@@ -1210,7 +1212,8 @@ void InitMapMusicInfo(void)

     for (i = 0; i < 99; i++)
     {
-        strcpy(MapInfo[i].songLump, DEFAULT_SONG_LUMP);
+        M_StringCopy(MapInfo[i].songLump, DEFAULT_SONG_LUMP,
+                     sizeof(MapInfo[i].songLump));
     }
     MapCount = 98;
 }
diff --git a/src/hexen/r_data.c b/src/hexen/r_data.c
index 0565c891..903eb316 100644
--- a/src/hexen/r_data.c
+++ b/src/hexen/r_data.c
@@ -315,14 +315,13 @@ void R_InitTextures(void)
 //
 // load the patch names from pnames.lmp
 //
-    name[8] = 0;
     names = W_CacheLumpName("PNAMES", PU_STATIC);
     nummappatches = LONG(*((int *) names));
     name_p = names + 4;
     patchlookup = Z_Malloc(nummappatches * sizeof(*patchlookup), PU_STATIC, NULL);
     for (i = 0; i < nummappatches; i++)
     {
-        strncpy(name, name_p + i * 8, 8);
+        M_StringCopy(name, name_p + i * 8, 8);
         patchlookup[i] = W_CheckNumForName(name);
     }
     W_ReleaseLumpName("PNAMES");
diff --git a/src/hexen/s_sound.c b/src/hexen/s_sound.c
index 3f1a1e76..71944f11 100644
--- a/src/hexen/s_sound.c
+++ b/src/hexen/s_sound.c
@@ -981,11 +981,13 @@ void S_InitScript(void)
                     SC_MustGetString();
                     if (*sc_String != '?')
                     {
-                        strcpy(S_sfx[i].name, sc_String);
+                        M_StringCopy(S_sfx[i].name, sc_String,
+                                     sizeof(S_sfx[i].name));
                     }
                     else
                     {
-                        strcpy(S_sfx[i].name, "default");
+                        M_StringCopy(S_sfx[i].name, "default",
+                                     sizeof(S_sfx[i].name));
                     }
                     break;
                 }
@@ -1002,7 +1004,7 @@ void S_InitScript(void)
     {
         if (!strcmp(S_sfx[i].name, ""))
         {
-            strcpy(S_sfx[i].name, "default");
+            M_StringCopy(S_sfx[i].name, "default", sizeof(S_sfx[i].name));
         }
     }
 }
diff --git a/src/hexen/sc_man.c b/src/hexen/sc_man.c
index 833d7e1e..2802596b 100644
--- a/src/hexen/sc_man.c
+++ b/src/hexen/sc_man.c
@@ -138,7 +138,7 @@ static void OpenScript(char *name, int type)
         ScriptLumpNum = W_GetNumForName(name);
         ScriptBuffer = (char *) W_CacheLumpNum(ScriptLumpNum, PU_STATIC);
         ScriptSize = W_LumpLength(ScriptLumpNum);
-        strcpy(ScriptName, name);
+        M_StringCopy(ScriptName, name, sizeof(ScriptName));
     }
     else if (type == FILE_ZONE_SCRIPT)
     {                           // File script - zone
diff --git a/src/hexen/sv_save.c b/src/hexen/sv_save.c
index f81df46a..1a32ecd6 100644
--- a/src/hexen/sv_save.c
+++ b/src/hexen/sv_save.c
@@ -1948,7 +1948,7 @@ void SV_SaveGame(int slot, char *description)

     // Write version info
     memset(versionText, 0, HXS_VERSION_TEXT_LENGTH);
-    strcpy(versionText, HXS_VERSION_TEXT);
+    M_StringCopy(versionText, HXS_VERSION_TEXT, HXS_VERSION_TEXT_LENGTH);
     StreamOutBuffer(versionText, HXS_VERSION_TEXT_LENGTH);

     // Place a header marker