]> git.mxchange.org Git - quix0rs-blobwars.git/commitdiff
Fix -Wunused-result compiler warnings
authorHans de Goede <hdegoede@redhat.com>
Mon, 14 Feb 2011 11:22:07 +0000 (12:22 +0100)
committerHans de Goede <hdegoede@redhat.com>
Mon, 14 Feb 2011 11:22:07 +0000 (12:22 +0100)
Fedora uses -Wunused-result when building packages, combined with the -Werror
from the makefile, this causes compile errors in various places because
of not properly error checking various file io actions. This patch fixes this.

src/CConfig.cpp
src/CEngine.cpp
src/CPak.cpp
src/CReplayData.cpp
src/headers.h
src/init.cpp
src/loadSave.cpp
src/pak.cpp

index ef997bd7741234e8c74293228db87bc9ff10a84e..5121c7f13d08a500c2050509f670e54337b725a8 100644 (file)
@@ -113,6 +113,7 @@ bool Config::loadJoystickConfig()
 
 bool Config::saveJoystickConfig()
 {
+       bool ret = true;
        char filename[PATH_MAX];
        snprintf(filename, sizeof filename, "%sjoystick.cfg", engine->userHomeDirectory);
        
@@ -122,15 +123,19 @@ bool Config::saveJoystickConfig()
        
        if (!fp)
        {
-               debug(("WARNING: Couldn't save joystick config\n"));
+               debug(("WARNING: Couldn't save joystick config: %s\n", strerror(errno)));
                return false;
        }
-               
-       fwrite(&joystick, sizeof(Joystick), 1, fp);
-               
+
+       if (fwrite(&joystick, sizeof(Joystick), 1, fp) != 1)
+       {
+               debug(("WARNING: Couldn't save joystick config: %s\n", strerror(errno)));
+               ret = false;
+       }
+
        fclose(fp);
        
-       return true;
+       return ret;
 }
 
 bool Config::loadKeyConfig()
@@ -163,6 +168,7 @@ bool Config::loadKeyConfig()
 
 bool Config::saveKeyConfig()
 {
+       bool ret = true;
        char filename[PATH_MAX];
        snprintf(filename, sizeof filename, "%skeyboard.cfg", engine->userHomeDirectory);
        
@@ -172,14 +178,19 @@ bool Config::saveKeyConfig()
        
        if (!fp)
        {
+               debug(("WARNING: Couldn't save keyboard config: %s\n", strerror(errno)));
                return false;
        }
-               
-       fwrite(&keyboard, sizeof(keyboard), 1, fp);
-               
+
+       if (fwrite(&keyboard, sizeof(keyboard), 1, fp) != 1)
+       {
+               debug(("WARNING: Couldn't save keyboard config: %s\n", strerror(errno)));
+               ret = false;
+       }
+
        fclose(fp);
        
-       return true;
+       return ret;
 }
 
 void Config::restoreKeyDefaults()
index 7017039722123c54f5df2ad448a3a2297b5b4282..a0deaafcb15feb14b362ae1ee2368ee6c0c502fa 100644 (file)
@@ -330,6 +330,8 @@ since SDL currently provides no means to load music directly from memory
 */
 bool Engine::unpack(const char *filename, int fileType)
 {
+       bool ret = true;
+
        if (fileType == PAK_DATA)
        {
                delete[] dataBuffer;
@@ -396,17 +398,23 @@ bool Engine::unpack(const char *filename, int fileType)
                        return false;
                }
 
-               fwrite(binaryBuffer, 1, pak.getUncompressedSize(), fp);
+               if (fwrite(binaryBuffer, 1, pak.getUncompressedSize(), fp) != pak.getUncompressedSize())
+               {
+                       printf("Fatal Error: could not write to %s: %s", tempPath, strerror(errno));
+                       ret = false;
+               }
                fclose(fp);
        }
 
-       debug(("unpack() : Loaded %s (%d)\n", filename, pak.getUncompressedSize()));
+       debug(("unpack() : Loaded %s (%d), ret: %d\n", filename, pak.getUncompressedSize(), (int)ret));
 
-       return true;
+       return ret;
 }
 
 bool Engine::loadData(const char *filename)
 {
+       bool ret = true;
+
        delete[] dataBuffer;
        dataBuffer = NULL;
        
@@ -427,14 +435,16 @@ bool Engine::loadData(const char *filename)
 
        dataBuffer = new unsigned char[fSize + 1];
 
-       fread(dataBuffer, 1, fSize, fp);
+       if (fread(dataBuffer, 1, fSize, fp) != (size_t)fSize)
+               ret = false;
+
        dataBuffer[fSize] = 0;
 
        fclose(fp);
 
-       debug(("loadData() : Loaded %s (%d)\n", filename, fSize));
+       debug(("loadData() : Loaded %s (%d), ret: %d\n", filename, fSize, (int)ret));
 
-       return true;
+       return ret;
 }
 
 void Engine::reportFontFailure()
