shithub: choc

Download patch

ref: 4465be140a27e47aa8bed15be4e58064297c7d94
parent: e56c9c948df1ad0e2ff2b64d8593caefbf98fd11
author: Simon Howard <fraggle@gmail.com>
date: Sun Mar 30 14:21:42 EDT 2014

doom: Eliminate use of sprintf().

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

--- a/src/doom/am_map.c
+++ b/src/doom/am_map.c
@@ -689,7 +689,8 @@
         }
         else if (key == key_map_mark)
         {
-            sprintf(buffer, "%s %d", DEH_String(AMSTR_MARKEDSPOT), markpointnum);
+            snprintf(buffer, sizeof(buffer), "%s %d",
+                     DEH_String(AMSTR_MARKEDSPOT), markpointnum);
             plr->message = buffer;
             AM_addMark();
         }
--- a/src/doom/d_main.c
+++ b/src/doom/d_main.c
@@ -384,7 +384,7 @@
     {
         char buf[12];
 
-        sprintf(buf, "chatmacro%i", i);
+        snprintf(buf, sizeof(buf), "chatmacro%i", i);
         M_BindVariable(buf, &chat_macros[i]);
     }
 }
@@ -669,7 +669,8 @@
             gamename_size = strlen(deh_sub) + 10;
             gamename = Z_Malloc(gamename_size, PU_STATIC, 0);
             version = G_VanillaVersionCode();
-            sprintf(gamename, deh_sub, version / 100, version % 100);
+            snprintf(gamename, gamename_size, deh_sub,
+                     version / 100, version % 100);
 
             while (gamename[0] != '\0' && isspace(gamename[0]))
             {
--- a/src/doom/g_game.c
+++ b/src/doom/g_game.c
@@ -909,17 +909,18 @@
                 turbodetected[i] = true;
             }
 
