shithub: choc

Download patch

ref: 5f9b4368a2adad65dcc960a76c45d12059ca7214
parent: 040ca1cfb5a3e1be7d05bcd90eaa4f01fb68437a
author: Simon Howard <fraggle@gmail.com>
date: Sat Mar 29 17:24:03 EDT 2014

heretic: Eliminate use of unsafe string functions.

Eliminate use of strcpy, strcat, strncpy, and use the new safe
alternatives.

--- 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 @@
                 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)
--- 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 @@
 {
     if (using_graphical_startup)
     {
-        strcat(smsg, string);
+        M_StringConcat(smsg, string, sizeof(smsg));
         drawstatus();
     }
 }
@@ -676,7 +677,7 @@
 
     if (initflag)
         tmsg[0] = 0;
-    strcat(tmsg, msg);
+    M_StringConcat(tmsg, msg, sizeof(tmsg));
     blitStartup();
     DrawThermo();
     _setbkcolor(4);
@@ -685,7 +686,11 @@
         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;
--- 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 @@
     // 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;
 
--- a/src/heretic/g_game.c
+++ b/src/heretic/g_game.c
@@ -1008,16 +1008,16 @@
                         {
                             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 @@
 
     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 @@
 void G_SaveGame(int slot, char *description)
 {
     savegameslot = slot;
-    strcpy(savedescription, description);
+    M_StringCopy(savedescription, description, sizeof(savedescription));
     sendsave = true;
 }
 
--- 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 @@
     if (!FileMenuKeySteal)
     {
         FileMenuKeySteal = true;
-        strcpy(oldSlotText, SlotText[option]);
+        M_StringCopy(oldSlotText, SlotText[option], sizeof(oldSlotText));
         ptr = SlotText[option];
         while (*ptr)
         {
@@ -1513,7 +1514,8 @@
         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);
--- 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,7 +326,6 @@
 //
 // load the patch names from pnames.lmp
 //
-    name[8] = 0;
     names = W_CacheLumpName(pnames, PU_STATIC);
     nummappatches = LONG(*((int *) names));
     name_p = names + 4;
@@ -332,7 +332,7 @@
     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);