shithub: choc

Download patch

ref: 01ae851bf998330a9ca95467e238c507aa55cae1
parent: 6bb5f78bd2bdf10ddbb087171c6f640d425c9d40
author: Simon Howard <[email protected]>
date: Sun Mar 30 15:00:49 EDT 2014

hexen: Eliminate use of sprintf().

Use snprintf() in place of sprintf(). This is part of fixing #371.

--- a/src/hexen/am_map.c
+++ b/src/hexen/am_map.c
@@ -1474,7 +1474,8 @@
         else
         {
             MN_DrTextA(PlayerColorText[order[i]], 8, yPosition);
-            sprintf(textBuffer, "%d", fragCount[order[i]]);
+            snprintf(textBuffer, sizeof(textBuffer),
+                     "%d", fragCount[order[i]]);
             MN_DrTextA(textBuffer, 80, yPosition);
             yPosition += 10;
         }
@@ -1508,7 +1509,8 @@
     worldTimer -= minutes * 60;
     seconds = worldTimer;
 
-    sprintf(timeBuffer, "%.2d : %.2d : %.2d", hours, minutes, seconds);
+    snprintf(timeBuffer, sizeof(timeBuffer),
+             "%.2d : %.2d : %.2d", hours, minutes, seconds);
     MN_DrTextA(timeBuffer, 240, 8);
 
     if (days)
@@ -1515,11 +1517,11 @@
     {
         if (days == 1)
         {
-            sprintf(dayBuffer, "%.2d DAY", days);
+            snprintf(dayBuffer, sizeof(dayBuffer), "%.2d DAY", days);
         }
         else
         {
-            sprintf(dayBuffer, "%.2d DAYS", days);
+            snprintf(dayBuffer, sizeof(dayBuffer), "%.2d DAYS", days);
         }
         MN_DrTextA(dayBuffer, 240, 20);
         if (days >= 5)
--- a/src/hexen/h2_main.c
+++ b/src/hexen/h2_main.c
@@ -172,7 +172,7 @@
     {
         char buf[12];
 
-        sprintf(buf, "chatmacro%i", i);
+        snprintf(buf, sizeof(buf), "chatmacro%i", i);
         M_BindVariable(buf, &chat_macros[i]);
     }
 }
@@ -190,7 +190,7 @@
     if (!strcmp(SavePath, ""))
     {
         SavePath = malloc(10);
-	sprintf(SavePath, "hexndata%c", DIR_SEPARATOR);
+	snprintf(SavePath, 10, "hexndata%c", DIR_SEPARATOR);
     }
 }
 
@@ -632,7 +632,7 @@
     if (M_CheckParm("-debugfile"))
     {
         char filename[20];
-        sprintf(filename, "debug%i.txt", consoleplayer);
+        snprintf(filename, sizeof(filename), "debug%i.txt", consoleplayer);
         debugfile = fopen(filename, "w");
     }
     I_SetWindowTitle("Hexen");
--- a/src/hexen/in_lude.c
+++ b/src/hexen/in_lude.c
@@ -537,7 +537,8 @@
 
     if (!(val < -9 && wrapThresh < 1000))
     {
-        sprintf(buff, "%d", val >= wrapThresh ? val % wrapThresh : val);
+        snprintf(buff, sizeof(buff), "%d",
+                 val >= wrapThresh ? val % wrapThresh : val);
     }
     MN_DrTextA(buff, x - MN_TextAWidth(buff) / 2, y);
 }
@@ -554,7 +555,8 @@
 
     if (!(val < -9 && wrapThresh < 1000))
     {
-        sprintf(buff, "%d", val >= wrapThresh ? val % wrapThresh : val);
+        snprintf(buff, sizeof(buff), "%d",
+                 val >= wrapThresh ? val % wrapThresh : val);
     }
     MN_DrTextAYellow(buff, x - MN_TextAWidth(buff) / 2, y);
 }
