foxygit / doom Log in
commit 71d8ef248f4f1a05646f5e1e7e6388a59f08b121
Author:     Simon Howard <fraggle@gmail.com>
AuthorDate: Sun Mar 30 18:07:52 2014 -0400
Commit:     Simon Howard <fraggle@gmail.com>
CommitDate: Sun Mar 30 18:09:07 2014 -0400

    Eliminate some uses of sprintf() from common code.

    As part of this, add DIR_SEPARATOR_S as a string version of the
    DIR_SEPARATOR macro. Change M_TempFile() to return a string allocated
    on the C heap rather than the zone heap.

    This is a first step towards fixing #371.
---
 src/d_iwad.c      | 29 +++++++++++------------------
 src/doom/m_menu.c |  4 ++--
 src/doomtype.h    |  2 ++
 src/i_oplmusic.c  | 30 +++++++++++++++++-------------
 src/i_sdlmusic.c  |  7 +++----
 src/i_sdlsound.c  |  3 ++-
 src/i_video.c     | 16 ++++++----------
 src/m_config.c    | 31 +++++++++++++------------------
 src/m_controls.c  |  2 +-
 src/m_misc.c      |  6 +-----
 src/net_gui.c     |  6 +++---
 src/v_video.c     |  2 +-
 12 files changed, 62 insertions(+), 76 deletions(-)

diff --git a/src/d_iwad.c b/src/d_iwad.c
index cb955685..508abf9d 100644
--- a/src/d_iwad.c
+++ b/src/d_iwad.c
@@ -289,11 +289,8 @@ static void CheckCollectorsEdition(void)

     for (i=0; i<arrlen(collectors_edition_subdirs); ++i)
     {
-        subpath = malloc(strlen(install_path)
-                         + strlen(collectors_edition_subdirs[i])
-                         + 5);
-
-        sprintf(subpath, "%s\\%s", install_path, collectors_edition_subdirs[i]);
+        subpath = M_StringJoin(install_path, DIR_SEPARATOR_S,
+                               collectors_edition_subdirs[i], NULL);

         AddIWADDir(subpath);
     }
@@ -319,10 +316,8 @@ static void CheckSteamEdition(void)

     for (i=0; i<arrlen(steam_install_subdirs); ++i)
     {
-        subpath = malloc(strlen(install_path)
-                         + strlen(steam_install_subdirs[i]) + 5);
-
-        sprintf(subpath, "%s\\%s", install_path, steam_install_subdirs[i]);
+        subpath = M_StringJoin(install_path, DIR_SEPARATOR_S,
+                               steam_install_subdirs[i], NULL);

         AddIWADDir(subpath);
     }
@@ -438,8 +433,7 @@ static char *CheckDirectoryHasIWAD(char *dir, char *iwadname)
     }
     else
     {
-        char sep[] = {DIR_SEPARATOR, '\0'};
-        filename = M_StringJoin(dir, sep, iwadname, NULL);
+        filename = M_StringJoin(dir, DIR_SEPARATOR_S, iwadname, NULL);
     }

     if (M_FileExists(filename))
@@ -632,7 +626,7 @@ static void BuildIWADDirList(void)

 char *D_FindWADByName(char *name)
 {
-    char *buf;
+    char *path;
     int i;

     // Absolute path?
@@ -643,7 +637,7 @@ char *D_FindWADByName(char *name)
     }

     BuildIWADDirList();
-
+
     // Search through all IWAD paths for a file with the given name.

     for (i=0; i<num_iwad_dirs; ++i)
@@ -659,15 +653,14 @@ char *D_FindWADByName(char *name)

         // Construct a string for the full path

-        buf = malloc(strlen(iwad_dirs[i]) + strlen(name) + 5);
-        sprintf(buf, "%s%c%s", iwad_dirs[i], DIR_SEPARATOR, name);
+        path = M_StringJoin(iwad_dirs[i], DIR_SEPARATOR_S, name, NULL);

-        if (M_FileExists(buf))
+        if (M_FileExists(path))
         {
-            return buf;
+            return path;
         }

-        free(buf);
+        free(path);
     }

     // File not found
