From 3deb8d1d99bdd1a433b2a14340b72633bb2d8032 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Wed, 28 Mar 2012 19:44:00 +0000 Subject: [PATCH] Clear up handling of g_*NamePtr in the game. The primary change is that things have been made memory-clean. Some of these pointers may point to wildly different places during the course of the program such as statically or dynamically allocated storage, the buffer returned by getenv() (which must not be modified according to the docs), or an element of argv[]. Consequently, we need to strdup, or better, dup_filename them if they are ever to be passed to a function that modifies their pointed-to data. Specifically: - added statics or consts according to usage - 3 new functions clear{Grp,Def,Script}NamePtr, only 'Def' one extern for now - in G_CheckCommandLine, don't strip 'const'; use Bstrncpyz where appropriate - remove multiple declarations Also, warn if an application parameter has been ignored (not matched). git-svn-id: https://svn.eduke32.com/eduke32@2561 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/config.c | 1 - polymer/eduke32/source/game.c | 112 ++++++++++++++++++------- polymer/eduke32/source/game.h | 8 +- polymer/eduke32/source/gamedef.c | 9 +- polymer/eduke32/source/premap.c | 1 - polymer/eduke32/source/premap.h | 1 - polymer/eduke32/source/startgtk.game.c | 2 +- polymer/eduke32/source/startwin.game.c | 2 +- 8 files changed, 93 insertions(+), 43 deletions(-) diff --git a/polymer/eduke32/source/config.c b/polymer/eduke32/source/config.c index 8b9ce0b06..acde021df 100644 --- a/polymer/eduke32/source/config.c +++ b/polymer/eduke32/source/config.c @@ -600,7 +600,6 @@ void CONFIG_SetupJoystick(void) = =================== */ -extern char *g_grpNamePtr; extern void G_CheckPlayerColor(int32_t *color,int32_t prev_color); extern palette_t CrosshairColors; extern palette_t DefaultCrosshairColors; diff --git a/polymer/eduke32/source/game.c b/polymer/eduke32/source/game.c index 02db35a84..0be048f6d 100644 --- a/polymer/eduke32/source/game.c +++ b/polymer/eduke32/source/game.c @@ -88,8 +88,8 @@ int32_t g_noSetup = 0; static int32_t g_noAutoLoad = 0; static int32_t g_noSound = 0; static int32_t g_noMusic = 0; -static char *CommandMap = NULL; -static char *CommandName = NULL; +static const char *CommandMap = NULL; +static const char *CommandName = NULL; int32_t g_forceWeaponChoice = 0; char boardfilename[BMAX_PATH] = {0}, currentboardfilename[BMAX_PATH] = {0}; @@ -117,13 +117,45 @@ static int32_t g_noLogo = 0; char defaultduke3dgrp[BMAX_PATH] = "duke3d.grp"; char defsfilename[BMAX_PATH] = "duke3d.def"; static char defaultconfilename[2][BMAX_PATH] = { "EDUKE.CON", "GAME.CON" }; -char defaultrtsfilename[128] = "DUKE.RTS"; +const char *defaultrtsfilename = "DUKE.RTS"; +// g_defNamePtr can ONLY point to one of: defaultduke3dgrp, +// settings[].selectedgrp, malloc'd block (all length BMAX_PATH) char *g_grpNamePtr = defaultduke3dgrp; +// g_defNamePtr can ONLY point to one of: defsfilename, malloc'd block (all +// length BMAX_PATH) char *g_defNamePtr = defsfilename; -char *g_scriptNamePtr = defaultconfilename[0]; -char *g_gameNamePtr = NULL; -char *g_rtsNamePtr = NULL; +// g_scriptNamePtr can ONLY point to defaultconfilename[0] OR to malloc'd block +// (all length BMAX_PATH) +static char *g_scriptNamePtr = defaultconfilename[0]; +// g_gameNamePtr can point to one of: grpfiles[].name (string literal), string +// literal, malloc'd block (XXX: possible leak) +const char *g_gameNamePtr = NULL; +// g_rtsNamePtr can point to an argv[] element +const char *g_rtsNamePtr = NULL; + +// FIXME: I am leaking! +// FIXME: make me extern! +static void clearGrpNamePtr(void) +{ +// if (g_grpNamePtr != defaultduke3dgrp) && not one of settings[].selectedgrp +// Bfree(g_grpNamePtr); + // g_grpNamePtr assumed to be assigned to right after +} + +void clearDefNamePtr(void) +{ + if (g_defNamePtr != defsfilename) + Bfree(g_defNamePtr); + // g_defNamePtr assumed to be assigned to right after +} + +static void clearScriptNamePtr(void) +{ + if (g_scriptNamePtr != defaultconfilename[0]) + Bfree(g_scriptNamePtr); + // g_scriptNamePtr assumed to be assigned to right after +} char **g_scriptModules = NULL; int32_t g_scriptModulesNum = 0; @@ -8466,7 +8498,7 @@ static int32_t loaddefinitions_game(const char *fn, int32_t preload) static void G_CheckCommandLine(int32_t argc, const char **argv) { int16_t i = 1, j; - char *c, *k; + const char *c, *k; ud.fta_on = 1; ud.god = 0; @@ -8510,7 +8542,10 @@ static void G_CheckCommandLine(int32_t argc, const char **argv) i = 1; do { - c = (char *)argv[i]; + const char *const oc = argv[i]; + + c = oc; + if ((*c == '-') #ifdef _WIN32 || (*c == '/') @@ -8675,7 +8710,7 @@ static void G_CheckCommandLine(int32_t argc, const char **argv) { if (argc > i+1) { - CommandName = (char *)argv[i+1]; + CommandName = argv[i+1]; i++; } i++; @@ -8685,7 +8720,7 @@ static void G_CheckCommandLine(int32_t argc, const char **argv) { if (argc > i+1) { - CommandMap = (char *)argv[i+1]; + CommandMap = argv[i+1]; i++; } i++; @@ -8695,9 +8730,9 @@ static void G_CheckCommandLine(int32_t argc, const char **argv) { if (argc > i+1) { - g_rtsNamePtr = (char *)argv[i+1]; - Bstrcpy(ud.rtsname, g_rtsNamePtr); - initprintf("Using RTS file \"%s\".\n",ud.rtsname); + g_rtsNamePtr = argv[i+1]; + Bstrncpyz(ud.rtsname, g_rtsNamePtr, sizeof(ud.rtsname)); + initprintf("Using RTS file \"%s\".\n", ud.rtsname); i++; } i++; @@ -8850,7 +8885,8 @@ static void G_CheckCommandLine(int32_t argc, const char **argv) c++; if (*c) { - g_defNamePtr = c; + clearDefNamePtr(); + g_defNamePtr = dup_filename(c); g_skipDefaultDefs = 1; initprintf("Using DEF file \"%s\".\n",g_defNamePtr); } @@ -8974,7 +9010,8 @@ static void G_CheckCommandLine(int32_t argc, const char **argv) c++; if (*c) { - g_scriptNamePtr = c; + clearScriptNamePtr(); + g_scriptNamePtr = dup_filename(c); g_skipDefaultCons = 1; initprintf("Using CON file \"%s\".\n",g_scriptNamePtr); } @@ -9006,7 +9043,7 @@ static void G_CheckCommandLine(int32_t argc, const char **argv) { if (!Bstrcasecmp(k,".map")) { - CommandMap = (char *)argv[i++]; + CommandMap = argv[i++]; continue; } if (!Bstrcasecmp(k,".grp") || !Bstrcasecmp(k,".zip") || !Bstrcasecmp(k,".pk3")) @@ -9016,27 +9053,32 @@ static void G_CheckCommandLine(int32_t argc, const char **argv) } if (!Bstrcasecmp(k,".con")) { - g_scriptNamePtr = (char *)argv[i++]; + clearScriptNamePtr(); + g_scriptNamePtr = dup_filename(argv[i++]); g_skipDefaultCons = 1; initprintf("Using CON file \"%s\".\n",g_scriptNamePtr); continue; } if (!Bstrcasecmp(k,".def")) { - g_defNamePtr = (char *)argv[i++]; + clearDefNamePtr(); + g_defNamePtr = dup_filename(argv[i++]); g_skipDefaultDefs = 1; initprintf("Using DEF file \"%s\".\n",g_defNamePtr); continue; } if (!Bstrcasecmp(k,".rts")) { - g_rtsNamePtr = (char *)argv[i++]; - Bstrcpy(ud.rtsname, g_rtsNamePtr); - initprintf("Using RTS file \"%s\".\n",ud.rtsname); + g_rtsNamePtr = argv[i++]; + Bstrncpyz(ud.rtsname, g_rtsNamePtr, sizeof(ud.rtsname)); + initprintf("Using RTS file \"%s\".\n", ud.rtsname); continue; } } } + + initprintf("Warning: ignored application parameter \"%s\".\n", oc); + i++; } while (i < argc); @@ -9835,10 +9877,14 @@ int32_t app_main(int32_t argc,const char **argv) i = CONFIG_ReadSetup(); - if (getenv("DUKE3DGRP")) { - g_grpNamePtr = getenv("DUKE3DGRP"); - initprintf("Using \"%s\" as main GRP file\n", g_grpNamePtr); + const char *cp = getenv("DUKE3DGRP"); + if (cp) + { + clearGrpNamePtr(); + g_grpNamePtr = dup_filename(cp); + initprintf("Using \"%s\" as main GRP file\n", g_grpNamePtr); + } } #ifdef _WIN32 @@ -9917,7 +9963,7 @@ int32_t app_main(int32_t argc,const char **argv) if (!Bstrcasecmp(fg->name, defaultduke3dgrp)) { g_gameType = grpfiles[i].game; - g_gameNamePtr = (char *)grpfiles[i].name; + g_gameNamePtr = grpfiles[i].name; break; } } @@ -9925,7 +9971,7 @@ int32_t app_main(int32_t argc,const char **argv) { Bstrcpy(defaultduke3dgrp, first->name); g_gameType = first->game; - g_gameNamePtr = (char *)grpfiles[0].name; + g_gameNamePtr = grpfiles[0].name; } else if (!fg) g_gameNamePtr = "Unknown GRP"; } @@ -10076,12 +10122,18 @@ CLEAN_DIRECTORY: if (g_modDir[0] != '/') G_LoadGroupsInDir(g_modDir); - if (getenv("DUKE3DDEF")) + // CODEDUP astub.c { - g_defNamePtr = getenv("DUKE3DDEF"); - g_skipDefaultDefs = 1; - initprintf("Using \"%s\" as definitions file\n", g_defNamePtr); + const char *tmpptr = getenv("DUKE3DDEF"); + if (tmpptr) + { + clearDefNamePtr(); + g_defNamePtr = dup_filename(tmpptr); + g_skipDefaultDefs = 1; + initprintf("Using \"%s\" as definitions file\n", g_defNamePtr); + } } + if (g_skipDefaultDefs == 0) if (g_defNamePtr != defsfilename) Bstrcpy(g_defNamePtr, defsfilename); // it MAY have changed, with NAM/WWII GI diff --git a/polymer/eduke32/source/game.h b/polymer/eduke32/source/game.h index e20104d2e..414154708 100644 --- a/polymer/eduke32/source/game.h +++ b/polymer/eduke32/source/game.h @@ -180,16 +180,15 @@ extern cactype cac[]; extern const char *s_buildDate; extern char *g_grpNamePtr; extern char *g_defNamePtr; -extern char *g_scriptNamePtr; -extern char *g_gameNamePtr; -extern char *g_rtsNamePtr; +extern const char *g_gameNamePtr; +extern const char *g_rtsNamePtr; extern char **g_scriptModules; extern int32_t g_scriptModulesNum; extern char CheatStrings[][MAXCHEATLEN]; extern char boardfilename[BMAX_PATH], currentboardfilename[BMAX_PATH]; extern char boardfilename[BMAX_PATH]; extern char defaultduke3dgrp[BMAX_PATH]; -extern char defaultrtsfilename[128]; +extern const char *defaultrtsfilename; extern char g_modDir[BMAX_PATH]; extern char g_modDir[BMAX_PATH]; extern char inputloc; @@ -242,6 +241,7 @@ extern int8_t g_noFloorPal[MAXPALOOKUPS]; extern user_defs ud; //extern int32_t g_yax_smoothratio; +void clearDefNamePtr(void); int32_t A_CheckInventorySprite(spritetype *s); int32_t A_InsertSprite(int32_t whatsect,int32_t s_x,int32_t s_y,int32_t s_z,int32_t s_pn,int32_t s_s,int32_t s_xr,int32_t s_yr,int32_t s_a,int32_t s_ve,int32_t s_zv,int32_t s_ow,int32_t s_ss); diff --git a/polymer/eduke32/source/gamedef.c b/polymer/eduke32/source/gamedef.c index 188804b01..2ad9000bf 100644 --- a/polymer/eduke32/source/gamedef.c +++ b/polymer/eduke32/source/gamedef.c @@ -25,6 +25,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "gamedef.h" #include "gameexec.h" #include "savegame.h" +#include "common.h" #include "osd.h" @@ -160,9 +161,6 @@ extern int32_t qsetmode; char *textptr; int32_t g_numCompilerErrors,g_numCompilerWarnings; -extern char *g_gameNamePtr; -extern char *g_defNamePtr; - extern int32_t g_maxSoundPos; enum @@ -5003,7 +5001,10 @@ repeatcase: j++; } tempbuf[j] = '\0'; - g_defNamePtr = Bstrdup(tempbuf); + + clearDefNamePtr(); + g_defNamePtr = dup_filename(tempbuf); + initprintf("Using DEF file: %s.\n",g_defNamePtr); } continue; diff --git a/polymer/eduke32/source/premap.c b/polymer/eduke32/source/premap.c index d83370c47..d8928154e 100644 --- a/polymer/eduke32/source/premap.c +++ b/polymer/eduke32/source/premap.c @@ -48,7 +48,6 @@ int16_t SpriteCacheList[MAXTILES][3]; static uint8_t precachehightile[2][MAXTILES>>3]; static int32_t g_precacheCount; -extern char *g_gameNamePtr; extern int32_t g_levelTextTime; static void tloadtile(int32_t tilenume, int32_t type) diff --git a/polymer/eduke32/source/premap.h b/polymer/eduke32/source/premap.h index b2b283f7c..d71701835 100644 --- a/polymer/eduke32/source/premap.h +++ b/polymer/eduke32/source/premap.h @@ -23,7 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #ifndef __premap_h__ #define __premap_h__ -extern char *g_gameNamePtr; extern const char pow2char[]; extern int16_t SpriteCacheList[MAXTILES][3]; diff --git a/polymer/eduke32/source/startgtk.game.c b/polymer/eduke32/source/startgtk.game.c index 31dcaadc7..992797f45 100644 --- a/polymer/eduke32/source/startgtk.game.c +++ b/polymer/eduke32/source/startgtk.game.c @@ -920,7 +920,7 @@ int32_t startwin_run(void) for (i = 0; i