--- a/src/hexen/mn_menu.c
+++ b/src/hexen/mn_menu.c
@@ -685,7 +685,7 @@
     char name[100];
     char versionText[HXS_VERSION_TEXT_LENGTH];
 
-    sprintf(name, "%shex%d.hxs", SavePath, slot);
+    snprintf(name, sizeof(name), "%shex%d.hxs", SavePath, slot);
 
     fp = fopen(name, "rb");
 
--- a/src/hexen/p_acs.c
+++ b/src/hexen/p_acs.c
@@ -443,7 +443,8 @@
     if (infoIndex == -1)
     {                           // Script not found
         //I_Error("P_StartACS: Unknown script number %d", number);
-        sprintf(ErrorMsg, "P_STARTACS ERROR: UNKNOWN SCRIPT %d", number);
+        snprintf(ErrorMsg, sizeof(ErrorMsg),
+                 "P_STARTACS ERROR: UNKNOWN SCRIPT %d", number);
         P_SetMessage(&players[consoleplayer], ErrorMsg, true);
     }
     statePtr = &ACSInfo[infoIndex].state;
@@ -539,8 +540,8 @@
     {
         if (!(mo->player->keys & (1 << (lock - 1))))
         {
-            sprintf(LockedBuffer, "YOU NEED THE %s\n",
-                    TextKeyMessages[lock - 1]);
+            snprintf(LockedBuffer, sizeof(LockedBuffer),
+                     "YOU NEED THE %s\n", TextKeyMessages[lock - 1]);
             P_SetMessage(mo->player, LockedBuffer, true);
             S_StartSound(mo, SFX_DOOR_LOCKED);
             return false;
--- a/src/hexen/p_setup.c
+++ b/src/hexen/p_setup.c
@@ -700,7 +700,7 @@
     P_InitThinkers();
     leveltime = 0;
 
-    sprintf(lumpname, "MAP%02d", map);
+    snprintf(lumpname, sizeof(lumpname), "MAP%02d", map);
     lumpnum = W_GetNumForName(lumpname);
     //
     // Begin processing map lumps
--- a/src/hexen/p_spec.c
+++ b/src/hexen/p_spec.c
@@ -428,7 +428,8 @@
     }
     if (!(mo->player->keys & (1 << (lock - 1))))
     {
-        sprintf(LockedBuffer, "YOU NEED THE %s\n", TextKeyMessages[lock - 1]);
+        snprintf(LockedBuffer, sizeof(LockedBuffer),
+                 "YOU NEED THE %s\n", TextKeyMessages[lock - 1]);
         P_SetMessage(mo->player, LockedBuffer, true);
         S_StartSound(mo, SFX_DOOR_LOCKED);
         return false;
--- a/src/hexen/sb_bar.c
+++ b/src/hexen/sb_bar.c
@@ -640,20 +640,20 @@
             MN_DrTextA("------", xPos[0], y);
             continue;
         }
-        sprintf(text, "%s", c->name);
+        snprintf(text, sizeof(text), "%s", c->name);
         M_ForceUppercase(text);
         MN_DrTextA(text, xPos[x++], y);
-        sprintf(text, "%d", c->mo->type);
+        snprintf(text, sizeof(text), "%d", c->mo->type);
         MN_DrTextA(text, xPos[x++], y);
-        sprintf(text, "%d", c->mo->x >> FRACBITS);
+        snprintf(text, sizeof(text), "%d", c->mo->x >> FRACBITS);
         MN_DrTextA(text, xPos[x++], y);
-        sprintf(text, "%d", c->mo->y >> FRACBITS);
+        snprintf(text, sizeof(text), "%d", c->mo->y >> FRACBITS);
         MN_DrTextA(text, xPos[x++], y);
-        sprintf(text, "%d", (int) c->id);
+        snprintf(text, sizeof(text), "%d", (int) c->id);
         MN_DrTextA(text, xPos[x++], y);
-        sprintf(text, "%d", c->priority);
+        snprintf(text, sizeof(text), "%d", c->priority);
         MN_DrTextA(text, xPos[x++], y);
-        sprintf(text, "%d", c->distance);
+        snprintf(text, sizeof(text), "%d", c->distance);
         MN_DrTextA(text, xPos[x++], y);
     }
     UpdateState |= I_FULLSCRN;