index ff29cef806f90ace3628e5ccda4328ef9a6fdfec..f63086742a51b0c60d76486f36c655980f27e048 100644 (file)
@@ -66,8 +66,16 @@ void Pak::setPakFile(const char *pakFilename)
        }
 
        fseek(pak, (-sizeof(Uint32)) * 2, SEEK_END);
-       fread(&listPos, sizeof(Uint32), 1, pak);
-       fread(&numberOfFiles, sizeof(Uint32), 1, pak);
+       if (fread(&listPos, sizeof(Uint32), 1, pak) != 1)
+       {
+               fclose(pak);
+               showPakErrorAndExit();
+       }
+       if (fread(&numberOfFiles, sizeof(Uint32), 1, pak) != 1)
+       {
+               fclose(pak);
+               showPakErrorAndExit();
+       }
        
        debug(("Pak : File list resides at %d\n", (int)listPos));
        debug(("Pak : Number of files are %d\n", (int)numberOfFiles));
@@ -130,7 +138,11 @@ bool Pak::unpack(const char *filename, unsigned char **buffer)
        input = new unsigned char[(int)(currentFile->cSize * 1.01) + 12];
        *buffer = new unsigned char[currentFile->fSize + 1];
 
-       fread(input, 1, currentFile->cSize, pak);
+       if (fread(input, 1, currentFile->cSize, pak) != currentFile->cSize)
+       {
+               fclose(pak);
+               showPakErrorAndExit();
+       }
        
        uLongf fSize = (uLongf)currentFile->fSize;
        
index 6af849229891b3f1ce5ee4f8f928e4972ec204e3..51c1834fba1067bbe60e95161696313c5e052038 100644 (file)
@@ -22,7 +22,12 @@ ReplayData::~ReplayData()
        {
                save();
                rewind(fp);
-               fwrite(&header, sizeof(ReplayDataHeader), 1, fp);
+               int size = fwrite(&header, sizeof(ReplayDataHeader), 1, fp);
+               if (size != 1)
+               {
+                       printf("Error saving replay data: %s\n", strerror(errno));
+                       exit(1);
+               }
        }
        
        if (replayMode != REPLAY_MODE::NONE)
@@ -98,7 +103,15 @@ void ReplayData::setMode(REPLAY_MODE::TYPE replayMode)
                
                swapHeaderEndians();
                
-               fwrite(&header, sizeof(ReplayDataHeader), 1, fp);
+               int size = fwrite(&header, sizeof(ReplayDataHeader), 1, fp);
+               if (size != 1)
+               {
+                       printf("Error writing replay data header: %s\n", strerror(errno));
+                       replayMode = REPLAY_MODE::NONE;
+                       fclose(fp);
+                       fp = NULL;
+                       return;
+               }
                
                reset();
        }
index be319310c91bf23395f637d0b0ab10ffd7b8004d..d67980993aec1e444a635fefc905985949eda357 100644 (file)
@@ -18,6 +18,7 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
 */
 
+#include <errno.h>
 #include <stdlib.h>
 #include <string.h>
 #include <math.h>
index 46ac3817282ad1fdab019be8712da28d5007d444..b12cba6398dfa2be5c7a38547e9f7e3c75ca9304 100644 (file)
@@ -144,7 +144,10 @@ bool loadConfig()
                return true;
        }
 
-       fscanf(fp, "%10f %10d", &version, &release);
+       if (fscanf(fp, "%10f %10d", &version, &release) != 2)
+       {
+               rtn = true;
+       }
 
        debug(("Version = %.2f - Expected %.2f\n", version, VERSION));
        debug(("Release = %d - Expected %d\n", release, RELEASE));
@@ -154,7 +157,10 @@ bool loadConfig()
                rtn = true;
        }
 
-       fscanf(fp, "%10d %10d %10d %10d %10d %10d %10d", &engine.fullScreen, &game.musicVol, &game.soundVol, &game.output, &game.brightness, &engine.extremeAvailable, &game.gore);
+       if (fscanf(fp, "%10d %10d %10d %10d %10d %10d %10d", &engine.fullScreen, &game.musicVol, &game.soundVol, &game.output, &game.brightness, &engine.extremeAvailable, &game.gore) != 7)
+       {
+               rtn = true;
+       }
 
        fclose(fp);
 
@@ -223,7 +229,13 @@ int initMedalService(void *data)
                return 0;
        }
        
-       fscanf(fp, "%19s", privateKey);
+       if (fscanf(fp, "%19s", privateKey) != 1)
+       {
+               graphics.showMedalMessage(-1, "Medal Key file corrupt - Online functions disabled");
+               SDL_mutexV(medalServer.lock);
+               fclose(fp);
+               return 0;
+       }
        
        fclose(fp);
                
