shithub: choc

Download patch

ref: 6bb5f78bd2bdf10ddbb087171c6f640d425c9d40
parent: 4465be140a27e47aa8bed15be4e58064297c7d94
author: Simon Howard <[email protected]>
date: Sun Mar 30 14:50:35 EDT 2014

heretic: Eliminate use of sprintf().

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

--- a/src/heretic/am_map.c
+++ b/src/heretic/am_map.c
@@ -411,7 +411,7 @@
     //char namebuf[9];
 /*  for (i=0;i<10;i++)
   {
-    sprintf(namebuf, "AMMNUM%d", i);
+    snprintf(namebuf, sizeof(namebuf), "AMMNUM%d", i);
     marknums[i] = W_CacheLumpName(namebuf, PU_STATIC);
   }*/
     maplump = W_CacheLumpName(DEH_String("AUTOPAGE"), PU_STATIC);
@@ -603,7 +603,8 @@
         }
         else if (key == key_map_mark)
         {
-            sprintf(buffer, "%s %d", AMSTR_MARKEDSPOT, markpointnum);
+            snprintf(buffer, sizeof(buffer), "%s %d",
+                     AMSTR_MARKEDSPOT, markpointnum);
             plr->message = buffer;
             AM_addMark();
         }
--- a/src/heretic/d_main.c
+++ b/src/heretic/d_main.c
@@ -247,7 +247,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_GraphicsCheckCommandLine();
@@ -776,7 +776,7 @@
     {
         char buf[12];
 
-        sprintf(buf, "chatmacro%i", i);
+        snprintf(buf, sizeof(buf), "chatmacro%i", i);
         M_BindVariable(buf, &chat_macros[i]);
     }
 }
--- a/src/heretic/f_finale.c
+++ b/src/heretic/f_finale.c
@@ -405,7 +405,7 @@
         laststage = stage;
     }
 
-    sprintf(name, "END%i", stage);
+    snprintf(name, sizeof(name), "END%i", stage);
     V_DrawPatch((SCREENWIDTH - 13 * 8) / 2, (SCREENHEIGHT - 8 * 8) / 2,
                 W_CacheLumpName(name, PU_CACHE));
 }
--- a/src/heretic/i_sound.c
+++ b/src/heretic/i_sound.c
@@ -211,7 +211,8 @@
         return 0;
     if (sound->link)
         sound = sound->link;
-//  sprintf(namebuf, "d%c%s", snd_prefixen[snd_SfxDevice], sound->name);
+//  snprintf(namebuf, sizeof(namebuf), "d%c%s",
+//           snd_prefixen[snd_SfxDevice], sound->name);
     return W_GetNumForName(sound->name);
 
 }
@@ -319,14 +320,15 @@
     {
         if (debugmode)
         {
-            sprintf(tmp, "cfg p=0x%x, i=%d, d=%d\n",
-                    snd_SBport, snd_SBirq, snd_SBdma);
+            snprintf(tmp, sizeof(tmp), "cfg p=0x%x, i=%d, d=%d\n",
+                     snd_SBport, snd_SBirq, snd_SBdma);
             tprintf(tmp, 0);
         }
         if (SB_Detect(&snd_SBport, &snd_SBirq, &snd_SBdma, 0))
         {
-            sprintf(tmp, "SB isn't responding at p=0x%x, i=%d, d=%d\n",
-                    snd_SBport, snd_SBirq, snd_SBdma);
+            snprintf(tmp, sizeof(tmp),
+                     "SB isn't responding at p=0x%x, i=%d, d=%d\n",
+                     snd_SBport, snd_SBirq, snd_SBdma);
             tprintf(tmp, 0);
         }
         else
@@ -334,8 +336,8 @@
 
         if (debugmode)
         {
-            sprintf(tmp, "SB_Detect returned p=0x%x,i=%d,d=%d\n",
-                    snd_SBport, snd_SBirq, snd_SBdma);
+            snprintf(tmp, sizeof(tmp), "SB_Detect returned p=0x%x,i=%d,d=%d\n",
+                     snd_SBport, snd_SBirq, snd_SBdma);
             tprintf(tmp, 0);
         }
     }
@@ -352,14 +354,14 @@
     {
         if (debugmode)
         {
-            sprintf(tmp, "cfg p=0x%x\n", snd_Mport);
+            snprintf(tmp, sizeof(tmp), "cfg p=0x%x\n", snd_Mport);
             tprintf(tmp, 0);
         }
 
         if (MPU_Detect(&snd_Mport, &i))
         {
-            sprintf(tmp, "The MPU-401 isn't reponding @ p=0x%x.\n",
-                    snd_Mport);
+            snprintf(tmp, sizeof(tmp),
+                     "The MPU-401 isn't reponding @ p=0x%x.\n", snd_Mport);
             tprintf(tmp, 0);
         }
         else
@@ -397,11 +399,11 @@
 
     if (debugmode)
     {
-        sprintf(tmp, "  Music device #%d & dmxCode=%d", snd_MusicDevice,
-                dmxCodes[snd_MusicDevice]);
+        snprintf(tmp, sizeof(tmp), "  Music device #%d & dmxCode=%d",
+                 snd_MusicDevice, dmxCodes[snd_MusicDevice]);
         tprintf(tmp, 0);
-        sprintf(tmp, "  Sfx device #%d & dmxCode=%d\n", snd_SfxDevice,
-                dmxCodes[snd_SfxDevice]);
+        snprintf(tmp, sizeof(tmp),  "  Sfx device #%d & dmxCode=%d\n",
+                 snd_SfxDevice, dmxCodes[snd_SfxDevice]);
         tprintf(tmp, 0);
     }
 
@@ -412,7 +414,7 @@
 
     if (debugmode)
     {
-        sprintf(tmp, "  DMX_Init() returned %d", rc);
+        snprintf(tmp, sizeof(tmp), "  DMX_Init() returned %d", rc);
         tprintf(tmp, 0);
     }
 
--- a/src/heretic/p_saveg.c
+++ b/src/heretic/p_saveg.c
@@ -51,9 +51,12 @@
 char *SV_Filename(int slot)
 {
     char *filename;
+    size_t filename_len;
 
-    filename = malloc(strlen(savegamedir) + strlen(SAVEGAMENAME) + 8);
-    sprintf(filename, "%s" SAVEGAMENAME "%d.hsg", savegamedir, slot);
+    filename_len = strlen(savegamedir) + strlen(SAVEGAMENAME) + 8;
+    filename = malloc(filename_len);
+    snprintf(filename, filename_len,
+             "%s" SAVEGAMENAME "%d.hsg", savegamedir, slot);
 
     return filename;
 }
--- a/src/heretic/sb_bar.c
+++ b/src/heretic/sb_bar.c
@@ -484,20 +484,20 @@
             MN_DrTextA(DEH_String("------"), 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", c->id);
+        snprintf(text, sizeof(text), "%d", 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;