foxygit / doom Log in
commit 5f9b4368a2adad65dcc960a76c45d12059ca7214
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Sat Mar 29 21:24:03 2014 -0400
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Sat Mar 29 21:24:03 2014 -0400

    heretic: Eliminate use of unsafe string functions.

    Eliminate use of strcpy, strcat, strncpy, and use the new safe
    alternatives.
---
 src/heretic/ct_chat.c | 12 +++++++-----
 src/heretic/d_main.c  | 11 ++++++++---
 src/heretic/d_net.c   |  5 +++--
 src/heretic/g_game.c  | 18 +++++++++---------
 src/heretic/mn_menu.c |  6 ++++--
 src/heretic/r_data.c  |  4 ++--
 6 files changed, 33 insertions(+), 23 deletions(-)

diff --git a/src/heretic/ct_chat.c b/src/heretic/ct_chat.c
index 19c27bb2..80ad8c87 100644
--- a/src/heretic/ct_chat.c
+++ b/src/heretic/ct_chat.c
@@ -33,6 +33,7 @@

 #include "deh_str.h"
 #include "m_controls.h"
+#include "m_misc.h"
 #include "p_local.h"
 #include "s_sound.h"
 #include "v_video.h"
@@ -288,14 +289,15 @@ void CT_Ticker(void)
                 CT_AddChar(i, 0);       // set the end of message character
                 if (numplayers > 2)
                 {
-                    strncpy(plr_lastmsg[i], DEH_String(CT_FromPlrText[i]),
-                            MESSAGESIZE + 9);
-                    plr_lastmsg[i][MESSAGESIZE + 8] = '\0';
-                    strcat(plr_lastmsg[i], chat_msg[i]);
+                    M_StringCopy(plr_lastmsg[i], DEH_String(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/heretic/d_main.c b/src/heretic/d_main.c
index 178db2eb..7a5517f2 100644
--- a/src/heretic/d_main.c
+++ b/src/heretic/d_main.c
@@ -48,6 +48,7 @@
 #include "m_argv.h"
 #include "m_config.h"
 #include "m_controls.h"
+#include "m_misc.h"
 #include "p_local.h"
 #include "s_sound.h"
 #include "w_main.h"
@@ -543,7 +544,7 @@ void status(char *string)
 {
     if (using_graphical_startup)
     {
-        strcat(smsg, string);
+        M_StringConcat(smsg, string, sizeof(smsg));
         drawstatus();
     }
 }
@@ -676,7 +677,7 @@ void tprintf(char *msg, int initflag)

     if (initflag)
         tmsg[0] = 0;
-    strcat(tmsg, msg);
+    M_StringConcat(tmsg, msg, sizeof(tmsg));
     blitStartup();
     DrawThermo();
     _setbkcolor(4);
@@ -685,7 +686,11 @@ void tprintf(char *msg, int initflag)
         if ((tmsg[i] == '\n') || (!tmsg[i]))
         {
             memset(temp, 0, 80);
-            strncpy(temp, tmsg + start, i - start);
+            M_StringCopy(temp, tmsg + start, sizeof(temp));
+            if (i - start < sizeof(temp))
+            {
+                temp[i - start] = '\0';
+            }
             _settextposition(MSG_Y + add, 40 - strlen(temp) / 2);
             _outtext(temp);
             start = i + 1;
diff --git a/src/heretic/d_net.c b/src/heretic/d_net.c
index e774eb03..f10302d0 100644
--- a/src/heretic/d_net.c
+++ b/src/heretic/d_net.c
@@ -29,11 +29,12 @@

 #include "doomfeatures.h"

-#include "m_argv.h"
 #include "i_system.h"
 #include "i_timer.h"
 #include "i_video.h"
 #include "doomdef.h"
+#include "m_argv.h"
+#include "m_misc.h"
 #include "w_checksum.h"

 #include "deh_main.h"
@@ -60,7 +61,7 @@ static void PlayerQuitGame(player_t *player)
     // The Heretic source code does this, which doesn't actually work.
     // As a result, the exit message is never seen.

-    strcpy(exitmsg, "PLAYER 1 LEFT THE GAME");
+    M_StringCopy(exitmsg, "PLAYER 1 LEFT THE GAME", sizeof(exitmsg));
     exitmsg[7] += player_num;
     players[consoleplayer].message = exitmsg;

diff --git a/src/heretic/g_game.c b/src/heretic/g_game.c
index c48b9c8e..64186769 100644
--- a/src/heretic/g_game.c
+++ b/src/heretic/g_game.c
@@ -1008,16 +1008,16 @@ void G_Ticker(void)
                         {
                             if (netgame)
                             {
-                                strncpy(savedescription, DEH_String("NET GAME"),
-                                        sizeof(savedescription));
+                                M_StringCopy(savedescription,
+                                             DEH_String("NET GAME"),
+                                             sizeof(savedescription));
                             }
                             else
                             {
-                                strncpy(savedescription, DEH_String("SAVE GAME"),
-                                        sizeof(savedescription));
+                                M_StringCopy(savedescription,
+                                             DEH_String("SAVE GAME"),
+                                             sizeof(savedescription));
                             }
-
-                            savedescription[sizeof(savedescription) - 1] = '\0';
                         }
                         savegameslot =
                             (players[i].cmd.
@@ -1672,8 +1672,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;
@@ -1816,7 +1816,7 @@ boolean G_CheckDemoStatus(void)
 void G_SaveGame(int slot, char *description)
 {
     savegameslot = slot;
-    strcpy(savedescription, description);
+    M_StringCopy(savedescription, description, sizeof(savedescription));
     sendsave = true;
 }

diff --git a/src/heretic/mn_menu.c b/src/heretic/mn_menu.c
index 1c94b20b..3057261f 100644
--- a/src/heretic/mn_menu.c
+++ b/src/heretic/mn_menu.c
@@ -32,6 +32,7 @@
 #include "i_system.h"
 #include "i_swap.h"
 #include "m_controls.h"
+#include "m_misc.h"
 #include "p_local.h"
 #include "r_local.h"
 #include "s_sound.h"
@@ -854,7 +855,7 @@ static boolean SCSaveGame(int option)
     if (!FileMenuKeySteal)
     {
         FileMenuKeySteal = true;
-        strcpy(oldSlotText, SlotText[option]);
+        M_StringCopy(oldSlotText, SlotText[option], sizeof(oldSlotText));
         ptr = SlotText[option];
         while (*ptr)
         {
@@ -1513,7 +1514,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/heretic/r_data.c b/src/heretic/r_data.c
index a2f3f10e..34fcb225 100644
--- a/src/heretic/r_data.c
+++ b/src/heretic/r_data.c
@@ -29,6 +29,7 @@

 #include "i_swap.h"
 #include "i_system.h"
+#include "m_misc.h"
 #include "r_local.h"
 #include "p_local.h"

@@ -325,14 +326,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, sizeof(name));
         patchlookup[i] = W_CheckNumForName(name);
     }
     W_ReleaseLumpName(pnames);