From 129da805a18eb230ff0435a346884ec060983409 Mon Sep 17 00:00:00 2001 From: Denis Pauk Date: Thu, 12 Sep 2019 22:36:13 +0300 Subject: [PATCH] Make static C code analysis tool little more happy(#426) sw_image.c: 203: The potential null pointer is passed into 'memcpy' function. Inspect the first argument. Check lines: 203, 192. 368: The null pointer is passed into 'free' function. Inspect the first argument. 401: Expression '!pic' is always false. 401: There are two 'if' statements with identical conditional expressions. The first 'if' statement contains function return. This means that the second 'if' statement is senseless. Check lines: 394, 401. 565: The potential null pointer is passed into 'memcpy' function. Inspect the first argument. Check lines: 565, 564. sw_model.c: 215: The 'model' pointer was used unsafely after it was verified against nullptr. Check lines: 210, 215. 985: The 'header->lumps[12].filelen / sizeof (int)' expression was implicitly cast from 'size_t' type to 'float' type. Consider utilizing an explicit type cast to avoid the loss of a fractional part. An example: double A = (double)(X) / Y;. 1277: Function 'RE_BeginRegistration' argument 1 names different: declaration 'map' definition 'model'. sw_surf.c: 239: There might be dereferencing of a potential null pointer 'sc_base'. Check lines: 239, 236. sw_main.c: 1345: The if condition is the same as the previous if condition 1466: Function 'R_GammaCorrectAndSetPalette' argument 1 names different: declaration 'pal' definition 'palette' 2063: The 'swap_buffers' pointer in the expression could be nullptr. In such case, resulting value of arithmetic operations on this pointer will be senseless and it should not be used. Check lines: 2063, 2061. sw_aclip.c: 180: Variable 'j' is reassigned a value before the old one has been used. sw_alias.c: 209: Function 'R_AliasTransformVector' argument 3 names different: declaration 'm' definition 'xf'. sw_draw.c: 78: Function 'RE_Draw_CharScaled' argument 3 names different: declaration 'c' definition 'num'. 149: Function 'RE_Draw_GetPicSize' argument 3 names different: declaration 'name' definition 'pic'. 173: The scope of the variable 'u' can be reduced. 267: Function 'RE_Draw_PicScaled' argument 4 names different: declaration 'factor' definition 'scale'. sw_scan.c 593: Function 'D_DrawSpansPow2' argument 1 names different: declaration 'pspans' definition 'pspan'. 745: Function 'D_DrawZSpans' argument 1 names different: declaration 'pspans' definition 'pspan'. --- src/client/refresh/soft/header/local.h | 10 +++++----- src/client/refresh/soft/sw_aclip.c | 6 +++++- src/client/refresh/soft/sw_alias.c | 2 +- src/client/refresh/soft/sw_draw.c | 20 +++++++++++--------- src/client/refresh/soft/sw_image.c | 15 ++++++++++----- src/client/refresh/soft/sw_main.c | 16 +++++++++++----- src/client/refresh/soft/sw_model.c | 3 ++- src/client/refresh/soft/sw_surf.c | 6 ++++++ 8 files changed, 51 insertions(+), 27 deletions(-) diff --git a/src/client/refresh/soft/header/local.h b/src/client/refresh/soft/header/local.h index e4f6dccb..1f2b6e93 100644 --- a/src/client/refresh/soft/header/local.h +++ b/src/client/refresh/soft/header/local.h @@ -358,8 +358,8 @@ extern float d_sdivzstepu, d_tdivzstepu; extern float d_sdivzstepv, d_tdivzstepv; extern float d_sdivzorigin, d_tdivzorigin; -void D_DrawSpansPow2(espan_t *pspans, float d_ziorigin, float d_zistepu, float d_zistepv); -void D_DrawZSpans(espan_t *pspans, float d_ziorigin, float d_zistepu, float d_zistepv); +void D_DrawSpansPow2(espan_t *pspan, float d_ziorigin, float d_zistepu, float d_zistepv); +void D_DrawZSpans(espan_t *pspan, float d_ziorigin, float d_zistepu, float d_zistepv); void TurbulentPow2(espan_t *pspan, float d_ziorigin, float d_zistepu, float d_zistepv); void NonTurbulentPow2(espan_t *pspan, float d_ziorigin, float d_zistepu, float d_zistepv); @@ -557,14 +557,14 @@ void Draw_InitLocal(void); void R_InitCaches(void); void D_FlushCaches(void); -void RE_BeginRegistration (char *map); -struct model_s *RE_RegisterModel (char *name); +void RE_BeginRegistration (char *model); +struct model_s *RE_RegisterModel (char *name); void RE_EndRegistration (void); struct image_s *RE_Draw_FindPic (char *name); void RE_Draw_GetPicSize (int *w, int *h, char *name); -void RE_Draw_PicScaled (int x, int y, char *name, float factor); +void RE_Draw_PicScaled (int x, int y, char *name, float scale); void RE_Draw_StretchPic (int x, int y, int w, int h, char *name); void RE_Draw_StretchRaw (int x, int y, int w, int h, int cols, int rows, byte *data); void RE_Draw_CharScaled (int x, int y, int c, float scale); diff --git a/src/client/refresh/soft/sw_aclip.c b/src/client/refresh/soft/sw_aclip.c index 734686f8..c5a2276c 100644 --- a/src/client/refresh/soft/sw_aclip.c +++ b/src/client/refresh/soft/sw_aclip.c @@ -177,7 +177,7 @@ R_AliasClip (const finalvert_t *in, finalvert_t *out, int flag, int count, j = count-1; k = 0; - for (i=0 ; i vid.height ) // status text was missing in sw... return; - row = num>>4; - col = num&15; + row = c>>4; + col = c&15; source = draw_chars->pixels[0] + (row<<10) + (col<<3); if (y < 0) @@ -146,11 +146,11 @@ RE_Draw_GetPicSize ============= */ void -RE_Draw_GetPicSize (int *w, int *h, char *pic) +RE_Draw_GetPicSize (int *w, int *h, char *name) { image_t *gl; - gl = RE_Draw_FindPic (pic); + gl = RE_Draw_FindPic (name); if (!gl) { *w = *h = -1; @@ -170,7 +170,6 @@ RE_Draw_StretchPicImplementation (int x, int y, int w, int h, const image_t *pic { pixel_t *dest; byte *source; - int v, u; int height; int skip; @@ -198,6 +197,8 @@ RE_Draw_StretchPicImplementation (int x, int y, int w, int h, const image_t *pic if (w == pic->width) { + int v; + for (v=0 ; vheight/h; @@ -207,9 +208,10 @@ RE_Draw_StretchPicImplementation (int x, int y, int w, int h, const image_t *pic } else { + int v; for (v=0 ; vheight/h; source = pic->pixels[0] + sv*pic->width; f = 0; diff --git a/src/client/refresh/soft/sw_image.c b/src/client/refresh/soft/sw_image.c index 2d2544bd..f2e96b31 100644 --- a/src/client/refresh/soft/sw_image.c +++ b/src/client/refresh/soft/sw_image.c @@ -190,6 +190,12 @@ R_LoadPic (char *name, byte *pic, int width, int height, imagetype_t type) size = width * height; full_size = R_GetImageMipsSize(size); image->pixels[0] = malloc(full_size); + if (!image->pixels[0]) + { + ri.Sys_Error(ERR_FATAL, "%s: Can't allocate image.", __func__); + // code never returns after ERR_FATAL + return NULL; + } image->transparent = false; for (i=0 ; ivieworg, r_refdef.vieworg); VectorCopy (fd->viewangles, r_refdef.viewangles); - // compare current possition with old + // compare current position with old if (!VectorCompareRound(fd->vieworg, lastvieworg) || !VectorCompare(fd->viewangles, lastviewangles)) { @@ -1278,7 +1278,7 @@ RE_RenderFrame (refdef_t *fd) fastmoving = false; } - // save possition for next check + // save position for next check VectorCopy (fd->vieworg, lastvieworg); VectorCopy (fd->viewangles, lastviewangles); @@ -1340,10 +1340,10 @@ RE_RenderFrame (refdef_t *fd) D_WarpScreen (); if (r_dspeeds->value) + { da_time1 = SDL_GetTicks(); - - if (r_dspeeds->value) da_time2 = SDL_GetTicks(); + } // Modify the palette (when taking hit or pickup item) so all colors are modified R_CalcPalette (); @@ -2172,6 +2172,12 @@ SWimp_CreateRender(void) { swap_current = 0; swap_buffers = malloc(vid.height * vid.width * sizeof(pixel_t) * 2); + if (!swap_buffers) + { + ri.Sys_Error(ERR_FATAL, "%s: Can't allocate swapbuffer.", __func__); + // code never returns after ERR_FATAL + return; + } swap_frames[0] = swap_buffers; swap_frames[1] = swap_buffers + vid.height * vid.width * sizeof(pixel_t); vid_buffer = swap_frames[swap_current&1]; diff --git a/src/client/refresh/soft/sw_model.c b/src/client/refresh/soft/sw_model.c index 44fb4b70..7489153c 100644 --- a/src/client/refresh/soft/sw_model.c +++ b/src/client/refresh/soft/sw_model.c @@ -210,6 +210,7 @@ Mod_PointInLeaf (vec3_t p, model_t *model) if (!model || !model->nodes) { ri.Sys_Error(ERR_DROP, "%s: bad model", __func__); + return NULL; } node = model->nodes; @@ -982,7 +983,7 @@ Mod_LoadBrushModel(model_t *mod, void *buffer, int modfilelen) int hunkSize = 0; hunkSize += calcLumpHunkSize(&header->lumps[LUMP_VERTEXES], sizeof(dvertex_t), sizeof(mvertex_t), 8); hunkSize += calcLumpHunkSize(&header->lumps[LUMP_EDGES], sizeof(dedge_t), sizeof(medge_t), 13); - float surfEdgeCount = header->lumps[LUMP_SURFEDGES].filelen/sizeof(int); + float surfEdgeCount = (float)header->lumps[LUMP_SURFEDGES].filelen / sizeof(int); if(surfEdgeCount < MAX_MAP_SURFEDGES) // else it errors out later anyway hunkSize += calcLumpHunkSize(&header->lumps[LUMP_SURFEDGES], sizeof(int), sizeof(int), 24); diff --git a/src/client/refresh/soft/sw_surf.c b/src/client/refresh/soft/sw_surf.c index 8131c79d..ed212302 100644 --- a/src/client/refresh/soft/sw_surf.c +++ b/src/client/refresh/soft/sw_surf.c @@ -234,6 +234,12 @@ R_InitCaches (void) sc_size = size; sc_base = (surfcache_t *)malloc(size); + if (!sc_base) + { + ri.Sys_Error(ERR_FATAL, "%s: Can't allocate cache.", __func__); + // code never returns after ERR_FATAL + return; + } sc_rover = sc_base; sc_base->next = NULL;