shithub: choc

Download patch

ref: 2e6e43c4a706e3670f131c7b2d5a5525f9bf0d7b
parent: 5f9b4368a2adad65dcc960a76c45d12059ca7214
author: Simon Howard <fraggle@gmail.com>
date: Sat Mar 29 17:25:55 EDT 2014

heretic: Eliminate use of unsafe string functions.

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

--- 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 @@
                 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)
--- 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 @@
 
     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);
--- a/src/hexen/g_game.c
+++ b/src/hexen/g_game.c
@@ -1017,11 +1017,13 @@
                         {
                             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_SaveGame(int slot, char *description)
 {
     savegameslot = slot;
-    strcpy(savedescription, description);
+    M_StringCopy(savedescription, description, sizeof(savedescription));
     sendsave = true;
 }
 
@@ -1810,8 +1812,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;
--- a/src/hexen/h2_main.c
+++ b/src/hexen/h2_main.c
@@ -551,12 +551,11 @@
     {
         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);
--- 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 @@
     if (!FileMenuKeySteal)
     {
         FileMenuKeySteal = true;
-        strcpy(oldSlotText, SlotText[option]);
+        M_StringCopy(oldSlotText, SlotText[option], sizeof(oldSlotText));
         ptr = SlotText[option];
         while (*ptr)
         {
@@ -1635,8 +1636,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/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 CmdPrintString(void)
 {
-    strcat(PrintBuffer, ACStrings[Pop()]);
+    M_StringConcat(PrintBuffer, ACStrings[Pop()], sizeof(PrintBuffer));
     return SCRIPT_CONTINUE;
 }
 
@@ -1690,8 +1691,8 @@
 {
     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;
 }
 
--- 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 @@
     {
         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 @@
     {
         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)
--- 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 @@
     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,13 +840,13 @@
         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;
@@ -852,7 +853,7 @@
 
         // 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 @@
     {
         return;
     }
-    strcpy(MapInfo[map].songLump, lumpName);
+    M_StringCopy(MapInfo[map].songLump, lumpName,
+                 sizeof(MapInfo[map].songLump));
 }
 
 //==========================================================================
@@ -1210,7 +1212,8 @@
 
     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;
 }
--- a/src/hexen/r_data.c
+++ b/src/hexen/r_data.c
@@ -315,7 +315,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;
@@ -322,7 +321,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, 8);
         patchlookup[i] = W_CheckNumForName(name);
     }
     W_ReleaseLumpName("PNAMES");
--- a/src/hexen/s_sound.c
+++ b/src/hexen/s_sound.c
@@ -981,11 +981,13 @@
                     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 @@
     {
         if (!strcmp(S_sfx[i].name, ""))
         {
-            strcpy(S_sfx[i].name, "default");
+            M_StringCopy(S_sfx[i].name, "default", sizeof(S_sfx[i].name));
         }
     }
 }
--- a/src/hexen/sc_man.c
+++ b/src/hexen/sc_man.c
@@ -138,7 +138,7 @@
         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
--- a/src/hexen/sv_save.c
+++ b/src/hexen/sv_save.c
@@ -1948,7 +1948,7 @@
 
     // 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