@@ -1738,7 +1738,7 @@
         P_SetMessage(player, TXT_CHEATBADINPUT, true);
         return;
     }
-    sprintf(mapName, "MAP%02d", map);
+    snprintf(mapName, sizeof(mapName), "MAP%02d", map);
     if (W_CheckNumForName(mapName) == -1)
     {                       // Can't find
         P_SetMessage(player, TXT_CHEATNOMAP, true);
@@ -1769,7 +1769,7 @@
     char buffer[80];
 
     count = P_Massacre();
-    sprintf(buffer, "%d MONSTERS KILLED\n", count);
+    snprintf(buffer, sizeof(buffer), "%d MONSTERS KILLED\n", count);
     P_SetMessage(player, buffer, true);
 }
 
@@ -1857,11 +1857,12 @@
 static void CheatDebugFunc(player_t * player, Cheat_t * cheat)
 {
     char textBuffer[50];
-    sprintf(textBuffer, "MAP %d (%d)  X:%5d  Y:%5d  Z:%5d",
-            P_GetMapWarpTrans(gamemap),
-            gamemap,
-            player->mo->x >> FRACBITS,
-            player->mo->y >> FRACBITS, player->mo->z >> FRACBITS);
+    snprintf(textBuffer, sizeof(textBuffer),
+             "MAP %d (%d)  X:%5d  Y:%5d  Z:%5d",
+             P_GetMapWarpTrans(gamemap),
+             gamemap,
+             player->mo->x >> FRACBITS,
+             player->mo->y >> FRACBITS, player->mo->z >> FRACBITS);
     P_SetMessage(player, textBuffer, true);
 }
 
@@ -1896,7 +1897,8 @@
 
     if (P_StartACS(script, 0, script_args, player->mo, NULL, 0))
     {
-        sprintf(textBuffer, "RUNNING SCRIPT %.2d", script);
+        snprintf(textBuffer, sizeof(textBuffer),
+                 "RUNNING SCRIPT %.2d", script);
         P_SetMessage(player, textBuffer, true);
     }
 }
@@ -1928,7 +1930,7 @@
         P_SetMessage(player, "ERROR INITIALIZING CD", true);
     }
 
-    sprintf(buffer, "ENTER DESIRED CD TRACK (%.2d - %.2d):\n",
+    snprintf(buffer, sizeof(buffer), "ENTER DESIRED CD TRACK (%.2d - %.2d):\n",
             I_CDMusFirstTrack(), I_CDMusLastTrack());
     P_SetMessage(player, buffer, true);
 }
@@ -1966,13 +1968,14 @@
 
     if (!S_StartCustomCDTrack(track))
     {
-        sprintf(buffer, "ERROR WHILE TRYING TO PLAY CD TRACK: %.2d\n", track);
+        snprintf(buffer, sizeof(buffer),
+                 "ERROR WHILE TRYING TO PLAY CD TRACK: %.2d\n", track);
         P_SetMessage(player, buffer, true);
     }
     else
     {
         // No error encountered while attempting to play the track
-        sprintf(buffer, "PLAYING TRACK: %.2d\n", track);
+        snprintf(buffer, sizeof(buffer), "PLAYING TRACK: %.2d\n", track);
         P_SetMessage(player, buffer, true);
     }
 }
--- a/src/hexen/sc_man.c
+++ b/src/hexen/sc_man.c
@@ -88,7 +88,7 @@
 
     if (sc_FileScripts == true)
     {
-        sprintf(fileName, "%s%s.txt", sc_ScriptsDir, name);
+        snprintf(fileName, sizeof(fileName), "%s%s.txt", sc_ScriptsDir, name);
         SC_OpenFile(fileName);
     }
     else