index 73f53840277cc1845b2563e94ab3c7a210131eef..9489f5e46c28d620cbf4807cb52cc186c1c9ed36 100644 (file)
@@ -122,8 +122,11 @@ bool loadGame(int slot)
        
        while (true)
        {
-               fgets(line, 1024, fp);
-               
+               if (!fgets(line, 1024, fp)) {
+                       fclose(fp);
+                       graphics.showErrorAndExit("Unexpected end of file reading save data", "");
+               }
+
                sscanf(line, "%*c %[^\"] %*c %*c %[^\"] %*c %d %d", string[0], string[1], &param[0], &param[1]);
                
                data = new Data();
@@ -160,8 +163,11 @@ bool loadGame(int slot)
        
        while (true)
        {
-               fgets(line, 1024, fp);
-               
+               if (!fgets(line, 1024, fp)) {
+                       fclose(fp);
+                       graphics.showErrorAndExit("Unexpected end of file reading save data", "");
+               }
+
                sscanf(line, "%[^\n\r]", string[0]);
                strlcpy(stageName, string[0], sizeof stageName);
                
@@ -169,8 +175,11 @@ bool loadGame(int slot)
                {
                        break;
                }
-               
-               fgets(line, 1024, fp);
+
+               if (!fgets(line, 1024, fp)) {
+                       fclose(fp);
+                       graphics.showErrorAndExit("Unexpected end of file reading save data", "");
+               }
                sscanf(line, "%d", &numberOfLines);
                
                debug(("Read %s with %d lines.\n", stageName, numberOfLines));
@@ -180,9 +189,12 @@ bool loadGame(int slot)
                for (int i = 0 ; i < numberOfLines ; i++)
                {
                        persistData = new PersistData();
-                       
-                       fgets(line, 1024, fp);
-                       
+
+                       if (!fgets(line, 1024, fp)) {
+                               fclose(fp);
+                               graphics.showErrorAndExit("Unexpected end of file reading save data", "");
+                       }
+
                        strlcpy(persistData->data, line, sizeof persistData->data);
                        
                        //debug(("Read %d: %s", i, persistData->data));
@@ -346,7 +358,11 @@ void saveGame()
                graphics.showErrorAndExit("File write error whilst saving game", "");
        }
 
-       fwrite(&game, sizeof(Game), 1, fp);
+       if (fwrite(&game, sizeof(Game), 1, fp) != 1)
+       {
+               fclose(fp);
+               graphics.showErrorAndExit("File write error whilst saving game", strerror(errno));
+       }
        
        fclose(fp);
        
index d70fe571864cc15227742e257cd782486f9b5567..226afe45aa2fc391a25c8456cbeaf5f850045843 100644 (file)
@@ -154,7 +154,12 @@ void recurseDirectory(const char *dirName)
                                
                                fileData[files].set(filename, fSize, cSize, ftell(pak));
 
-                               fwrite(output, 1, cSize, pak);
+                               if (fwrite(output, 1, cSize, pak) != cSize)
+                               {
+                                       fprintf(stderr, "Error writing to pakfile: %s\n", strerror(errno));
+                                       fclose(pak);
+                                       exit(1);
+                               }
 
                                files++;
                                
@@ -183,6 +188,11 @@ int main(int argc, char *argv[])
        }
 
        pak = fopen(argv[argc - 1], "wb");
+       if (!pak)
+       {
+               fprintf(stderr, "Error opening %s: %s\n", argv[argc - 1], strerror(errno));
+               return 1;
+       }
        
        for (int i = 1 ; i < (argc - 1) ; i++)
        {
@@ -211,13 +221,28 @@ int main(int argc, char *argv[])
                        break;
                }
 
-               fwrite(&fileData[i], sizeof(FileData), 1, pak);
+               if (fwrite(&fileData[i], sizeof(FileData), 1, pak) != 1)
+               {
+                       fprintf(stderr, "Error writing to %s: %s\n", argv[argc - 1], strerror(errno));
+                       fclose(pak);
+                       return 1;
+               }
        }
        
        unsigned int numberOfFiles = totalFiles;
 
-       fwrite(&pos, sizeof(unsigned int), 1, pak);
-       fwrite(&numberOfFiles, sizeof(unsigned int), 1, pak);
+       if (fwrite(&pos, sizeof(unsigned int), 1, pak) != 1)
+       {
+               fprintf(stderr, "Error writing to %s: %s\n", argv[argc - 1], strerror(errno));
+               fclose(pak);
+               return 1;
+       }
+       if (fwrite(&numberOfFiles, sizeof(unsigned int), 1, pak) != 1)
+       {
+               fprintf(stderr, "Error writing to %s: %s\n", argv[argc - 1], strerror(errno));
+               fclose(pak);
+               return 1;
+       }
 
        fclose(pak);