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
This commit is contained in:
helixhorned 2012-03-28 19:44:00 +00:00
parent 77918caf9c
commit 3deb8d1d99
8 changed files with 93 additions and 43 deletions

View file

@ -600,7 +600,6 @@ void CONFIG_SetupJoystick(void)
= =
=================== ===================
*/ */
extern char *g_grpNamePtr;
extern void G_CheckPlayerColor(int32_t *color,int32_t prev_color); extern void G_CheckPlayerColor(int32_t *color,int32_t prev_color);
extern palette_t CrosshairColors; extern palette_t CrosshairColors;
extern palette_t DefaultCrosshairColors; extern palette_t DefaultCrosshairColors;

View file

@ -88,8 +88,8 @@ int32_t g_noSetup = 0;
static int32_t g_noAutoLoad = 0; static int32_t g_noAutoLoad = 0;
static int32_t g_noSound = 0; static int32_t g_noSound = 0;
static int32_t g_noMusic = 0; static int32_t g_noMusic = 0;
static char *CommandMap = NULL; static const char *CommandMap = NULL;
static char *CommandName = NULL; static const char *CommandName = NULL;
int32_t g_forceWeaponChoice = 0; int32_t g_forceWeaponChoice = 0;
char boardfilename[BMAX_PATH] = {0}, currentboardfilename[BMAX_PATH] = {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 defaultduke3dgrp[BMAX_PATH] = "duke3d.grp";
char defsfilename[BMAX_PATH] = "duke3d.def"; char defsfilename[BMAX_PATH] = "duke3d.def";
static char defaultconfilename[2][BMAX_PATH] = { "EDUKE.CON", "GAME.CON" }; 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; 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_defNamePtr = defsfilename;
char *g_scriptNamePtr = defaultconfilename[0]; // g_scriptNamePtr can ONLY point to defaultconfilename[0] OR to malloc'd block
char *g_gameNamePtr = NULL; // (all length BMAX_PATH)
char *g_rtsNamePtr = NULL; 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; char **g_scriptModules = NULL;
int32_t g_scriptModulesNum = 0; 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) static void G_CheckCommandLine(int32_t argc, const char **argv)
{ {
int16_t i = 1, j; int16_t i = 1, j;
char *c, *k; const char *c, *k;
ud.fta_on = 1; ud.fta_on = 1;
ud.god = 0; ud.god = 0;
@ -8510,7 +8542,10 @@ static void G_CheckCommandLine(int32_t argc, const char **argv)
i = 1; i = 1;
do do
{ {
c = (char *)argv[i]; const char *const oc = argv[i];
c = oc;
if ((*c == '-') if ((*c == '-')
#ifdef _WIN32 #ifdef _WIN32
|| (*c == '/') || (*c == '/')
@ -8675,7 +8710,7 @@ static void G_CheckCommandLine(int32_t argc, const char **argv)
{ {
if (argc > i+1) if (argc > i+1)
{ {
CommandName = (char *)argv[i+1]; CommandName = argv[i+1];
i++; i++;
} }
i++; i++;
@ -8685,7 +8720,7 @@ static void G_CheckCommandLine(int32_t argc, const char **argv)
{ {
if (argc > i+1) if (argc > i+1)
{ {
CommandMap = (char *)argv[i+1]; CommandMap = argv[i+1];
i++; i++;
} }
i++; i++;
@ -8695,9 +8730,9 @@ static void G_CheckCommandLine(int32_t argc, const char **argv)
{ {
if (argc > i+1) if (argc > i+1)
{ {
g_rtsNamePtr = (char *)argv[i+1]; g_rtsNamePtr = argv[i+1];
Bstrcpy(ud.rtsname, g_rtsNamePtr); Bstrncpyz(ud.rtsname, g_rtsNamePtr, sizeof(ud.rtsname));
initprintf("Using RTS file \"%s\".\n",ud.rtsname); initprintf("Using RTS file \"%s\".\n", ud.rtsname);
i++; i++;
} }
i++; i++;
@ -8850,7 +8885,8 @@ static void G_CheckCommandLine(int32_t argc, const char **argv)
c++; c++;
if (*c) if (*c)
{ {
g_defNamePtr = c; clearDefNamePtr();
g_defNamePtr = dup_filename(c);
g_skipDefaultDefs = 1; g_skipDefaultDefs = 1;
initprintf("Using DEF file \"%s\".\n",g_defNamePtr); initprintf("Using DEF file \"%s\".\n",g_defNamePtr);
} }
@ -8974,7 +9010,8 @@ static void G_CheckCommandLine(int32_t argc, const char **argv)
c++; c++;
if (*c) if (*c)
{ {
g_scriptNamePtr = c; clearScriptNamePtr();
g_scriptNamePtr = dup_filename(c);
g_skipDefaultCons = 1; g_skipDefaultCons = 1;
initprintf("Using CON file \"%s\".\n",g_scriptNamePtr); 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")) if (!Bstrcasecmp(k,".map"))
{ {
CommandMap = (char *)argv[i++]; CommandMap = argv[i++];
continue; continue;
} }
if (!Bstrcasecmp(k,".grp") || !Bstrcasecmp(k,".zip") || !Bstrcasecmp(k,".pk3")) 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")) if (!Bstrcasecmp(k,".con"))
{ {
g_scriptNamePtr = (char *)argv[i++]; clearScriptNamePtr();
g_scriptNamePtr = dup_filename(argv[i++]);
g_skipDefaultCons = 1; g_skipDefaultCons = 1;
initprintf("Using CON file \"%s\".\n",g_scriptNamePtr); initprintf("Using CON file \"%s\".\n",g_scriptNamePtr);
continue; continue;
} }
if (!Bstrcasecmp(k,".def")) if (!Bstrcasecmp(k,".def"))
{ {
g_defNamePtr = (char *)argv[i++]; clearDefNamePtr();
g_defNamePtr = dup_filename(argv[i++]);
g_skipDefaultDefs = 1; g_skipDefaultDefs = 1;
initprintf("Using DEF file \"%s\".\n",g_defNamePtr); initprintf("Using DEF file \"%s\".\n",g_defNamePtr);
continue; continue;
} }
if (!Bstrcasecmp(k,".rts")) if (!Bstrcasecmp(k,".rts"))
{ {
g_rtsNamePtr = (char *)argv[i++]; g_rtsNamePtr = argv[i++];
Bstrcpy(ud.rtsname, g_rtsNamePtr); Bstrncpyz(ud.rtsname, g_rtsNamePtr, sizeof(ud.rtsname));
initprintf("Using RTS file \"%s\".\n",ud.rtsname); initprintf("Using RTS file \"%s\".\n", ud.rtsname);
continue; continue;
} }
} }
} }
initprintf("Warning: ignored application parameter \"%s\".\n", oc);
i++; i++;
} }
while (i < argc); while (i < argc);
@ -9835,10 +9877,14 @@ int32_t app_main(int32_t argc,const char **argv)
i = CONFIG_ReadSetup(); i = CONFIG_ReadSetup();
if (getenv("DUKE3DGRP"))
{ {
g_grpNamePtr = getenv("DUKE3DGRP"); const char *cp = getenv("DUKE3DGRP");
initprintf("Using \"%s\" as main GRP file\n", g_grpNamePtr); if (cp)
{
clearGrpNamePtr();
g_grpNamePtr = dup_filename(cp);
initprintf("Using \"%s\" as main GRP file\n", g_grpNamePtr);
}
} }
#ifdef _WIN32 #ifdef _WIN32
@ -9917,7 +9963,7 @@ int32_t app_main(int32_t argc,const char **argv)
if (!Bstrcasecmp(fg->name, defaultduke3dgrp)) if (!Bstrcasecmp(fg->name, defaultduke3dgrp))
{ {
g_gameType = grpfiles[i].game; g_gameType = grpfiles[i].game;
g_gameNamePtr = (char *)grpfiles[i].name; g_gameNamePtr = grpfiles[i].name;
break; break;
} }
} }
@ -9925,7 +9971,7 @@ int32_t app_main(int32_t argc,const char **argv)
{ {
Bstrcpy(defaultduke3dgrp, first->name); Bstrcpy(defaultduke3dgrp, first->name);
g_gameType = first->game; g_gameType = first->game;
g_gameNamePtr = (char *)grpfiles[0].name; g_gameNamePtr = grpfiles[0].name;
} }
else if (!fg) g_gameNamePtr = "Unknown GRP"; else if (!fg) g_gameNamePtr = "Unknown GRP";
} }
@ -10076,12 +10122,18 @@ CLEAN_DIRECTORY:
if (g_modDir[0] != '/') if (g_modDir[0] != '/')
G_LoadGroupsInDir(g_modDir); G_LoadGroupsInDir(g_modDir);
if (getenv("DUKE3DDEF")) // CODEDUP astub.c
{ {
g_defNamePtr = getenv("DUKE3DDEF"); const char *tmpptr = getenv("DUKE3DDEF");
g_skipDefaultDefs = 1; if (tmpptr)
initprintf("Using \"%s\" as definitions file\n", g_defNamePtr); {
clearDefNamePtr();
g_defNamePtr = dup_filename(tmpptr);
g_skipDefaultDefs = 1;
initprintf("Using \"%s\" as definitions file\n", g_defNamePtr);
}
} }
if (g_skipDefaultDefs == 0) if (g_skipDefaultDefs == 0)
if (g_defNamePtr != defsfilename) if (g_defNamePtr != defsfilename)
Bstrcpy(g_defNamePtr, defsfilename); // it MAY have changed, with NAM/WWII GI Bstrcpy(g_defNamePtr, defsfilename); // it MAY have changed, with NAM/WWII GI

View file

@ -180,16 +180,15 @@ extern cactype cac[];
extern const char *s_buildDate; extern const char *s_buildDate;
extern char *g_grpNamePtr; extern char *g_grpNamePtr;
extern char *g_defNamePtr; extern char *g_defNamePtr;
extern char *g_scriptNamePtr; extern const char *g_gameNamePtr;
extern char *g_gameNamePtr; extern const char *g_rtsNamePtr;
extern char *g_rtsNamePtr;
extern char **g_scriptModules; extern char **g_scriptModules;
extern int32_t g_scriptModulesNum; extern int32_t g_scriptModulesNum;
extern char CheatStrings[][MAXCHEATLEN]; extern char CheatStrings[][MAXCHEATLEN];
extern char boardfilename[BMAX_PATH], currentboardfilename[BMAX_PATH]; extern char boardfilename[BMAX_PATH], currentboardfilename[BMAX_PATH];
extern char boardfilename[BMAX_PATH]; extern char boardfilename[BMAX_PATH];
extern char defaultduke3dgrp[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 g_modDir[BMAX_PATH]; extern char g_modDir[BMAX_PATH];
extern char inputloc; extern char inputloc;
@ -242,6 +241,7 @@ extern int8_t g_noFloorPal[MAXPALOOKUPS];
extern user_defs ud; extern user_defs ud;
//extern int32_t g_yax_smoothratio; //extern int32_t g_yax_smoothratio;
void clearDefNamePtr(void);
int32_t A_CheckInventorySprite(spritetype *s); 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); 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);

View file

@ -25,6 +25,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "gamedef.h" #include "gamedef.h"
#include "gameexec.h" #include "gameexec.h"
#include "savegame.h" #include "savegame.h"
#include "common.h"
#include "osd.h" #include "osd.h"
@ -160,9 +161,6 @@ extern int32_t qsetmode;
char *textptr; char *textptr;
int32_t g_numCompilerErrors,g_numCompilerWarnings; int32_t g_numCompilerErrors,g_numCompilerWarnings;
extern char *g_gameNamePtr;
extern char *g_defNamePtr;
extern int32_t g_maxSoundPos; extern int32_t g_maxSoundPos;
enum enum
@ -5003,7 +5001,10 @@ repeatcase:
j++; j++;
} }
tempbuf[j] = '\0'; tempbuf[j] = '\0';
g_defNamePtr = Bstrdup(tempbuf);
clearDefNamePtr();
g_defNamePtr = dup_filename(tempbuf);
initprintf("Using DEF file: %s.\n",g_defNamePtr); initprintf("Using DEF file: %s.\n",g_defNamePtr);
} }
continue; continue;