--- a/src/hexen/sv_save.c
+++ b/src/hexen/sv_save.c
@@ -1940,7 +1940,7 @@
     unsigned int i;
 
     // Open the output file
-    sprintf(fileName, "%shex6.hxs", SavePath);
+    snprintf(fileName, sizeof(fileName), "%shex6.hxs", SavePath);
     OpenStreamOut(fileName);
 
     // Write game save description
@@ -2000,7 +2000,7 @@
     SavingPlayers = savePlayers;
 
     // Open the output file
-    sprintf(fileName, "%shex6%02d.hxs", SavePath, gamemap);
+    snprintf(fileName, sizeof(fileName), "%shex6%02d.hxs", SavePath, gamemap);
     OpenStreamOut(fileName);
 
     // Place a header marker
@@ -2048,7 +2048,7 @@
     }
 
     // Create the name
-    sprintf(fileName, "%shex6.hxs", SavePath);
+    snprintf(fileName, sizeof(fileName), "%shex6.hxs", SavePath);
 
     // Load the file
     M_ReadFile(fileName, &SaveBuffer);
@@ -2192,7 +2192,7 @@
     TargetPlayerAddrs = NULL;
 
     gamemap = map;
-    sprintf(fileName, "%shex6%02d.hxs", SavePath, gamemap);
+    snprintf(fileName, sizeof(fileName), "%shex6%02d.hxs", SavePath, gamemap);
     if (!deathmatch && ExistingFile(fileName))
     {                           // Unarchive map
         SV_LoadMap();
@@ -2342,7 +2342,7 @@
 {
     char fileName[100];
 
-    sprintf(fileName, "%shex%d.hxs", SavePath, REBORN_SLOT);
+    snprintf(fileName, sizeof(fileName), "%shex%d.hxs", SavePath, REBORN_SLOT);
     return ExistingFile(fileName);
 }
 
@@ -2363,7 +2363,7 @@
     RemoveAllThinkers();
 
     // Create the name
-    sprintf(fileName, "%shex6%02d.hxs", SavePath, gamemap);
+    snprintf(fileName, sizeof(fileName), "%shex6%02d.hxs", SavePath, gamemap);
 
     // Load the file
     M_ReadFile(fileName, &SaveBuffer);
@@ -3204,10 +3204,11 @@
 
     for (i = 0; i < MAX_MAPS; i++)
     {
-        sprintf(fileName, "%shex%d%02d.hxs", SavePath, slot, i);
+        snprintf(fileName, sizeof(fileName),
+                 "%shex%d%02d.hxs", SavePath, slot, i);
         remove(fileName);
     }
-    sprintf(fileName, "%shex%d.hxs", SavePath, slot);
+    snprintf(fileName, sizeof(fileName), "%shex%d.hxs", SavePath, slot);
     remove(fileName);
 }
 
@@ -3227,17 +3228,21 @@
 
     for (i = 0; i < MAX_MAPS; i++)
     {
-        sprintf(sourceName, "%shex%d%02d.hxs", SavePath, sourceSlot, i);
+        snprintf(sourceName, sizeof(sourceName),
+                 "%shex%d%02d.hxs", SavePath, sourceSlot, i);
         if (ExistingFile(sourceName))
         {
-            sprintf(destName, "%shex%d%02d.hxs", SavePath, destSlot, i);
+            snprintf(destName, sizeof(destName),
+                     "%shex%d%02d.hxs", SavePath, destSlot, i);
             CopyFile(sourceName, destName);
         }
     }
-    sprintf(sourceName, "%shex%d.hxs", SavePath, sourceSlot);
+    snprintf(sourceName, sizeof(sourceName),
+             "%shex%d.hxs", SavePath, sourceSlot);
     if (ExistingFile(sourceName))
     {
-        sprintf(destName, "%shex%d.hxs", SavePath, destSlot);
+        snprintf(destName, sizeof(destName),
+                 "%shex%d.hxs", SavePath, destSlot);
         CopyFile(sourceName, destName);
     }
 }