shithub: choc

Download patch

ref: 17c14e1fad6dc277a6e58e4f421d5c65e210d1fe
parent: 8eb3200286d523379295143ce3f44d77ce036d4b
author: Simon Howard <fraggle@gmail.com>
date: Tue Apr 1 16:56:33 EDT 2014

setup: Eliminate use of sprintf().

Use M_snprintf() or M_StringJoin() instead where appropriate.
This fixes #371.

--- a/src/setup/display.c
+++ b/src/setup/display.c
@@ -29,6 +29,7 @@
 
 #include "textscreen.h"
 #include "m_config.h"
+#include "m_misc.h"
 #include "mode.h"
 
 #include "display.h"
@@ -152,8 +153,7 @@
     {
         char *env_string;
 
-        env_string = malloc(strlen(video_driver) + 30);
-        sprintf(env_string, "SDL_VIDEODRIVER=%s", video_driver);
+        env_string = M_StringJoin("SDL_VIDEODRIVER=", video_driver, NULL);
         putenv(env_string);
         free(env_string);
     }
@@ -487,7 +487,7 @@
             continue;
         }
 
-        sprintf(buf, "%ix%i", modes[i].w, modes[i].h);
+        M_snprintf(buf, sizeof(buf), "%ix%i", modes[i].w, modes[i].h);
         rbutton = TXT_NewRadioButton(buf, &vidmode, i);
         TXT_AddWidget(modes_table, rbutton);
         TXT_SignalConnect(rbutton, "selected", ModeSelected, &modes[i]);
--- a/src/setup/execute.c
+++ b/src/setup/execute.c
@@ -61,7 +61,6 @@
 
 static char *TempFile(char *s)
 {
-    char *result;
     char *tempdir;
 
 #ifdef _WIN32
@@ -79,10 +78,7 @@
     tempdir = "/tmp";
 #endif
 
-    result = malloc(strlen(tempdir) + strlen(s) + 2);
-    sprintf(result, "%s%c%s", tempdir, DIR_SEPARATOR, s);
-
-    return result;
+    return M_StringJoin(tempdir, DIR_SEPARATOR_S, s, NULL);
 }
 
 static int ArgumentNeedsEscape(char *arg)
@@ -345,8 +341,7 @@
 
     // Build the command line
 
-    response_file_arg = malloc(strlen(context->response_file) + 2);
-    sprintf(response_file_arg, "@%s", context->response_file);
+    response_file_arg = M_StringJoin("@", context->response_file);
 
     // Run Doom
 
--- a/src/setup/keyboard.c
+++ b/src/setup/keyboard.c
@@ -23,6 +23,7 @@
 #include "doomtype.h"
 #include "m_config.h"
 #include "m_controls.h"
+#include "m_misc.h"
 
 #include "execute.h"
 #include "txt_keyinput.h"
@@ -171,7 +172,7 @@
                               NULL);
     }
 
-    sprintf(buf, " - %s - ", title);
+    M_snprintf(buf, sizeof(buf), " - %s - ", title);
 
     TXT_AddWidgets(table, TXT_NewLabel(buf),  TXT_NewStrut(0, 0),
                           NULL);
--- a/src/setup/mode.c
+++ b/src/setup/mode.c
@@ -34,6 +34,7 @@
 #include "m_argv.h"
 #include "m_config.h"
 #include "m_controls.h"
+#include "m_misc.h"
 
 #include "compatibility.h"
 #include "display.h"
@@ -140,8 +141,7 @@
         if (!strcmp(savedir, ""))
         {
             free(savedir);
-            savedir = malloc(10);
-            sprintf(savedir, "hexndata%c", DIR_SEPARATOR);
+            savedir = "hexndata" DIR_SEPARATOR_S;
         }
     }
 
@@ -215,8 +215,7 @@
     extension = "";
 #endif
 
-    executable = malloc(strlen(config->executable) + 5);
-    sprintf(executable, "%s%s", config->executable, extension);
+    executable = M_StringJoin(config->executable, extension, NULL);
 }
 
 static void SetMission(mission_config_t *config)
--- a/src/setup/multiplayer.c
+++ b/src/setup/multiplayer.c
@@ -308,11 +308,11 @@
 
     if (warptype == WARP_ExMy)
     {
-        sprintf(buf, "E%iM%i", warpepisode, warpmap);
+        M_snprintf(buf, sizeof(buf), "E%iM%i", warpepisode, warpmap);
     }
     else if (warptype == WARP_MAPxy)
     {
-        sprintf(buf, "MAP%02i", warpmap);
+        M_snprintf(buf, sizeof(buf), "MAP%02i", warpmap);
     }
 
     TXT_SetButtonLabel(warpbutton, buf);
@@ -425,7 +425,7 @@
                     continue;
                 }
 
-                sprintf(buf, " E%iM%i ", x, y);
+                M_snprintf(buf, sizeof(buf), " E%iM%i ", x, y);
                 button = TXT_NewButton(buf);
                 TXT_SignalConnect(button, "pressed",
                                   SetExMyWarp, (void *) (x * 10 + y));
@@ -457,7 +457,7 @@
                 continue;
             }
 
-            sprintf(buf, " MAP%02i ", l);
+            M_snprintf(buf, sizeof(buf), " MAP%02i ", l);
             button = TXT_NewButton(buf);
             TXT_SignalConnect(button, "pressed", 
                               SetMAPxyWarp, (void *) l);
@@ -898,7 +898,7 @@
     char ping_time_str[16];
     char description[47];
 
-    sprintf(ping_time_str, "%ims", ping_time);
+    M_snprintf(ping_time_str, sizeof(ping_time_str), "%ims", ping_time);
     M_StringCopy(description, querydata->description,
                  sizeof(description));
 
@@ -1094,7 +1094,7 @@
 
     for (i=0; i<10; ++i)
     {
-        sprintf(buf, "#%i ", i + 1);
+        M_snprintf(buf, sizeof(buf), "#%i ", i + 1);
 
         label = TXT_NewLabel(buf);
         TXT_SetFGColor(label, TXT_COLOR_BRIGHT_CYAN);
@@ -1119,7 +1119,7 @@
 
     for (i=0; i<10; ++i)
     {
-        sprintf(buf, "chatmacro%i", i);
+        M_snprintf(buf, sizeof(buf), "chatmacro%i", i);
         M_BindVariable(buf, &chat_macros[i]);
     }
 
--- a/src/setup/txt_joybinput.c
+++ b/src/setup/txt_joybinput.c
@@ -125,9 +125,9 @@
     joystick_input->widget.h = 1;
 }
 
-static void GetJoystickButtonDescription(int button, char *buf)
+static void GetJoystickButtonDescription(int button, char *buf, size_t buf_len)
 {
-    sprintf(buf, "BUTTON #%i", button + 1);
+    M_snprintf(buf, buf_len, "BUTTON #%i", button + 1);
 }
 
 static void TXT_JoystickInputDrawer(TXT_UNCAST_ARG(joystick_input))
@@ -142,7 +142,8 @@
     }
     else
     {
-        GetJoystickButtonDescription(*joystick_input->variable, buf);
+        GetJoystickButtonDescription(*joystick_input->variable,
+                                     buf, sizeof(buf));
     }
 
     TXT_SetWidgetBG(joystick_input);