Merge branch 'drmemory-cleanup' into 'master'

Clean up memory mishandling flagged by DrMemory

See merge request KartKrew/Kart!863
This commit is contained in:
toaster 2023-01-06 12:07:10 +00:00
commit c068013022
7 changed files with 60 additions and 48 deletions

View file

@ -3213,12 +3213,12 @@ void readfollower(MYFILE *f)
if (fastcmp(word, "NAME"))
{
strcpy(followers[numfollowers].name, word2);
strlcpy(followers[numfollowers].name, word2, SKINNAMESIZE+1);
nameset = true;
}
else if (fastcmp(word, "ICON"))
{
strcpy(followers[numfollowers].icon, word2);
strlcpy(followers[numfollowers].icon, word2, 8+1);
nameset = true;
}
else if (fastcmp(word, "CATEGORY"))
@ -3362,30 +3362,26 @@ void readfollower(MYFILE *f)
if (!nameset)
{
// well this is problematic.
strcpy(followers[numfollowers].name, va("Follower%d", numfollowers)); // this is lazy, so what
strlcpy(followers[numfollowers].name, va("Follower%d", numfollowers), SKINNAMESIZE+1);
strcpy(testname, followers[numfollowers].name);
}
// set skin name (this is just the follower's name in lowercases):
// but before we do, let's... actually check if another follower isn't doing the same shit...
res = K_FollowerAvailable(testname);
if (res > -1) // yikes, someone else has stolen our name already
else
{
INT32 startlen = strlen(testname);
char cpy[2];
//deh_warning("There was already a follower with the same name. (%s)", testname); This warning probably isn't necessary anymore?
sprintf(cpy, "%d", numfollowers);
memcpy(&testname[startlen], cpy, 2);
// in that case, we'll be very lazy and copy numfollowers to the end of our skin name.
}
strcpy(testname, followers[numfollowers].name);
strcpy(testname, followers[numfollowers].name);
// now that the skin name is ready, post process the actual name to turn the underscores into spaces!
for (i = 0; followers[numfollowers].name[i]; i++)
{
if (followers[numfollowers].name[i] == '_')
followers[numfollowers].name[i] = ' ';
}
// now that the skin name is ready, post process the actual name to turn the underscores into spaces!
for (i = 0; followers[numfollowers].name[i]; i++)
{
if (followers[numfollowers].name[i] == '_')
followers[numfollowers].name[i] = ' ';
res = K_FollowerAvailable(followers[numfollowers].name);
if (res > -1) // yikes, someone else has stolen our name already
{
deh_warning("Follower%d: Name \"%s\" already in use!", numfollowers, testname);
strlcpy(followers[numfollowers].name, va("Follower%d", numfollowers), SKINNAMESIZE+1);
}
}
// fallbacks for variables

View file