diff --git a/src/doom/m_menu.c b/src/doom/m_menu.c
index 93941b3b..1fde34ba 100644
--- a/src/doom/m_menu.c
+++ b/src/doom/m_menu.c
@@ -1932,12 +1932,12 @@ void M_StartControlPanel (void)

 static void M_DrawOPLDev(void)
 {
-    extern void I_OPL_DevMessages(char *);
+    extern void I_OPL_DevMessages(char *, size_t);
     char debug[1024];
     char *curr, *p;
     int line;

-    I_OPL_DevMessages(debug);
+    I_OPL_DevMessages(debug, sizeof(debug));
     curr = debug;
     line = 0;

diff --git a/src/doomtype.h b/src/doomtype.h
index fdcdb66b..626d69a8 100644
--- a/src/doomtype.h
+++ b/src/doomtype.h
@@ -97,11 +97,13 @@ typedef uint8_t byte;
 #ifdef _WIN32

 #define DIR_SEPARATOR '\\'
+#define DIR_SEPARATOR_S "\\"
 #define PATH_SEPARATOR ';'

 #else

 #define DIR_SEPARATOR '/'
+#define DIR_SEPARATOR_S "/"
 #define PATH_SEPARATOR ':'

 #endif
diff --git a/src/i_oplmusic.c b/src/i_oplmusic.c
index 62cbaca7..379537dd 100644
--- a/src/i_oplmusic.c
+++ b/src/i_oplmusic.c
@@ -1376,7 +1376,7 @@ static void *I_OPL_RegisterSong(void *data, int len)
     {
         M_WriteFile(filename, data, len);
     }
-    else
+    else
     {
 	// Assume a MUS file and try to convert

@@ -1393,8 +1393,7 @@ static void *I_OPL_RegisterSong(void *data, int len)
     // remove file now

     remove(filename);
-
-    Z_Free(filename);
+    free(filename);

     return result;
 }
@@ -1519,19 +1518,20 @@ static int ChannelInUse(opl_channel_data_t *channel)
     return 0;
 }

-void I_OPL_DevMessages(char *result)
+void I_OPL_DevMessages(char *result, size_t result_len)
 {
+    char tmp[80];
     int instr_num;
     int lines;
     int i;

     if (num_tracks == 0)
     {
-        sprintf(result, "No OPL track!");
+        snprintf(result, result_len, "No OPL track!");
         return;
     }

-    sprintf(result, "Tracks:\n");
+    snprintf(result, result_len, "Tracks:\n");
     lines = 1;

     for (i = 0; i < NumActiveChannels(); ++i)
@@ -1543,16 +1543,19 @@ void I_OPL_DevMessages(char *result)

         instr_num = tracks[0].channels[i].instrument - main_instrs;

-        sprintf(result + strlen(result),
+        snprintf(tmp, sizeof(tmp),
                 "chan %i: %c i#%i (%s)\n",
                 i,
                 ChannelInUse(&tracks[0].channels[i]) ? '\'' : ' ',
                 instr_num + 1,
                 main_instr_names[instr_num]);
+        M_StringConcat(result, tmp, result_len);
+
         ++lines;
     }

-    sprintf(result + strlen(result), "\nLast percussion:\n");
+    snprintf(tmp, sizeof(tmp), "\nLast percussion:\n");
+    M_StringConcat(result, tmp, result_len);
     lines += 2;

     i = (last_perc_count + PERCUSSION_LOG_LEN - 1) % PERCUSSION_LOG_LEN;
@@ -1563,11 +1566,12 @@ void I_OPL_DevMessages(char *result)
             break;
         }

-        sprintf(result + strlen(result),
-                "%cp#%i (%s)\n",
-                i == 0 ? '\'' : ' ',
-                last_perc[i],
-                percussion_names[last_perc[i] - 35]);
+        snprintf(tmp, sizeof(tmp),
+                 "%cp#%i (%s)\n",
+                 i == 0 ? '\'' : ' ',
+                 last_perc[i],
+                 percussion_names[last_perc[i] - 35]);
+        M_StringConcat(result, tmp, result_len);
         ++lines;

         i = (i + PERCUSSION_LOG_LEN - 1) % PERCUSSION_LOG_LEN;
diff --git a/src/i_sdlmusic.c b/src/i_sdlmusic.c
index fd5aa006..50a7bbbf 100644
--- a/src/i_sdlmusic.c
+++ b/src/i_sdlmusic.c
@@ -116,13 +116,12 @@ void I_InitTimidityConfig(void)

     if (success)
     {
-        env_string = malloc(strlen(temp_timidity_cfg) + 15);
-        sprintf(env_string, "TIMIDITY_CFG=%s", temp_timidity_cfg);
+        env_string = M_StringJoin("TIMIDITY_CFG=", temp_timidity_cfg, NULL);
         putenv(env_string);
     }
     else
     {
-        Z_Free(temp_timidity_cfg);
+        free(temp_timidity_cfg);
         temp_timidity_cfg = NULL;
     }
 }
