YQ2_COM_CHECK_OOM() macro to check if malloc() etc was successful

This commit is contained in:
Daniel Gibson 2019-08-21 21:07:31 -01:00
parent 95983f08cf
commit bebd9e73b3
6 changed files with 22 additions and 48 deletions

View file

@ -974,10 +974,7 @@ void CL_SetHTTPServer (const char *URL)
// Remove trailing / from URL if any.
size_t urllen = strlen(URL);
char *cleanURL = strdup(URL);
if(cleanURL == NULL)
{
Sys_Error("strdup(URL) failed: out of memory (in %s())!\n", __func__);
}
YQ2_COM_CHECK_OOM(cleanURL, "strdup(URL)", strlen(URL))
if (cleanURL[urllen - 1] == '/')
{
@ -1073,10 +1070,7 @@ qboolean CL_QueueHTTPDownload(const char *quakePath, qboolean gamedirForFilelist
q->next = malloc(sizeof(*q));
if(q->next == NULL)
{
Sys_Error("malloc(sizeof(*q)) failed, out of memory (in %s())\n", __func__);
}
YQ2_COM_CHECK_OOM(q->next, "malloc(sizeof(*q))", sizeof(*q))
q = q->next;
q->next = NULL;

View file

@ -2973,10 +2973,8 @@ StartServer_MenuInit(void)
}
mapnames = malloc(sizeof(char *) * (nummaps + 1));
if(mapnames == NULL)
{
Sys_Error("malloc(sizeof(char *) * (nummaps + 1)) failed in %s()!\n", __func__);
}
YQ2_COM_CHECK_OOM(mapnames, "malloc(sizeof(char *) * (nummaps + 1))", sizeof(char *) * (nummaps + 1))
memset(mapnames, 0, sizeof(char *) * (nummaps + 1));
@ -3001,10 +2999,7 @@ StartServer_MenuInit(void)
Com_sprintf(scratch, sizeof(scratch), "%s\n%s", longname, shortname);
mapnames[i] = malloc(strlen(scratch) + 1);
if(mapnames == NULL)
{
Sys_Error("malloc(strlen(scratch) + 1) failed in %s()!\n", __func__);
}
YQ2_COM_CHECK_OOM(mapnames, "malloc()", strlen(scratch)+1)
strcpy(mapnames[i], scratch);
}
@ -4083,10 +4078,7 @@ PlayerConfig_ScanDirectories(void)
}
skinnames = malloc(sizeof(char *) * (nskins + 1));
if(skinnames == NULL)
{
Sys_Error("malloc(sizeof(char *) * (nskins + 1)) failed in %s()!\n", __func__);
}
YQ2_COM_CHECK_OOM(skinnames, "malloc()", sizeof(char *) * (nskins + 1))
memset(skinnames, 0, sizeof(char *) * (nskins + 1));

View file

@ -250,10 +250,7 @@ InitDisplayIndices()
{
/* There are a maximum of 10 digits in 32 bit int + 1 for the NULL terminator. */
displayindices[ i ] = malloc(11 * sizeof( char ));
if(displayindices[i] == NULL)
{
Sys_Error("malloc(11 * sizeof( char )) failed in %s() - out of memory!\n", __func__);
}
YQ2_COM_CHECK_OOM(displayindices[i], "malloc()", 11 * sizeof( char ))
snprintf( displayindices[ i ], 11, "%d", i );
}

View file

