From 6671923e966e5fb82eeac52be174fb6295dc9ee7 Mon Sep 17 00:00:00 2001 From: Guus Sliepen Date: Sat, 21 Nov 2015 17:07:54 +0100 Subject: [PATCH] Fix all issues found by the Clang Static Analyzer. Only a few were actual errors. --- src/CEngine.cpp | 8 +++----- src/CGraphics.cpp | 12 ++++++++---- src/CReplayData.cpp | 8 ++++---- src/cutscene.cpp | 4 ++-- src/enemies.cpp | 4 ++-- src/game.cpp | 14 ++++++++------ src/graphics.cpp | 4 +--- src/headers.h | 4 ++-- src/info.cpp | 2 -- src/intro.cpp | 6 +++--- src/items.cpp | 9 +++------ src/map.cpp | 4 +--- src/mission.cpp | 6 +----- src/particles.cpp | 2 +- src/tankBoss.cpp | 2 +- src/title.cpp | 2 +- src/trains.cpp | 3 +-- 17 files changed, 42 insertions(+), 52 deletions(-) diff --git a/src/CEngine.cpp b/src/CEngine.cpp index d99bb0a..0f4721f 100644 --- a/src/CEngine.cpp +++ b/src/CEngine.cpp @@ -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); } diff --git a/src/CGraphics.cpp b/src/CGraphics.cpp index 0beb6ca..cde769d 100644 --- a/src/CGraphics.cpp +++ b/src/CGraphics.cpp @@ -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)) diff --git a/src/CReplayData.cpp b/src/CReplayData.cpp index 51c1834..747c601 100644 --- a/src/CReplayData.cpp +++ b/src/CReplayData.cpp @@ -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; diff --git a/src/cutscene.cpp b/src/cutscene.cpp index 1592add..68672b6 100644 --- a/src/cutscene.cpp +++ b/src/cutscene.cpp @@ -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; diff --git a/src/enemies.cpp b/src/enemies.cpp index 679d6d9..944eab2 100644 --- a/src/enemies.cpp +++ b/src/enemies.cpp @@ -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"); diff --git a/src/game.cpp b/src/game.cpp index 7cb8c0f..29aafdf 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -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) diff --git a/src/graphics.cpp b/src/graphics.cpp index ae6d4a2..7b8da92 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -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) { diff --git a/src/headers.h b/src/headers.h index aea64aa..a79edb2 100644 --- a/src/headers.h +++ b/src/headers.h @@ -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" diff --git a/src/info.cpp b/src/info.cpp index 8f0822d..f3934e2 100644 --- a/src/info.cpp +++ b/src/info.cpp @@ -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); } diff --git a/src/intro.cpp b/src/intro.cpp index ca84b05..697daf2 100644 --- a/src/intro.cpp +++ b/src/intro.cpp @@ -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) diff --git a/src/items.cpp b/src/items.cpp index 814dead..5b78d4a 100644 --- a/src/items.cpp +++ b/src/items.cpp @@ -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(); diff --git a/src/map.cpp b/src/map.cpp index 8c3a765..34dbcfa 100644 --- a/src/map.cpp +++ b/src/map.cpp @@ -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) { diff --git a/src/mission.cpp b/src/mission.cpp index 02d98b7..4519b24 100644 --- a/src/mission.cpp +++ b/src/mission.cpp @@ -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); diff --git a/src/particles.cpp b/src/particles.cpp index 5ea2597..79f71ef 100644 --- a/src/particles.cpp +++ b/src/particles.cpp @@ -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; diff --git a/src/tankBoss.cpp b/src/tankBoss.cpp index 59ed0f1..4cda249 100644 --- a/src/tankBoss.cpp +++ b/src/tankBoss.cpp @@ -92,7 +92,7 @@ void traceTankBossMGFireLine() dx = -1; } - int x = (int)self->x; + int x; int y = (int)self->y + 10; Entity *enemy = NULL; diff --git a/src/title.cpp b/src/title.cpp index ae3e889..48c826e 100644 --- a/src/title.cpp +++ b/src/title.cpp @@ -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")); diff --git a/src/trains.cpp b/src/trains.cpp index d2a43cb..0357521 100644 --- a/src/trains.cpp +++ b/src/trains.cpp @@ -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); } -- 2.39.5