-	    if ((gametic & 31) == 0 
+            if ((gametic & 31) == 0 
              && ((gametic >> 5) % MAXPLAYERS) == i
              && turbodetected[i])
-	    {
-		static char turbomessage[80];
-		extern char *player_names[4];
-		sprintf (turbomessage, "%s is turbo!",player_names[i]);
-		players[consoleplayer].message = turbomessage;
+            {
+                static char turbomessage[80];
+                extern char *player_names[4];
+                snprintf(turbomessage, sizeof(turbomessage),
+                         "%s is turbo!", player_names[i]);
+                players[consoleplayer].message = turbomessage;
                 turbodetected[i] = false;
-	    }
-			
+            }
+
 	    if (netgame && !netdemo && !(gametic%ticdup) ) 
 	    { 
 		if (gametic > BACKUPTICS 
@@ -1937,16 +1938,18 @@
  
  
 //
-// G_RecordDemo 
-// 
-void G_RecordDemo (char *name) 
-{ 
-    int             i; 
-    int				maxsize;
-	
-    usergame = false; 
-    demoname = Z_Malloc(strlen(name) + 5, PU_STATIC, NULL);
-    sprintf(demoname, "%s.lmp", name);
+// G_RecordDemo
+//
+void G_RecordDemo (char *name)
+{
+    size_t demoname_size;
+    int i;
+    int maxsize;
+
+    usergame = false;
+    demoname_size = strlen(name) + 5;
+    demoname = Z_Malloc(demoname_size, PU_STATIC, NULL);
+    snprintf(demoname, demoname_size, "%s.lmp", name);
     maxsize = 0x20000;
 
     //!
@@ -2073,7 +2076,8 @@
     }
     else
     {
-        sprintf(resultbuf, "%i.%i (unknown)", version / 100, version % 100);
+        snprintf(resultbuf, sizeof(resultbuf),
+                 "%i.%i (unknown)", version / 100, version % 100);
         return resultbuf;
     }
 }
--- a/src/doom/hu_stuff.c
+++ b/src/doom/hu_stuff.c
@@ -627,7 +627,7 @@
 		// static unsigned char buf[20]; // DEBUG
 		HU_queueChatChar(c);
 		
-		// sprintf(buf, "KEY: %d => %d", ev->data1, c);
+		// snprintf(buf, sizeof(buf), "KEY: %d => %d", ev->data1, c);
 		//      plr->message = buf;
 	    }
 	    if (c == KEY_ENTER)
--- a/src/doom/m_menu.c
+++ b/src/doom/m_menu.c
@@ -1192,10 +1192,9 @@
 
 void M_QuitDOOM(int choice)
 {
-    sprintf(endstring,
-            DEH_String("%s\n\n" DOSY),
-            DEH_String(M_SelectEndMessage()));
-  
+    DEH_snprintf(endstring, sizeof(endstring), "%s\n\n" DOSY,
+                 DEH_String(M_SelectEndMessage()));
+
     M_StartMessage(endstring,M_QuitResponse,true);
 }
 
--- a/src/doom/p_saveg.c
+++ b/src/doom/p_saveg.c
@@ -38,6 +38,7 @@
 // State.
 #include "doomstat.h"
 #include "g_game.h"
+#include "m_misc.h"
 #include "r_state.h"
 
 #define SAVEGAME_EOF 0x1d
@@ -57,11 +58,9 @@
 
     if (filename == NULL)
     {
-        filename = malloc(strlen(savegamedir) + 32);
+        filename = M_StringJoin(savegamedir, "temp.dsg", NULL);
     }
 
-    sprintf(filename, "%stemp.dsg", savegamedir);
-
     return filename;
 }
 
@@ -70,17 +69,18 @@
 char *P_SaveGameFile(int slot)
 {
     static char *filename = NULL;
+    static size_t filename_size = 0;
     char basename[32];
 
     if (filename == NULL)
     {
-        filename = malloc(strlen(savegamedir) + 32);
+        filename_size = strlen(savegamedir) + 32;
+        filename = malloc(filename_size);
     }
 
     DEH_snprintf(basename, 32, SAVEGAMENAME "%d.dsg", slot);
+    snprintf(filename, filename_size, "%s%s", savegamedir, basename);
 
-    sprintf(filename, "%s%s", savegamedir, basename);
-
     return filename;
 }
 
@@ -1363,7 +1363,7 @@
         saveg_write8(0);
 
     memset(name, 0, sizeof(name));
-    sprintf(name, "version %i", G_VanillaVersionCode());
+    snprintf(name, sizeof(name), "version %i", G_VanillaVersionCode());
 
     for (i=0; i<VERSIONSIZE; ++i)
         saveg_write8(name[i]);
@@ -1400,7 +1400,7 @@
         read_vcheck[i] = saveg_read8();
 
     memset(vcheck, 0, sizeof(vcheck));
-    sprintf(vcheck, "version %i", G_VanillaVersionCode());
+    snprintf(vcheck, sizeof(vcheck), "version %i", G_VanillaVersionCode());
     if (strcmp(read_vcheck, vcheck) != 0)
 	return false;				// bad version 
 
--- a/src/doom/s_sound.c
+++ b/src/doom/s_sound.c
@@ -638,7 +638,7 @@
     // get lumpnum if neccessary
     if (!music->lumpnum)
     {
-        sprintf(namebuf, "d_%s", DEH_String(music->name));
+        snprintf(namebuf, sizeof(namebuf), "d_%s", DEH_String(music->name));
         music->lumpnum = W_GetNumForName(namebuf);
     }
 
--- a/src/doom/st_stuff.c
+++ b/src/doom/st_stuff.c
@@ -597,12 +597,12 @@
       // 'mypos' for player position
       else if (cht_CheckCheat(&cheat_mypos, ev->data2))
       {
-	static char	buf[ST_MSGWIDTH];
-	sprintf(buf, "ang=0x%x;x,y=(0x%x,0x%x)",
-		players[consoleplayer].mo->angle,
-		players[consoleplayer].mo->x,
-		players[consoleplayer].mo->y);
-	plyr->message = buf;
+        static char buf[ST_MSGWIDTH];
+        snprintf(buf, sizeof(buf), "ang=0x%x;x,y=(0x%x,0x%x)",
+                 players[consoleplayer].mo->angle,
+                 players[consoleplayer].mo->x,
+                 players[consoleplayer].mo->y);
+        plyr->message = buf;
       }
     }