From d368a67976deff979c0e99d135bff4c45df82e66 Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Sun, 21 Jul 2019 03:14:21 +0200 Subject: [PATCH] Fixes for PVS Studio warnings: most of src/common/ except for frame.c, I'd like Yamagi's input on that --- src/common/collision.c | 6 +++--- src/common/filesystem.c | 31 +++++++++++++++++++++++++------ src/common/shared/shared.c | 9 ++++++--- src/common/zone.c | 3 +-- 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/common/collision.c b/src/common/collision.c index 0d219848..b9b72780 100644 --- a/src/common/collision.c +++ b/src/common/collision.c @@ -1673,8 +1673,8 @@ CM_LoadMap(char *name, qboolean clientload, unsigned *checksum) map_noareas = Cvar_Get("map_noareas", "0", 0); - if (!strcmp(map_name, - name) && (clientload || !Cvar_VariableValue("flushmap"))) + if (strcmp(map_name, name) == 0 + && (clientload || !Cvar_VariableValue("flushmap"))) { *checksum = last_checksum; @@ -1697,7 +1697,7 @@ CM_LoadMap(char *name, qboolean clientload, unsigned *checksum) map_entitystring[0] = 0; map_name[0] = 0; - if (!name || !name[0]) + if (!name[0]) { numleafs = 1; numclusters = 1; diff --git a/src/common/filesystem.c b/src/common/filesystem.c index f24ab491..8522d503 100644 --- a/src/common/filesystem.c +++ b/src/common/filesystem.c @@ -482,11 +482,6 @@ FS_FOpenFile(const char *name, fileHandle_t *f, qboolean gamedir_only) handle->file = Q_fopen(path, "rb"); } - if (!handle->file) - { - continue; - } - if (handle->file) { if (fs_debug->value) @@ -1001,6 +996,10 @@ 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__); + } /* Fill the list. */ s = Sys_FindFirst(findname, musthave, canthave); @@ -1103,6 +1102,10 @@ 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__); + } for (search = fs_searchPaths; search != NULL; search = search->next) { @@ -1129,6 +1132,10 @@ 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__); + } for (i = 0, j = nfiles - j; i < search->pack->numFiles; i++) { @@ -1153,6 +1160,10 @@ 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__); + } for (i = 0, j = nfiles - tmpnfiles; i < tmpnfiles; i++, j++) { @@ -1189,6 +1200,10 @@ 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__); + } for (i = 0, j = 0; i < nfiles + tmpnfiles; i++) { @@ -1207,6 +1222,10 @@ 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__); + } list[nfiles - 1] = NULL; } @@ -1620,7 +1639,7 @@ FS_BuildGameSpecificSearchPath(char *dir) } // The game was reset to baseq2. Nothing to do here. - if ((Q_stricmp(dir, BASEDIRNAME) == 0) || (*dir == 0)) { + if (Q_stricmp(dir, BASEDIRNAME) == 0) { Cvar_FullSet("gamedir", "", CVAR_SERVERINFO | CVAR_NOSET); Cvar_FullSet("game", "", CVAR_LATCH | CVAR_SERVERINFO); diff --git a/src/common/shared/shared.c b/src/common/shared/shared.c index f1c9b4dc..4653b518 100644 --- a/src/common/shared/shared.c +++ b/src/common/shared/shared.c @@ -75,7 +75,7 @@ RotatePointAroundVector(vec3_t dst, const vec3_t dir, im[2][1] = m[1][2]; memset(zrot, 0, sizeof(zrot)); - zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F; + zrot[2][2] = 1.0F; zrot[0][0] = (float)cos(DEG2RAD(degrees)); zrot[0][1] = (float)sin(DEG2RAD(degrees)); @@ -867,9 +867,12 @@ void Swap_Init(void) { byte swaptest[2] = {1, 0}; + short swapTestShort; + assert(sizeof(short) == 2); + memcpy(&swapTestShort, swaptest, 2); /* set the byte swapping variables in a portable manner */ - if (*(short *)swaptest == 1) + if (swapTestShort == 1) { bigendien = false; _BigShort = ShortSwap; @@ -892,7 +895,7 @@ Swap_Init(void) Com_Printf("Byte ordering: big endian\n\n"); } - if (LittleShort(*(short *)swaptest) != 1) + if (LittleShort(swapTestShort) != 1) assert("Error in the endian conversion!"); } diff --git a/src/common/zone.c b/src/common/zone.c index 275ef07b..1d312adf 100644 --- a/src/common/zone.c +++ b/src/common/zone.c @@ -41,9 +41,8 @@ Z_Free(void *ptr) if (z->magic != Z_MAGIC) { - printf("free: %p failed\n", ptr); + Com_Printf("ERROR: Z_free(%p) failed: bad magic\n", ptr); abort(); - Com_Error(ERR_FATAL, "Z_Free: bad magic"); } z->prev->next = z->next;