ref: 71d8ef248f4f1a05646f5e1e7e6388a59f08b121
parent: 724c4ad7a4f2a541eff157d9196b3835eb4bc8d8
author: Simon Howard <fraggle@gmail.com>
date: Sun Mar 30 14:07:52 EDT 2014
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.
--- a/src/d_iwad.c
+++ b/src/d_iwad.c
@@ -289,12 +289,9 @@
for (i=0; i<arrlen(collectors_edition_subdirs); ++i)
{
- subpath = malloc(strlen(install_path)
- + strlen(collectors_edition_subdirs[i])
- + 5);
+ subpath = M_StringJoin(install_path, DIR_SEPARATOR_S,
+ collectors_edition_subdirs[i], NULL);
- sprintf(subpath, "%s\\%s", install_path, collectors_edition_subdirs[i]);
-
AddIWADDir(subpath);
}
@@ -319,11 +316,9 @@
for (i=0; i<arrlen(steam_install_subdirs); ++i)
{
- subpath = malloc(strlen(install_path)
- + strlen(steam_install_subdirs[i]) + 5);
+ subpath = M_StringJoin(install_path, DIR_SEPARATOR_S,
+ steam_install_subdirs[i], NULL);
- sprintf(subpath, "%s\\%s", install_path, steam_install_subdirs[i]);
-
AddIWADDir(subpath);
}
@@ -438,8 +433,7 @@
}
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 @@
char *D_FindWADByName(char *name)
{
- char *buf;
+ char *path;
int i;
// Absolute path?
@@ -643,7 +637,7 @@
}
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 @@
// 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
--- a/src/doom/m_menu.c
+++ b/src/doom/m_menu.c
@@ -1932,12 +1932,12 @@
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;
--- a/src/doomtype.h
+++ b/src/doomtype.h
@@ -97,11 +97,13 @@
#ifdef _WIN32
#define DIR_SEPARATOR '\\'
+#define DIR_SEPARATOR_S "\\"
#define PATH_SEPARATOR ';'
#else
#define DIR_SEPARATOR '/'
+#define DIR_SEPARATOR_S "/"
#define PATH_SEPARATOR ':'
#endif
--- a/src/i_oplmusic.c
+++ b/src/i_oplmusic.c
@@ -1376,7 +1376,7 @@
{
M_WriteFile(filename, data, len);
}
- else
+ else
{
// Assume a MUS file and try to convert
@@ -1393,9 +1393,8 @@
// remove file now
remove(filename);
+ free(filename);
- Z_Free(filename);
-
return result;
}
@@ -1519,8 +1518,9 @@
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;
@@ -1527,11 +1527,11 @@
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 @@
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 @@
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;
--- a/src/i_sdlmusic.c
+++ b/src/i_sdlmusic.c
@@ -116,13 +116,12 @@
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 @@
remove(filename);
}
- Z_Free(filename);
+ free(filename);
return music;
}
--- a/src/i_sdlsound.c
+++ b/src/i_sdlsound.c
@@ -705,7 +705,8 @@
{
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);
}
--- 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 @@
{
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 @@
{
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 @@
}
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 @@
int winid;
sscanf(env, "0x%x", &winid);
- sprintf(winenv, "SDL_WINDOWID=%i", winid);
+ snprintf(winenv, sizeof(winenv), "SDL_WINDOWID=%i", winid);
putenv(winenv);
}
--- a/src/m_config.c
+++ b/src/m_config.c
@@ -1812,8 +1812,7 @@
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 @@
}
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,11 +1968,9 @@
// put all configuration in a config directory off the
// homedir
- result = malloc(strlen(homedir) + strlen(PACKAGE_TARNAME) + 5);
+ result = M_StringJoin(homedir, DIR_SEPARATOR_S,
+ "." PACKAGE_TARNAME, DIR_SEPARATOR_S, NULL);
- sprintf(result, "%s%c.%s%c", homedir, DIR_SEPARATOR,
- PACKAGE_TARNAME, DIR_SEPARATOR);
-
return result;
}
else
@@ -2023,6 +2018,7 @@
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 @@
}
else
{
- // ~/.chocolate-doom/savegames/
+ // ~/.chocolate-doom/savegames
- savegamedir = malloc(strlen(configdir) + 30);
- sprintf(savegamedir, "%ssavegames%c", configdir,
- DIR_SEPARATOR);
+ topdir = M_StringJoin(configdir, "savegames", NULL);
+ M_MakeDirectory(topdir);
- M_MakeDirectory(savegamedir);
-
// 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;
--- a/src/m_controls.c
+++ b/src/m_controls.c
@@ -385,7 +385,7 @@
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]);
}
}
--- a/src/m_misc.c
+++ b/src/m_misc.c
@@ -174,7 +174,6 @@
char *M_TempFile(char *s)
{
- char *result;
char *tempdir;
#ifdef _WIN32
@@ -193,10 +192,7 @@
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)
--- a/src/net_gui.c
+++ b/src/net_gui.c
@@ -101,7 +101,7 @@
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 @@
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
--- a/src/v_video.c
+++ b/src/v_video.c
@@ -818,7 +818,7 @@
for (i=0; i<=99; i++)
{
- sprintf(lbmname, format, i, ext);
+ snprintf(lbmname, sizeof(lbmname), format, i, ext);
if (!M_FileExists(lbmname))
{