Fix all issues found by the Clang Static Analyzer.
authorGuus Sliepen <guus@debian.org>
Sat, 21 Nov 2015 16:07:54 +0000 (17:07 +0100)
committerGuus Sliepen <guus@debian.org>
Sat, 21 Nov 2015 16:07:54 +0000 (17:07 +0100)
Only a few were actual errors.

17 files changed:
src/CEngine.cpp
src/CGraphics.cpp
src/CReplayData.cpp
src/cutscene.cpp
src/enemies.cpp
src/game.cpp
src/graphics.cpp
src/headers.h
src/info.cpp
src/intro.cpp
src/items.cpp
src/map.cpp
src/mission.cpp
src/particles.cpp
src/tankBoss.cpp
src/title.cpp
src/trains.cpp

index d99bb0aaf67190e4a51cf9a108d6f4014ba02925..0f4721fd7c8cbe05704eb82770ba46abee65516c 100644 (file)
@@ -918,20 +918,18 @@ bool Engine::loadDefines()
        if (!loadData("data/defines.h"))
                return false;
 
-       char *token = strtok((char*)dataBuffer, "\n");
-
-       Data *data;
+       strtok((char*)dataBuffer, "\n");
 
        while (true)
        {
-               token = strtok(NULL, "\n");
+               char *token = strtok(NULL, "\n");
                if (!token)
                        break;
 
                if (!strstr(token, "/*"))
                {
                        sscanf(token, "%*s %s %[^\n\r]", string[0], string[1]);
-                       data = new Data();
+                       Data *data = new Data();
                        data->set(string[0], string[1], 1, 1);
                        defineList.add(data);
                }
index 0beb6ca3ad4dd5f27cb5deb2e1142be72a6abe94..cde769d75013d580142c4fbc4c02c69087018fbc 100644 (file)
@@ -160,7 +160,8 @@ Sprite *Graphics::getSpriteHead()
 
 void Graphics::setTransparent(SDL_Surface *sprite)
 {
-       SDL_SetColorKey(sprite, SDL_TRUE, SDL_MapRGB(sprite->format, 0, 0, 0));
+       if (sprite)
+               SDL_SetColorKey(sprite, SDL_TRUE, SDL_MapRGB(sprite->format, 0, 0, 0));
 }
 
 bool Graphics::canShowMedalMessage() const
@@ -361,7 +362,7 @@ SDL_Surface *Graphics::loadImage(const char *filename, bool srcalpha)
        #endif
 
        if (!image)
-               showErrorAndExit(ERR_FILE, filename);
+               return showErrorAndExit(ERR_FILE, filename), image;
 
        newImage = SDL_ConvertSurface(image, screen->format, 0);
 
@@ -396,7 +397,7 @@ SDL_Surface *Graphics::loadImage(const char *filename, int hue, int sat, int val
        #endif
 
        if (!image)
-               showErrorAndExit(ERR_FILE, filename);
+               return showErrorAndExit(ERR_FILE, filename), image;
 
        if ((hue != 0) || (sat != 0) || (value != 0))
        {
@@ -521,6 +522,9 @@ void Graphics::loadMapTiles(const char *baseDir)
                {
                        tile[i] = loadImage(filename);
 
+                       if (!tile[i])
+                               abort();
+
                        if (autoAlpha)
                        {
                                if ((i < MAP_EXITSIGN) || (i >= MAP_WATERANIM))
@@ -746,7 +750,7 @@ void Graphics::blit(SDL_Surface *image, int x, int y, SDL_Surface *dest, bool ce
 {
        if (!image)
        {
-               showErrorAndExit("graphics::blit() - NULL pointer", SDL_GetError());
+               return showErrorAndExit("graphics::blit() - NULL pointer", SDL_GetError());
        }
 
        if ((x < -image->w) || (x > 640 + image->w))
index 51c1834fba1067bbe60e95161696313c5e052038..747c601e5d01570df84e70135d21e8e2be718dc8 100644 (file)
@@ -67,14 +67,14 @@ void ReplayData::setMode(REPLAY_MODE::TYPE replayMode)
                if (!fp)
                {
                        printf("ERROR: Replay file '%s' could not be loaded.\n", filename);
-                       replayMode = REPLAY_MODE::NONE;
+                       this->replayMode = REPLAY_MODE::NONE;
                        return;
                }
                
                if (fread(&header, sizeof(ReplayDataHeader), 1, fp) != 1)
                {
                        printf("ERROR: Replay file '%s' is corrupt\n", filename);
-                       replayMode = REPLAY_MODE::NONE;
+                       this->replayMode = REPLAY_MODE::NONE;
                        fclose(fp);
                        return;
                }
@@ -97,7 +97,7 @@ void ReplayData::setMode(REPLAY_MODE::TYPE replayMode)
                if (!fp)
                {
                        printf("ERROR: Replay file '%s' could not be opened for writing.\n", filename);
-                       replayMode = REPLAY_MODE::NONE;
+                       this->replayMode = REPLAY_MODE::NONE;
                        return;
                }
                
@@ -107,7 +107,7 @@ void ReplayData::setMode(REPLAY_MODE::TYPE replayMode)
                if (size != 1)
                {
                        printf("Error writing replay data header: %s\n", strerror(errno));
-                       replayMode = REPLAY_MODE::NONE;
+                       this->replayMode = REPLAY_MODE::NONE;
                        fclose(fp);
                        fp = NULL;
                        return;
index 1592addfa241d1d9f8c0788322d898117dad00da..68672b6b0a3a779f139dbfa3dce9fce8a8d80c8a 100644 (file)
@@ -64,7 +64,7 @@ void createSceneList()
                        line = strtok(NULL, "\n");
                }
                
-               if (strcmp(line, "@none@") != 0)
+               if (scene && strcmp(line, "@none@") != 0)
                {
                        scene->appendText(line);
                }
@@ -78,7 +78,7 @@ bool setupScene(const char *stagename)
        char sceneLine[1024];
 
        if (!engine.loadData(_("data/ending")))
-               graphics.showErrorAndExit("Couldn't load cutscene data file (%s)", _("data/ending"));
+               return graphics.showErrorAndExit("Couldn't load cutscene data file (%s)", _("data/ending")), false;
 
        char *line = strtok((char*)engine.dataBuffer, "\n");
        int i = 0;
index 679d6d996213217993ebd3758e6aa2633e3414bb..944eab2d30bd5703bbe9b6cff3baa86a0efa8cdd 100644 (file)
@@ -57,7 +57,6 @@ Entity *getEnemy(const char *name)
 
 void addEnemy(const char *name, int x, int y, int flags)
 {
-       Entity *enemy = new Entity();
        Entity *defEnemy = getDefinedEnemy(name);
 
        if (defEnemy == NULL)
@@ -66,6 +65,7 @@ void addEnemy(const char *name, int x, int y, int flags)
                return;
        }
 
+       Entity *enemy = new Entity();
        enemy->setName(defEnemy->name);
        enemy->setSprites(defEnemy->sprite[0], defEnemy->sprite[1], defEnemy->sprite[2]);
        enemy->currentWeapon = defEnemy->currentWeapon;
@@ -856,7 +856,7 @@ void loadDefEnemies()
 
        if (!engine.loadData("data/defEnemies"))
        {
-               graphics.showErrorAndExit("Couldn't load enemy definitions file (%s)", "data/defEnemies");
+               return graphics.showErrorAndExit("Couldn't load enemy definitions file (%s)", "data/defEnemies");
        }
 
        char *token = strtok((char*)engine.dataBuffer, "\n");
index 7cb8c0f5844f7d1ec4869093f9b697e6493a0cd2..29aafdfbae6255ccee848fe24dc315f71d7d03e7 100644 (file)
@@ -35,7 +35,7 @@ void showInGameOptions()
 {
        if (!engine.loadWidgets(_("data/inGameWidgets")))
        {
-               graphics.showErrorAndExit(ERR_FILE, _("data/inGameWidgets"));
+               return graphics.showErrorAndExit(ERR_FILE, _("data/inGameWidgets"));
        }
 
        graphics.drawRect(120, 100, 400, 300, graphics.black, graphics.white, graphics.screen);
@@ -288,7 +288,7 @@ int gameover()
        
        if (!engine.loadWidgets(_("data/gameOverWidgets")))
        {
-               graphics.showErrorAndExit(ERR_FILE, _("data/gameOverWidgets"));
+               return graphics.showErrorAndExit(ERR_FILE, _("data/gameOverWidgets")), SECTION_GAME;
        }
 
        SDL_Surface *gameover = graphics.quickSprite("Game Over", graphics.loadImage("gfx/main/gameover.png"));
@@ -565,11 +565,11 @@ int doGame()
        SDL_FillRect(graphics.screen, NULL, graphics.black);
        graphics.delay(1000);
 
-       Uint32 then, frames, frameLimit, millis;
+       Uint32 frames, frameLimit, millis;
        Uint32 start, cur;
 
        #if DEBUG
-       Uint32 now, frameCounter;
+       Uint32 now, then, frameCounter;
        char fps[10];
        strlcpy(fps, "fps", sizeof fps);
        #endif
@@ -614,9 +614,9 @@ int doGame()
 
        frameLimit = SDL_GetTicks() + 16;
        frames = millis = 0;
-       start = then = SDL_GetTicks();
+       start = SDL_GetTicks();
 #ifdef DEBUG
-       frameCounter = SDL_GetTicks();
+       then = frameCounter = start;
 #endif
 
        if ((strcmp(map.name, "Space Station") == 0) && (!game.continueFromCheckPoint))
@@ -795,7 +795,9 @@ int doGame()
                        config.populate();
                        config.doPause();
                        graphics.updateScreen();
+                       #ifdef DEBUG
                        then = SDL_GetTicks();
+                       #endif
                        frames = 0;
 
                        if (!engine.paused)
index ae6d4a27311f3e8780c446d8520d1fa7e4096a74..7b8da92741b4655be0863cc5dafaedeef854fd06 100644 (file)
@@ -25,8 +25,6 @@ void showAllSprites()
 {
        loadResources();
 
-       Sprite *sprite = graphics.getSpriteHead();
-
        int x, y, h;
 
        unsigned int frameLimit = SDL_GetTicks() + 16;
@@ -42,7 +40,7 @@ void showAllSprites()
 
                SDL_FillRect(graphics.screen, NULL, graphics.black);
 
-               sprite = graphics.getSpriteHead();
+               Sprite *sprite = graphics.getSpriteHead();
 
                while (sprite->next != NULL)
                {
index aea64aa7f8e079f5f7b39e537847002548f5591e..a79edb2cf27d0bb70fc5715fc669cb892b98d886 100644 (file)
@@ -57,8 +57,8 @@ extern DECLSPEC int SDLCALL SDL_GetGamma(float *red, float *green, float *blue);
 #endif
 
 #if !defined(OpenBSD) && !defined(FreeBSD)
-#define strlcat(dest, src, n) strncat((dest), (src), (n) - 1)
-#define strlcpy(dest, src, n) do {strncpy((dest), (src), (n)); (dest)[(n) - 1] = 0;} while(false)
+static inline void strlcat(char *dest, const char *src, size_t n) { strncat(dest, src, n - 1); }
+static inline void strlcpy(char *dest, const char *src, size_t n) { strncpy(dest, src, n); dest[n - 1] = 0; }
 #endif
 
 #include "defs.h"
index 8f0822dda856852d4029802e0586ca2eac2d68df..f3934e2434af7f69ec47310bc559db1fd5d72846 100644 (file)
@@ -325,8 +325,6 @@ void doPauseInfo()
 
        graphics.setFontColor(0xff, 0xff, 0xff, 0x00, 0x00, 0x00);
 
-       y += 10;
-
        snprintf(string, sizeof string, "%s - %.2d:%.2d:%.2d", _("Mission Time"), game.currentMissionHours, game.currentMissionMinutes, game.currentMissionSeconds);
        graphics.drawString(string, 320, 430, TXT_CENTERED, graphics.screen);
 }
index ca84b0572338ac87c71adc604a0d80f03ac59a85..697daf2fd6d348ff415c1653610ea5200c3f0807 100644 (file)
@@ -38,7 +38,7 @@ void playIntro(int tx, int ty, int delay)
 
                if (!line[i])
                {
-                       graphics.showErrorAndExit("Malformed Intro Data", "");
+                       return graphics.showErrorAndExit("Malformed Intro Data", "");
                }
 
                text[i] = NULL;
@@ -163,11 +163,11 @@ int doIntro()
 
        audio.playMusic();
 
-       char *line = strtok((char*)engine.dataBuffer, "\n");
+       strtok((char*)engine.dataBuffer, "\n");
 
        while (true)
        {
-               line = strtok(NULL, "\n");
+               char *line = strtok(NULL, "\n");
                sscanf(line, "%d %d %d", &x, &y, &delay);
 
                if (delay == -1)
index 814deadef31fda0f8e91c27d3f00a63310c189b3..5b78d4a1a3b568708ac21e2ac4608ca596eff332 100644 (file)
@@ -362,13 +362,10 @@ void showCarriedItems()
 void doItems()
 {
        Entity *item = (Entity*)map.itemList.getHead();
-       Entity *previous = item;
-
-       int x, y;
 
        while (item->next != NULL)
        {
-               previous = item;
+               Entity *previous = item;
                
                item = (Entity*)item->next;
                
@@ -377,8 +374,8 @@ void doItems()
                        continue;
                }
 
-               x = (int)(item->x - engine.playerPosX);
-               y = (int)(item->y - engine.playerPosY);
+               int x = (int)(item->x - engine.playerPosX);
+               int y = (int)(item->y - engine.playerPosY);
 
                item->think();
 
index 8c3a765b5a71b2c8945062344d944178f2622490..34dbcfad793995d87d8c50e7060e2257e5a0561b 100644 (file)
@@ -298,8 +298,6 @@ void showMap(int centerX, int centerY)
        
        graphics.blit(background, 0, 0, panel, false);
        
-       int color = graphics.black;
-       
        for (int y = 0 ; y < 48 ; y++)
        {
                for (int x = 0 ; x < 64 ; x++)
@@ -317,7 +315,7 @@ void showMap(int centerX, int centerY)
        {
                for (int x = 0 ; x < 64 ; x++)
                {
-                       color = graphics.black;
+                       int color = graphics.black;
 
                        if (map.data[x1 + x][y1 + y] == MAP_AIR)
                        {
index 02d98b7740b43e8b6aa015bba85b5a7693020fd1..4519b24181f96b877c41bce3fef1fb4e43b48a24 100644 (file)
@@ -177,7 +177,6 @@ void showMissionClear()
        int y = 520;
        int miaY = 335;
        int clearY = 520;
-       Objective *objective = (Objective*)map.objectiveList.getHead();
        Entity *mia = (Entity*)map.miaList.getHead();
        Sprite *teleportStar = graphics.getSprite("TeleportStar", true);
        char message[256];
@@ -212,9 +211,6 @@ void showMissionClear()
                place += 25;
        }
 
-       if (count > colCount)
-               count = colCount;
-
        if (place > (colCount * 25))
                place = colCount * 25;
 
@@ -248,7 +244,7 @@ void showMissionClear()
                }
        }
 
-       objective = (Objective*)map.objectiveList.getHead();
+       Objective *objective = (Objective*)map.objectiveList.getHead();
 
        engine.setPlayerPosition(0, 0, -1, -1, -1, -1);
 
index 5ea25974d34d4fd2397503b6314ab755af98569b..79f71efcab2b5f0c1fbfa46be6e70c3063861ebd 100644 (file)
@@ -23,7 +23,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 void addWindParticles()
 {
-       int c = graphics.white;
+       int c;
        float x, y, dx, dy;
        
 
index 59ed0f15c577b1c1c415e4f4c8866e75e60273a7..4cda249e86d1335f53b7f535b2c86fe1d843aa92 100644 (file)
@@ -92,7 +92,7 @@ void traceTankBossMGFireLine()
                dx = -1;
        }
        
-       int x = (int)self->x;
+       int x;
        int y = (int)self->y + 10;
        
        Entity *enemy = NULL;
index ae3e889dd71a40552e135f51d3657953e5c00e19..48c826e925094baa98096ab03f2fd8a2711e1174 100644 (file)
@@ -477,7 +477,7 @@ void doCredits()
        int i = 0;
        int numberOfCredits = 0;
        int pos1 = 0, pos2 = 0, size = 0;
-       float *y, deviceY;
+       float *y, deviceY = 0;
 
        SDL_Surface *backdrop = graphics.quickSprite("CreditsBackGround", graphics.loadImage("gfx/main/creditsBack.png"));
 
index d2a43cba9b9559a82a7c0140458b5127e8a9b897..03575210703e2c305bc1e4dce2375b662928aa7f 100644 (file)
@@ -361,7 +361,6 @@ void doTrains()
                {
                        oldX = (int)train->x;
                        oldY = (int)train->y;
-                       playSound = false;
                        
                        playSound = train->openClose();
                        
@@ -385,7 +384,7 @@ void doTrains()
                        setTrainSprite(train);
                }
 
-               if ((abs(x) <= 800) && (abs(y) <= 600))
+               if (train->sprite && (abs(x) <= 800) && (abs(y) <= 600))
                {
                        graphics.blit(train->sprite->getCurrentFrame(), x, y, graphics.screen, false);
                }