@@ -426,7 +425,7 @@ static void *I_SDL_RegisterSong(void *data, int len)
         remove(filename);
     }

-    Z_Free(filename);
+    free(filename);

     return music;
 }
diff --git a/src/i_sdlsound.c b/src/i_sdlsound.c
index b35c83ea..70c360aa 100644
--- a/src/i_sdlsound.c
+++ b/src/i_sdlsound.c
@@ -705,7 +705,8 @@ static boolean CacheSFX(sfxinfo_t *sfxinfo)
     {
         char filename[16];

-        sprintf(filename, "%s.wav", DEH_String(S_sfx[sound].name));
+        snprintf(filename, sizeof(filename), "%s.wav",
+                 DEH_String(S_sfx[sound].name));
         WriteWAV(filename, sound_chunks[sound].abuf,
                  sound_chunks[sound].alen, mixer_freq);
     }
diff --git a/src/i_video.c b/src/i_video.c
index c129d143..823209b8 100644
--- a/src/i_video.c
+++ b/src/i_video.c
@@ -50,6 +50,7 @@
 #include "i_scale.h"
 #include "m_argv.h"
 #include "m_config.h"
+#include "m_misc.h"
 #include "tables.h"
 #include "v_video.h"
 #include "w_wad.h"
@@ -1224,13 +1225,9 @@ void I_InitWindowTitle(void)
 {
     char *buf;

-    buf = Z_Malloc(strlen(window_title) + strlen(PACKAGE_STRING) + 5,
-                   PU_STATIC, NULL);
-    sprintf(buf, "%s - %s", window_title, PACKAGE_STRING);
-
+    buf = M_StringJoin(window_title, " - ", PACKAGE_STRING, NULL);
     SDL_WM_SetCaption(buf, NULL);
-
-    Z_Free(buf);
+    free(buf);
 }

 // Set the application icon
@@ -1801,8 +1798,7 @@ static void SetSDLVideoDriver(void)
     {
         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);
     }
@@ -1824,7 +1820,7 @@ static void SetWindowPositionVars(void)
     }
     else if (sscanf(window_position, "%i,%i", &x, &y) == 2)
     {
-        sprintf(buf, "SDL_VIDEO_WINDOW_POS=%i,%i", x, y);
+        snprintf(buf, sizeof(buf), "SDL_VIDEO_WINDOW_POS=%i,%i", x, y);
         putenv(buf);
     }
 }
@@ -1994,7 +1990,7 @@ void I_InitGraphics(void)
         int winid;

         sscanf(env, "0x%x", &winid);
-        sprintf(winenv, "SDL_WINDOWID=%i", winid);
+        snprintf(winenv, sizeof(winenv), "SDL_WINDOWID=%i", winid);

         putenv(winenv);
     }
diff --git a/src/m_config.c b/src/m_config.c
index b0e201d5..8bfbde9f 100644
--- a/src/m_config.c
+++ b/src/m_config.c
@@ -1812,8 +1812,7 @@ void M_LoadDefaults (void)
     else
     {
         doom_defaults.filename
-            = malloc(strlen(configdir) + strlen(default_main_config) + 1);
-        sprintf(doom_defaults.filename, "%s%s", configdir, default_main_config);
+            = M_StringJoin(configdir, default_main_config, NULL);
     }

     printf("saving config in %s\n", doom_defaults.filename);
@@ -1835,10 +1834,8 @@ void M_LoadDefaults (void)
     }
     else
     {
-        extra_defaults.filename
-            = malloc(strlen(configdir) + strlen(default_extra_config) + 1);
-        sprintf(extra_defaults.filename, "%s%s",
-                configdir, default_extra_config);
+        extra_defaults.filename
+            = M_StringJoin(configdir, default_extra_config, NULL);
     }

     LoadDefaultCollection(&doom_defaults);
@@ -1971,10 +1968,8 @@ static char *GetDefaultConfigDir(void)
         // put all configuration in a config directory off the
         // homedir