View file

@ -48,7 +48,6 @@ int16_t SpriteCacheList[MAXTILES][3];
static uint8_t precachehightile[2][MAXTILES>>3]; static uint8_t precachehightile[2][MAXTILES>>3];
static int32_t g_precacheCount; static int32_t g_precacheCount;
extern char *g_gameNamePtr;
extern int32_t g_levelTextTime; extern int32_t g_levelTextTime;
static void tloadtile(int32_t tilenume, int32_t type) static void tloadtile(int32_t tilenume, int32_t type)

View file

@ -23,7 +23,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#ifndef __premap_h__ #ifndef __premap_h__
#define __premap_h__ #define __premap_h__
extern char *g_gameNamePtr;
extern const char pow2char[]; extern const char pow2char[];
extern int16_t SpriteCacheList[MAXTILES][3]; extern int16_t SpriteCacheList[MAXTILES][3];

View file

@ -920,7 +920,7 @@ int32_t startwin_run(void)
for (i = 0; i<NUMGRPFILES; i++) if (settings.crcval == grpfiles[i].crcval) break; for (i = 0; i<NUMGRPFILES; i++) if (settings.crcval == grpfiles[i].crcval) break;
if (i != NUMGRPFILES) if (i != NUMGRPFILES)
g_gameNamePtr = (char *)grpfiles[i].name; g_gameNamePtr = grpfiles[i].name;
} }
return retval; return retval;

View file

@ -784,7 +784,7 @@ int32_t startwin_run(void)
for (i = 0; i<NUMGRPFILES; i++) if (settings.crcval == grpfiles[i].crcval) break; for (i = 0; i<NUMGRPFILES; i++) if (settings.crcval == grpfiles[i].crcval) break;
if (i != NUMGRPFILES) if (i != NUMGRPFILES)
g_gameNamePtr = (char *)grpfiles[i].name; g_gameNamePtr = grpfiles[i].name;
} }
if (wavedevs) if (wavedevs)