@ -172,6 +172,7 @@ opendir (const CHAR *szPath)
/* Initialize the dirent structure. ino and reclen are invalid under
* Win32, and name simply points at the appropriate part of the
* findfirst_t structure. */
nd->dd_dta = (struct _finddata_t) {0};
nd->dd_dir.d_ino = 0;
nd->dd_dir.d_reclen = 0;
nd->dd_dir.d_namlen = 0;
@ -450,7 +451,7 @@ filestatus_t filesearch(char *filename, const char *startpath, const UINT8 *want
filestatus_t retval = FS_NOTFOUND;
DIR **dirhandle;
struct dirent *dent;
struct stat fsstat;
struct stat fsstat = {0};
int found = 0;
char *searchname = strdup(filename);
int depthleft = maxsearchdepth;
@ -709,7 +710,7 @@ boolean preparefilemenu(boolean samedepth, boolean replayhut)
{
DIR *dirhandle;
struct dirent *dent;
struct stat fsstat;
struct stat fsstat = {0};
size_t pos = 0, folderpos = 0, numfolders = 0;
char *tempname = NULL;

View file

@ -86,7 +86,7 @@ static brightmapStorage_t *K_GetBrightmapStorageByTextureName(const char *checkN
}
/*--------------------------------------------------
static boolean K_BRIGHTLumpParser(UINT8 *data, size_t size)
static boolean K_BRIGHTLumpParser(char *data, size_t size)
Parses inputted lump data as a BRIGHT lump.
@ -97,7 +97,7 @@ static brightmapStorage_t *K_GetBrightmapStorageByTextureName(const char *checkN
Return:-
false if any errors occured, otherwise true.
--------------------------------------------------*/
static boolean K_BRIGHTLumpParser(UINT8 *data, size_t size)
static boolean K_BRIGHTLumpParser(char *data, size_t size)
{
char *tkn = M_GetToken((char *)data);
size_t pos = 0;
@ -188,6 +188,7 @@ void K_InitBrightmapsPwad(INT32 wadNum)
{
lumpinfo_t *lump_p = &wadfiles[wadNum]->lumpinfo[lumpNum];
size_t size = W_LumpLengthPwad(wadNum, lumpNum);
char *datacopy;
size_t nameLength = strlen(wadfiles[wadNum]->filename) + 1 + strlen(lump_p->fullname); // length of file name, '|', and lump name
char *name = malloc(nameLength + 1);
@ -198,10 +199,18 @@ void K_InitBrightmapsPwad(INT32 wadNum)
size = W_LumpLengthPwad(wadNum, lumpNum);
CONS_Printf(M_GetText("Loading BRIGHT from %s\n"), name);
K_BRIGHTLumpParser(data, size);
datacopy = (char *)Z_Malloc((size+1)*sizeof(char),PU_STATIC,NULL);
memmove(datacopy,data,size);
datacopy[size] = '\0';
Z_Free(data);
K_BRIGHTLumpParser(datacopy, size);
Z_Free(datacopy);
free(name);
Z_Free(data);
}
lumpNum = W_CheckNumForNamePwad("BRIGHT", (UINT16)wadNum, lumpNum + 1);

View file

@ -1734,7 +1734,7 @@ static boolean K_DoTERRAINLumpParse(size_t num, void (*parser)(size_t, char *, c
}
/*--------------------------------------------------
static boolean K_TERRAINLumpParser(UINT8 *data, size_t size)
static boolean K_TERRAINLumpParser(char *data, size_t size)
Parses inputted lump data as a TERRAIN lump.
@ -1745,9 +1745,9 @@ static boolean K_DoTERRAINLumpParse(size_t num, void (*parser)(size_t, char *, c
Return:-
false if any errors occured, otherwise true.
--------------------------------------------------*/
static boolean K_TERRAINLumpParser(UINT8 *data, size_t size)
static boolean K_TERRAINLumpParser(char *data, size_t size)
{
char *tkn = M_GetToken((char *)data);
char *tkn = M_GetToken(data);
UINT32 tknHash = 0;
size_t pos = 0;
size_t i;
@ -2136,6 +2136,7 @@ void K_InitTerrain(UINT16 wadNum)
else
{
size_t size = W_LumpLengthPwad(wadNum, lumpNum);
char *datacopy;
size_t nameLength = strlen(wadfiles[wadNum]->filename) + 1 + strlen(lump_p->fullname); // length of file name, '|', and lump name
char *name = malloc(nameLength + 1);
@ -2146,7 +2147,16 @@ void K_InitTerrain(UINT16 wadNum)
size = W_LumpLengthPwad(wadNum, lumpNum);
CONS_Printf(M_GetText("Loading TERRAIN from %s\n"), name);
K_TERRAINLumpParser(data, size);
datacopy = (char *)Z_Malloc((size+1)*sizeof(char),PU_STATIC,NULL);
memmove(datacopy,data,size);
datacopy[size] = '\0';
Z_Free(data);
K_TERRAINLumpParser(datacopy, size);
Z_Free(datacopy);
free(name);
}

View file

@ -1940,14 +1940,14 @@ char *M_GetToken(const char *inputString)
}
// Find the first non-whitespace char, or else the end of the string trying
while ((stringToUse[startPos] == ' '
while (startPos < stringLength
&& (stringToUse[startPos] == ' '
|| stringToUse[startPos] == '\t'
|| stringToUse[startPos] == '\r'
|| stringToUse[startPos] == '\n'
|| stringToUse[startPos] == '\0'
|| stringToUse[startPos] == '=' || stringToUse[startPos] == ';' // UDMF TEXTMAP.
|| inComment != 0)
&& startPos < stringLength)
|| inComment != 0))
{
// Try to detect comment endings now
if (inComment == 1
@ -1988,7 +1988,7 @@ char *M_GetToken(const char *inputString)
}
// If the end of the string is reached, no token is to be read
if (startPos == stringLength) {
if (startPos >= stringLength) {
endPos = stringLength;
return NULL;
}
@ -2007,7 +2007,7 @@ char *M_GetToken(const char *inputString)
else if (stringToUse[startPos] == '"')
{
endPos = ++startPos;
while (stringToUse[endPos] != '"' && endPos < stringLength)
while (endPos < stringLength && stringToUse[endPos] != '"')
endPos++;
texturesTokenLength = endPos++ - startPos;
@ -2023,7 +2023,8 @@ char *M_GetToken(const char *inputString)
// Now find the end of the token. This includes several additional characters that are okay to capture as one character, but not trailing at the end of another token.
endPos = startPos + 1;
while ((stringToUse[endPos] != ' '
while (endPos < stringLength
&& (stringToUse[endPos] != ' '
&& stringToUse[endPos] != '\t'
&& stringToUse[endPos] != '\r'
&& stringToUse[endPos] != '\n'
@ -2031,8 +2032,7 @@ char *M_GetToken(const char *inputString)
&& stringToUse[endPos] != '{'
&& stringToUse[endPos] != '}'
&& stringToUse[endPos] != '=' && stringToUse[endPos] != ';' // UDMF TEXTMAP.
&& inComment == 0)
&& endPos < stringLength)
&& inComment == 0))
{
endPos++;
// Try to detect comment starts now; if it's in a comment, we don't want it in this token

View file

@ -268,8 +268,6 @@ void S_RegisterSoundStuff(void)
static void SetChannelsNum(void)
{
INT32 i;
// Allocating the internal channels for mixing
// (the maximum number of sounds rendered
// simultaneously) within zone memory.
@ -291,12 +289,8 @@ static void SetChannelsNum(void)
}
#endif
if (cv_numChannels.value)
channels = (channel_t *)Z_Malloc(cv_numChannels.value * sizeof (channel_t), PU_STATIC, NULL);
numofchannels = cv_numChannels.value;
// Free all channels for use
for (i = 0; i < numofchannels; i++)
channels[i].sfxinfo = 0;
channels = (channel_t *)Z_Calloc(cv_numChannels.value * sizeof (channel_t), PU_STATIC, NULL);
numofchannels = (channels ? cv_numChannels.value : 0);
S_ResetCaptions();
}

View file

@ -476,6 +476,8 @@ void V_SetPaletteLump(const char *pal)
static void CV_palette_OnChange(void)
{
if (con_startup_loadprogress < LOADED_CONFIG)
return;
// recalculate Color Cube
V_ReloadPalette();
V_SetPalette(0);