-        result = malloc(strlen(homedir) + strlen(PACKAGE_TARNAME) + 5);
-
-        sprintf(result, "%s%c.%s%c", homedir, DIR_SEPARATOR,
-                                     PACKAGE_TARNAME, DIR_SEPARATOR);
+        result = M_StringJoin(homedir, DIR_SEPARATOR_S,
+                              "." PACKAGE_TARNAME, DIR_SEPARATOR_S, NULL);

         return result;
     }
@@ -2023,6 +2018,7 @@ void M_SetConfigDir(char *dir)
 char *M_GetSaveGameDir(char *iwadname)
 {
     char *savegamedir;
+    char *topdir;

     // If not "doing" a configuration directory (Windows), don't "do"
     // a savegame directory, either.
@@ -2033,20 +2029,19 @@ char *M_GetSaveGameDir(char *iwadname)
     }
     else
     {
-        // ~/.chocolate-doom/savegames/
-
-        savegamedir = malloc(strlen(configdir) + 30);
-        sprintf(savegamedir, "%ssavegames%c", configdir,
-                             DIR_SEPARATOR);
+        // ~/.chocolate-doom/savegames

-        M_MakeDirectory(savegamedir);
+        topdir = M_StringJoin(configdir, "savegames", NULL);
+        M_MakeDirectory(topdir);

         // eg. ~/.chocolate-doom/savegames/doom2.wad/

-        sprintf(savegamedir + strlen(savegamedir), "%s%c",
-                iwadname, DIR_SEPARATOR);
+        savegamedir = M_StringJoin(topdir, DIR_SEPARATOR_S, iwadname,
+                                   DIR_SEPARATOR_S, NULL);

         M_MakeDirectory(savegamedir);
+
+        free(topdir);
     }

     return savegamedir;
diff --git a/src/m_controls.c b/src/m_controls.c
index bcc819ea..792c7406 100644
--- a/src/m_controls.c
+++ b/src/m_controls.c
@@ -385,7 +385,7 @@ void M_BindChatControls(unsigned int num_players)

     for (i=0; i<num_players; ++i)
     {
-        sprintf(name, "key_multi_msgplayer%i", i + 1);
+        snprintf(name, sizeof(name), "key_multi_msgplayer%i", i + 1);
         M_BindVariable(name, &key_multi_msgplayer[i]);
     }
 }
diff --git a/src/m_misc.c b/src/m_misc.c
index d48a82e5..15daee3b 100644
--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -174,7 +174,6 @@ int M_ReadFile(char *name, byte **buffer)

 char *M_TempFile(char *s)
 {
-    char *result;
     char *tempdir;

 #ifdef _WIN32
@@ -193,10 +192,7 @@ char *M_TempFile(char *s)
     tempdir = "/tmp";
 #endif

-    result = Z_Malloc(strlen(tempdir) + strlen(s) + 2, PU_STATIC, 0);
-    sprintf(result, "%s%c%s", tempdir, DIR_SEPARATOR, s);
-
-    return result;
+    return M_StringJoin(tempdir, DIR_SEPARATOR_S, s, NULL);
 }

 boolean M_StrToInt(const char *str, int *result)
diff --git a/src/net_gui.c b/src/net_gui.c
index 39bd39fd..41ef7d3b 100644
--- a/src/net_gui.c
+++ b/src/net_gui.c
@@ -101,7 +101,7 @@ static void BuildWindow(void)

     for (i = 0; i < net_client_wait_data.max_players; ++i)
     {
-        sprintf(buf, " %i. ", i + 1);
+        snprintf(buf, sizeof(buf), " %i. ", i + 1);
         TXT_AddWidget(table, TXT_NewLabel(buf));
         player_labels[i] = TXT_NewLabel("");
         ip_labels[i] = TXT_NewLabel("");
@@ -164,8 +164,8 @@ static void UpdateGUI(void)

     if (net_client_wait_data.num_drones > 0)
     {
-        sprintf(buf, " (+%i observer clients)",
-                     net_client_wait_data.num_drones);
+        snprintf(buf, sizeof(buf), " (+%i observer clients)",
+                 net_client_wait_data.num_drones);
         TXT_SetLabel(drone_label, buf);
     }
     else
diff --git a/src/v_video.c b/src/v_video.c
index f37aac86..d8934456 100644
--- a/src/v_video.c
+++ b/src/v_video.c
@@ -818,7 +818,7 @@ void V_ScreenShot(char *format)

     for (i=0; i<=99; i++)
     {
-        sprintf(lbmname, format, i, ext);
+        snprintf(lbmname, sizeof(lbmname), format, i, ext);

         if (!M_FileExists(lbmname))
         {