Merge branch 'pvs-studio-fixes-1'

This commit is contained in:
Daniel Gibson 2019-11-10 17:39:45 +01:00
commit f3899b1348
23 changed files with 102 additions and 98 deletions

View file

@ -25,6 +25,7 @@
* =======================================================================
*/
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
@ -69,7 +70,11 @@ main(int argc, char **argv)
return 1;
}
realpath(argv[i + 1], datadir);
if(realpath(argv[i + 1], datadir) == NULL)
{
printf("realpath(datadir) failed: %s\n", strerror(errno));
datadir[0] = '\0';
}
}
else
{

View file

@ -373,7 +373,11 @@ SCR_ReadNextFrame(void)
{
int r;
int command;
byte samples[22050 / 14 * 4];
// the samples array is used as bytes or shorts, depending on bitrate (cin.s_width)
// so we need to make sure to align it correctly
YQ2_ALIGNAS_TYPE(short) byte samples[22050 / 14 * 4];
byte compressed[0x20000];
int size;
byte *pic;
@ -413,7 +417,7 @@ SCR_ReadNextFrame(void)
FS_Read(&size, 4, cl.cinematic_file);
size = LittleLong(size);
if (((unsigned long)size > sizeof(compressed)) || (size < 1))
if (((size_t)size > sizeof(compressed)) || (size < 1))
{
Com_Error(ERR_DROP, "Bad compressed frame size");
}

View file

@ -422,20 +422,15 @@ CL_RequestNextDownload(void)
#endif
/* precache phase completed */
precache_check = ENV_CNT;
precache_check = ENV_CNT + 1;
if (precache_check == ENV_CNT)
CM_LoadMap(cl.configstrings[CS_MODELS + 1], true, &map_checksum);
if (map_checksum != (int)strtol(cl.configstrings[CS_MAPCHECKSUM], (char **)NULL, 10))
{
precache_check = ENV_CNT + 1;
CM_LoadMap(cl.configstrings[CS_MODELS + 1], true, &map_checksum);
if (map_checksum != (int)strtol(cl.configstrings[CS_MAPCHECKSUM], (char **)NULL, 10))
{
Com_Error(ERR_DROP, "Local map version differs from server: %i != '%s'\n",
map_checksum, cl.configstrings[CS_MAPCHECKSUM]);
return;
}
Com_Error(ERR_DROP, "Local map version differs from server: %i != '%s'\n",
map_checksum, cl.configstrings[CS_MAPCHECKSUM]);
return;
}
if ((precache_check > ENV_CNT) && (precache_check < TEXTURE_CNT))

View file

@ -1308,12 +1308,8 @@ Key_Event(int key, qboolean down, qboolean special)
return;
}
/* All input subsystems handled after this
point only care for key down events. */
if (!down)
{
return;
}
/* All input subsystems handled after this point only
care for key down events (=> if(!down) returns above). */
/* Everything that's not a special char
is processed by Char_Event(). */

View file

@ -859,7 +859,7 @@ CL_ParseServerData(void)
/* set gamedir */
if ((*str && (!fs_gamedirvar->string || !*fs_gamedirvar->string ||
strcmp(fs_gamedirvar->string, str))) ||
(!*str && (fs_gamedirvar->string || *fs_gamedirvar->string)))
(!*str && (fs_gamedirvar->string && !*fs_gamedirvar->string)))
{
Cvar_Set("game", str);
}

View file

@ -158,10 +158,6 @@ CL_ClipMoveToEntities(vec3_t start, vec3_t mins, vec3_t maxs,
*tr = trace;
}
}
else if (trace.startsolid)
{
tr->startsolid = true;
}
}
}

View file