@ -996,10 +996,7 @@ FS_ListFiles(char *findname, int *numfiles,
/* Allocate the list. */
list = calloc(nfiles, sizeof(char *));
if(list == NULL)
{
Com_Error(ERR_FATAL, "calloc() failed in %s() - out of memory?!\n", __func__);
}
YQ2_COM_CHECK_OOM(list, "calloc()", (size_t)nfiles*sizeof(char*))
/* Fill the list. */
s = Sys_FindFirst(findname, musthave, canthave);
@ -1102,10 +1099,7 @@ FS_ListFiles2(char *findname, int *numfiles,
nfiles = 0;
list = malloc(sizeof(char *));
if(list == NULL)
{
Com_Error(ERR_FATAL, "malloc() failed in %s() - out of memory?!\n", __func__);
}
YQ2_COM_CHECK_OOM(list, "malloc()", sizeof(char*))
for (search = fs_searchPaths; search != NULL; search = search->next)
{
@ -1132,10 +1126,7 @@ FS_ListFiles2(char *findname, int *numfiles,
nfiles += j;
list = realloc(list, nfiles * sizeof(char *));
if(list == NULL)
{
Com_Error(ERR_FATAL, "realloc() failed in %s() - out of memory?!\n", __func__);
}
YQ2_COM_CHECK_OOM(list, "realloc()", (size_t)nfiles*sizeof(char*))
for (i = 0, j = nfiles - j; i < search->pack->numFiles; i++)
{
@ -1160,10 +1151,7 @@ FS_ListFiles2(char *findname, int *numfiles,
tmpnfiles--;
nfiles += tmpnfiles;
list = realloc(list, nfiles * sizeof(char *));
if(list == NULL)
{
Com_Error(ERR_FATAL, "realloc() failed in %s() - out of memory?!\n", __func__);
}
YQ2_COM_CHECK_OOM(list, "2nd realloc()", (size_t)nfiles*sizeof(char*))
for (i = 0, j = nfiles - tmpnfiles; i < tmpnfiles; i++, j++)
{
@ -1200,10 +1188,7 @@ FS_ListFiles2(char *findname, int *numfiles,
{
nfiles -= tmpnfiles;
tmplist = malloc(nfiles * sizeof(char *));
if(tmplist == NULL)
{
Com_Error(ERR_FATAL, "malloc(nfiles * sizeof(char *)) failed in %s() - out of memory?!\n", __func__);
}
YQ2_COM_CHECK_OOM(tmplist, "malloc()", (size_t)nfiles*sizeof(char*))
for (i = 0, j = 0; i < nfiles + tmpnfiles; i++)
{
@ -1222,10 +1207,7 @@ FS_ListFiles2(char *findname, int *numfiles,
{
nfiles++;
list = realloc(list, nfiles * sizeof(char *));
if(list == NULL)
{
Com_Error(ERR_FATAL, "realloc() failed in %s() - out of memory?!\n", __func__);
}
YQ2_COM_CHECK_OOM(list, "3rd realloc()", (size_t)nfiles*sizeof(char*))
list[nfiles - 1] = NULL;
}

View file

@ -713,6 +713,13 @@ void Com_MDPrintf(char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
void Com_Error(int code, char *fmt, ...) __attribute__ ((format (printf, 2, 3)));
void Com_Quit(void);
// terminate yq2 (with Com_Error()) if VAR is NULL (after malloc() or similar)
// and print message about it
#define YQ2_COM_CHECK_OOM(VAR, ALLOC_FN_NAME, ALLOC_SIZE) \
if(VAR == NULL) { \
Com_Error(ERR_FATAL, "%s for %zd bytes failed in %s() (%s == NULL)! Out of Memory?!\n", \
ALLOC_FN_NAME, (size_t)ALLOC_SIZE, __func__, #VAR); }
int Com_ServerState(void); /* this should have just been a cvar... */
void Com_SetServerState(int state);

View file

@ -208,6 +208,8 @@ SV_GameMap_f(void)
at spawn points instead of occupying body shells */
savedInuse = malloc(maxclients->value * sizeof(qboolean));
YQ2_COM_CHECK_OOM(savedInuse, "malloc()", maxclients->value * sizeof(qboolean))
for (i = 0, cl = svs.clients; i < maxclients->value; i++, cl++)
{
savedInuse[i] = cl->edict->inuse;