@ -1104,7 +1104,7 @@ SCR_ExecuteLayoutString(char *s)
Com_Error(ERR_DROP, "Pic >= MAX_IMAGES");
}
if (cl.configstrings[CS_IMAGES + value])
if (cl.configstrings[CS_IMAGES + value][0] != '\0')
{
SCR_AddDirtyPoint(x, y);
SCR_AddDirtyPoint(x + 23*scale, y + 23*scale);

View file

@ -672,7 +672,6 @@ CL_ParseTEnt(void)
{
CL_ParticleEffect(pos, dir, 0, 40);
}
else
{
CL_ParticleEffect(pos, dir, 0xe0, 6);
@ -688,12 +687,10 @@ CL_ParseTEnt(void)
{
S_StartSound(pos, 0, 0, cl_sfx_ric1, 1, ATTN_NORM, 0);
}
else if (cnt == 2)
{
S_StartSound(pos, 0, 0, cl_sfx_ric2, 1, ATTN_NORM, 0);
}
else if (cnt == 3)
{
S_StartSound(pos, 0, 0, cl_sfx_ric3, 1, ATTN_NORM, 0);
@ -753,7 +750,6 @@ CL_ParseTEnt(void)
{
color = 0x00;
}
else
{
color = splash_color[r];
@ -769,12 +765,10 @@ CL_ParseTEnt(void)
{
S_StartSound(pos, 0, 0, cl_sfx_spark5, 1, ATTN_STATIC, 0);
}
else if (r == 1)
{
S_StartSound(pos, 0, 0, cl_sfx_spark6, 1, ATTN_STATIC, 0);
}
else
{
S_StartSound(pos, 0, 0, cl_sfx_spark7, 1, ATTN_STATIC, 0);
@ -815,12 +809,10 @@ CL_ParseTEnt(void)
{
ex->ent.angles[1] = 90;
}
else if (dir[1] < 0)
{
ex->ent.angles[1] = 270;
}
else
{
ex->ent.angles[1] = 0;
@ -867,7 +859,6 @@ CL_ParseTEnt(void)
{
S_StartSound(pos, 0, 0, cl_sfx_watrexp, 1, ATTN_NORM, 0);
}
else
{
S_StartSound(pos, 0, 0, cl_sfx_grenexp, 1, ATTN_NORM, 0);
@ -920,7 +911,6 @@ CL_ParseTEnt(void)
{
ex->ent.model = cl_mod_explo4;
}
else
{
ex->ent.model = cl_mod_explo4_big;
@ -942,7 +932,6 @@ CL_ParseTEnt(void)
{
S_StartSound(pos, 0, 0, cl_sfx_watrexp, 1, ATTN_NORM, 0);
}
else
{
S_StartSound(pos, 0, 0, cl_sfx_rockexp, 1, ATTN_NORM, 0);
@ -1041,7 +1030,6 @@ CL_ParseTEnt(void)
{
CL_BlasterParticles2(pos, dir, 0xd0);
}
else
{
CL_BlasterParticles2(pos, dir, 0x6f);
@ -1055,7 +1043,6 @@ CL_ParseTEnt(void)
{
ex->ent.angles[1] = (float)atan2(dir[1], dir[0]) / M_PI * 180;
}
else if (dir[1] > 0)
{
ex->ent.angles[1] = 90;
@ -1065,7 +1052,6 @@ CL_ParseTEnt(void)
{
ex->ent.angles[1] = 270;
}
else
{
ex->ent.angles[1] = 0;
@ -1078,7 +1064,6 @@ CL_ParseTEnt(void)
{
ex->ent.skinnum = 1;
}
else /* flechette */
{
ex->ent.skinnum = 2;
@ -1091,7 +1076,6 @@ CL_ParseTEnt(void)
{
ex->lightcolor[1] = 1;
}
else
{
/* flechette */
@ -1139,15 +1123,7 @@ CL_ParseTEnt(void)
ex->frames = 15;
if (type == TE_ROCKET_EXPLOSION_WATER)
{
S_StartSound(pos, 0, 0, cl_sfx_watrexp, 1, ATTN_NORM, 0);
}
else
{
S_StartSound(pos, 0, 0, cl_sfx_rockexp, 1, ATTN_NORM, 0);
}
S_StartSound(pos, 0, 0, cl_sfx_rockexp, 1, ATTN_NORM, 0);
break;

View file

@ -183,8 +183,8 @@ V_TestEntities(void)
{
ent = &r_entities[i];
r = 64 * ((i % 4) - 1.5);
f = 64 * (i / 4) + 128;
r = 64.0f * ((float)(i % 4) - 1.5f);
f = (float)(64 * (i / 4) + 128);
for (j = 0; j < 3; j++)
{
@ -236,7 +236,7 @@ V_TestLights(void)
void
CL_PrepRefresh(void)
{
char mapname[32];
char mapname[MAX_QPATH];
int i;
char name[MAX_QPATH];
float rotate;
@ -364,14 +364,7 @@ CL_PrepRefresh(void)
/* start the cd track */
int track = (int)strtol(cl.configstrings[CS_CDTRACK], (char **)NULL, 10);
if (Cvar_VariableValue("ogg_shuffle"))
{
OGG_PlayTrack(track);
}
else
{
OGG_PlayTrack(track);
}
OGG_PlayTrack(track);
}
float
@ -441,8 +434,7 @@ entitycmpfnc(const entity_t *a, const entity_t *b)
}
else
{
return (a->model == b->model) ? 0 :
(a->model > b->model) ? 1 : -1;
return (a->model > b->model) ? 1 : -1;
}
}

View file

@ -973,6 +973,7 @@ void CL_SetHTTPServer (const char *URL)
// Remove trailing / from URL if any.
size_t urllen = strlen(URL);
char *cleanURL = strdup(URL);
YQ2_COM_CHECK_OOM(cleanURL, "strdup(URL)", strlen(URL))
if (cleanURL[urllen - 1] == '/')
{
@ -1067,6 +1068,9 @@ qboolean CL_QueueHTTPDownload(const char *quakePath, qboolean gamedirForFilelist
}
q->next = malloc(sizeof(*q));
YQ2_COM_CHECK_OOM(q->next, "malloc(sizeof(*q))", sizeof(*q))
q = q->next;
q->next = NULL;
q->state = DLQ_STATE_NOT_STARTED;

View file

@ -3060,6 +3060,9 @@ StartServer_MenuInit(void)
}
mapnames = malloc(sizeof(char *) * (nummaps + 1));
YQ2_COM_CHECK_OOM(mapnames, "malloc(sizeof(char *) * (nummaps + 1))", sizeof(char *) * (nummaps + 1))
memset(mapnames, 0, sizeof(char *) * (nummaps + 1));
s = buffer;
@ -3083,6 +3086,8 @@ StartServer_MenuInit(void)
Com_sprintf(scratch, sizeof(scratch), "%s\n%s", longname, shortname);
mapnames[i] = malloc(strlen(scratch) + 1);
YQ2_COM_CHECK_OOM(mapnames, "malloc()", strlen(scratch)+1)
strcpy(mapnames[i], scratch);
}
@ -4160,6 +4165,8 @@ PlayerConfig_ScanDirectories(void)
}
skinnames = malloc(sizeof(char *) * (nskins + 1));
YQ2_COM_CHECK_OOM(skinnames, "malloc()", sizeof(char *) * (nskins + 1))
memset(skinnames, 0, sizeof(char *) * (nskins + 1));
/* copy the valid skins */
@ -4224,10 +4231,7 @@ PlayerConfig_ScanDirectories(void)
s_numplayermodels++;
}
if (dirnames)
{
FreeFileList(dirnames, ndirs);
}
FreeFileList(dirnames, ndirs);
return true;
}

View file

@ -481,7 +481,7 @@ Menu_DrawStatusBar(const char *string)
if (string)
{
int l = (int)strlen(string);
float col = (VID_WIDTH / 2) - (l*8 / 2) * scale;
int col = (VID_WIDTH / 2) - (l*8 / 2) * scale;
Draw_Fill(0, VID_HEIGHT - 8 * scale, VID_WIDTH, 8 * scale, 4);
Menu_DrawString(col, VID_HEIGHT / scale - 8, string);

View file

@ -130,7 +130,7 @@ OGG_InitTrackList(void)
char gameMusicDir[MAX_QPATH] = {0}; // e.g. "xatrix/music"
cvar_t* gameCvar = Cvar_Get("game", "", CVAR_LATCH | CVAR_SERVERINFO);
if (gameCvar == NULL || gameCvar->string[0] == '\0' || strcmp(BASEDIRNAME, gameCvar->string) == 0)
if (gameCvar->string[0] == '\0' || strcmp(BASEDIRNAME, gameCvar->string) == 0)
{
// baseq2 => only 2 dirs in searchPath
potMusicDirs[0] = BASEDIRNAME "/music/"; // baseq2/music/

View file

@ -282,7 +282,6 @@ SDL_TransferPaintBuffer(int endtime)
{
val = 0x7fff;
}
else if (val < -32768)
{
val = -32768;
@ -305,12 +304,14 @@ SDL_TransferPaintBuffer(int endtime)
{
val = 0x7fff;
}
else if (val < -32768)
{
val = -32768;
}
// FIXME: val might be negative and right-shifting it is implementation defined
// on x86 it does sign extension (=> fills up with 1 bits from the left)
// so this /might/ break on other platforms - if it does, look at this code again.
out[out_idx] = (val >> 8) + 128;
out_idx = (out_idx + 1) & out_mask;
}
@ -474,13 +475,12 @@ SDL_PaintChannels(int endtime)
break;
}
if ((count > 0) && ch->sfx)
if (count > 0)
{
if (sc->width == 1)
{
SDL_PaintChannelFrom8(ch, sc, count, ltime - paintedtime);
}
else
{
SDL_PaintChannelFrom16(ch, sc, count, ltime - paintedtime);

View file

@ -250,6 +250,8 @@ InitDisplayIndices()
{
/* There are a maximum of 10 digits in 32 bit int + 1 for the NULL terminator. */
displayindices[ i ] = malloc(11 * sizeof( char ));
YQ2_COM_CHECK_OOM(displayindices[i], "malloc()", 11 * sizeof( char ))
snprintf( displayindices[ i ], 11, "%d", i );
}

View file

@ -66,7 +66,8 @@ typedef struct
byte *cmod_base;
byte map_visibility[MAX_MAP_VISIBILITY];
byte pvsrow[MAX_MAP_LEAFS / 8];
// DG: is casted to int32_t* in SV_FatPVS() so align accordingly
static YQ2_ALIGNAS_TYPE(int32_t) byte pvsrow[MAX_MAP_LEAFS / 8];
byte phsrow[MAX_MAP_LEAFS / 8];
carea_t map_areas[MAX_MAP_AREAS];
cbrush_t map_brushes[MAX_MAP_BRUSHES];
@ -1673,8 +1674,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 +1698,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;
@ -1885,7 +1886,6 @@ CM_ClusterPVS(int cluster)
{
memset(pvsrow, 0, (numclusters + 7) >> 3);
}
else
{
CM_DecompressVis(map_visibility +

View file

@ -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,7 @@ FS_ListFiles(char *findname, int *numfiles,
/* Allocate the list. */
list = calloc(nfiles, sizeof(char *));
YQ2_COM_CHECK_OOM(list, "calloc()", (size_t)nfiles*sizeof(char*))
/* Fill the list. */
s = Sys_FindFirst(findname, musthave, canthave);
@ -1103,6 +1099,7 @@ FS_ListFiles2(char *findname, int *numfiles,
nfiles = 0;
list = malloc(sizeof(char *));
YQ2_COM_CHECK_OOM(list, "malloc()", sizeof(char*))
for (search = fs_searchPaths; search != NULL; search = search->next)
{
@ -1129,6 +1126,7 @@ FS_ListFiles2(char *findname, int *numfiles,
nfiles += j;
list = realloc(list, nfiles * sizeof(char *));
YQ2_COM_CHECK_OOM(list, "realloc()", (size_t)nfiles*sizeof(char*))
for (i = 0, j = nfiles - j; i < search->pack->numFiles; i++)
{
@ -1153,6 +1151,7 @@ FS_ListFiles2(char *findname, int *numfiles,
tmpnfiles--;
nfiles += tmpnfiles;
list = realloc(list, nfiles * sizeof(char *));
YQ2_COM_CHECK_OOM(list, "2nd realloc()", (size_t)nfiles*sizeof(char*))
for (i = 0, j = nfiles - tmpnfiles; i < tmpnfiles; i++, j++)
{
@ -1189,6 +1188,7 @@ FS_ListFiles2(char *findname, int *numfiles,
{
nfiles -= tmpnfiles;
tmplist = malloc(nfiles * sizeof(char *));
YQ2_COM_CHECK_OOM(tmplist, "malloc()", (size_t)nfiles*sizeof(char*))
for (i = 0, j = 0; i < nfiles + tmpnfiles; i++)
{
@ -1207,6 +1207,7 @@ FS_ListFiles2(char *findname, int *numfiles,
{
nfiles++;
list = realloc(list, nfiles * sizeof(char *));
YQ2_COM_CHECK_OOM(list, "3rd realloc()", (size_t)nfiles*sizeof(char*))
list[nfiles - 1] = NULL;
}
@ -1620,7 +1621,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);

View file

@ -53,6 +53,22 @@
#define CFGDIR ".yq2"
#endif
// stuff to align variables/arrays
#if __STDC_VERSION__ >= 201112L // C11 or newer
#define YQ2_ALIGNAS_SIZE(SIZE) _Alignas(SIZE)
#define YQ2_ALIGNAS_TYPE(TYPE) _Alignas(TYPE)
#elif defined(__GNUC__) // GCC and clang should support this attribute
#define YQ2_ALIGNAS_SIZE(SIZE) __attribute__(( __aligned__(SIZE) ))
#define YQ2_ALIGNAS_TYPE(TYPE) __attribute__(( __aligned__(__alignof__(TYPE)) ))
#elif defined(_MSC_VER)
#define YQ2_ALIGNAS_SIZE(SIZE) __declspec( align(SIZE) )
#define YQ2_ALIGNAS_TYPE(TYPE) __declspec( align( __alignof(TYPE) ) )
#else
#warning "Please add a case for your compiler here to align correctly"
#define YQ2_ALIGNAS_TYPE(TYPE)
#endif
/* ================================================================== */
typedef struct sizebuf_s
@ -713,6 +729,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

@ -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!");
}

View file

@ -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;

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;

View file

@ -28,7 +28,8 @@
#include "header/server.h"
byte fatpvs[65536 / 8];
// DG: is casted to int32_t* in SV_FatPVS() so align accordingly
static YQ2_ALIGNAS_TYPE(int32_t) byte fatpvs[65536 / 8];
/*
* Writes a delta update of an entity_state_t list to the message.
@ -433,7 +434,8 @@ SV_FatPVS(vec3_t org)
{
int leafs[64];
int i, j, count;
int longs;
// DG: used to be called "longs" and long was used which isn't really correct on 64bit
int32_t numInt32s;
byte *src;
vec3_t mins, maxs;
@ -450,7 +452,7 @@ SV_FatPVS(vec3_t org)
Com_Error(ERR_FATAL, "SV_FatPVS: count < 1");
}
longs = (CM_NumClusters() + 31) >> 5;
numInt32s = (CM_NumClusters() + 31) >> 5;
/* convert leafs to clusters */
for (i = 0; i < count; i++)
@ -458,7 +460,7 @@ SV_FatPVS(vec3_t org)
leafs[i] = CM_LeafCluster(leafs[i]);
}
memcpy(fatpvs, CM_ClusterPVS(leafs[0]), longs << 2);
memcpy(fatpvs, CM_ClusterPVS(leafs[0]), numInt32s << 2);
/* or in all the other leaf bits */
for (i = 1; i < count; i++)
@ -478,9 +480,9 @@ SV_FatPVS(vec3_t org)
src = CM_ClusterPVS(leafs[i]);
for (j = 0; j < longs; j++)
for (j = 0; j < numInt32s; j++)
{
((long *)fatpvs)[j] |= ((long *)src)[j];
((int32_t *)fatpvs)[j] |= ((int32_t *)src)[j];
}
}
}

View file

@ -227,7 +227,7 @@ SV_Multicast(vec3_t origin, multicast_t to)
continue;
}
if (mask && (!(mask[cluster >> 3] & (1 << (cluster & 7)))))
if (!(mask[cluster >> 3] & (1 << (cluster & 7))))
{
continue;
}