From 58077c63a7a925e1fe42319c251f92987485c3ac Mon Sep 17 00:00:00 2001 From: Gregory Hainaut Date: Fri, 18 Apr 2014 23:21:27 +0200 Subject: [PATCH] gcc-warning: check c function return. Print error when bad. v2: properly check return code of fread/getcwd function --- common/include/PS2Eext.h | 4 ++- plugins/GSdx/GS.cpp | 30 +++++++++++++------ plugins/GSdx/GSThread.h | 2 +- plugins/zzogl-pg/opengl/GSmain.cpp | 37 +++++++++++++++--------- plugins/zzogl-pg/opengl/Linux/Linux.cpp | 5 +++- plugins/zzogl-pg/opengl/ZZoglCreate.cpp | 4 ++- plugins/zzogl-pg/opengl/ZZoglShaders.cpp | 3 +- 7 files changed, 58 insertions(+), 27 deletions(-) diff --git a/common/include/PS2Eext.h b/common/include/PS2Eext.h index e6d6fedcb5..c7df9db28c 100644 --- a/common/include/PS2Eext.h +++ b/common/include/PS2Eext.h @@ -146,7 +146,9 @@ struct PluginConf int value = defval; std::string buf = item + " = %d\n"; - if (ConfFile) fscanf(ConfFile, buf.c_str(), &value); + if (ConfFile) + if (fscanf(ConfFile, buf.c_str(), &value) < 0) + SysMessage("Somethings got wrong when option was read\n"); return value; } diff --git a/plugins/GSdx/GS.cpp b/plugins/GSdx/GS.cpp index a4c90a443a..b7fae89a0b 100644 --- a/plugins/GSdx/GS.cpp +++ b/plugins/GSdx/GS.cpp @@ -1415,6 +1415,18 @@ inline unsigned long timeGetTime() return (unsigned long)(t.time*1000 + t.millitm); } +void _fread(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + static uint32 read_cnt = 0; + read_cnt++; + + size_t result = fread(ptr, size, nmemb, stream); + if (result != nmemb) { + fprintf(stderr, "Read error\n"); + exit(read_cnt); + } +} + // Note EXPORT_C GSReplay(char* lpszCmdLine, int renderer) { @@ -1471,17 +1483,17 @@ EXPORT_C GSReplay(char* lpszCmdLine, int renderer) if (s_gs->m_wnd == NULL) return; uint32 crc; - fread(&crc, 4, 1, fp); + _fread(&crc, 4, 1, fp); GSsetGameCRC(crc, 0); GSFreezeData fd; - fread(&fd.size, 4, 1, fp); + _fread(&fd.size, 4, 1, fp); fd.data = new uint8[fd.size]; - fread(fd.data, fd.size, 1, fp); + _fread(fd.data, fd.size, 1, fp); GSfreeze(FREEZE_LOAD, &fd); delete [] fd.data; - fread(regs, 0x2000, 1, fp); + _fread(regs, 0x2000, 1, fp); GSvsync(1); @@ -1503,20 +1515,20 @@ EXPORT_C GSReplay(char* lpszCmdLine, int renderer) p->param = (uint8)fgetc(fp); - fread(&p->size, 4, 1, fp); + _fread(&p->size, 4, 1, fp); switch(p->param) { case 0: p->buff.resize(0x4000); p->addr = 0x4000 - p->size; - fread(&p->buff[p->addr], p->size, 1, fp); + _fread(&p->buff[p->addr], p->size, 1, fp); break; case 1: case 2: case 3: p->buff.resize(p->size); - fread(&p->buff[0], p->size, 1, fp); + _fread(&p->buff[0], p->size, 1, fp); break; } @@ -1530,7 +1542,7 @@ EXPORT_C GSReplay(char* lpszCmdLine, int renderer) case 2: - fread(&p->size, 4, 1, fp); + _fread(&p->size, 4, 1, fp); break; @@ -1538,7 +1550,7 @@ EXPORT_C GSReplay(char* lpszCmdLine, int renderer) p->buff.resize(0x2000); - fread(&p->buff[0], 0x2000, 1, fp); + _fread(&p->buff[0], 0x2000, 1, fp); break; } diff --git a/plugins/GSdx/GSThread.h b/plugins/GSdx/GSThread.h index 0ad7b04a62..03faadee74 100644 --- a/plugins/GSdx/GSThread.h +++ b/plugins/GSdx/GSThread.h @@ -228,7 +228,7 @@ public: } void Set() {pthread_cond_signal(&m_cv);} - bool Wait(IGSLock* l) {pthread_cond_wait(&m_cv, *(GSCondVarLock*)l) == 0; return true;} + bool Wait(IGSLock* l) {return pthread_cond_wait(&m_cv, *(GSCondVarLock*)l) == 0;} operator pthread_cond_t* () {return &m_cv;} }; diff --git a/plugins/zzogl-pg/opengl/GSmain.cpp b/plugins/zzogl-pg/opengl/GSmain.cpp index 99267c99a2..f282c4ca89 100644 --- a/plugins/zzogl-pg/opengl/GSmain.cpp +++ b/plugins/zzogl-pg/opengl/GSmain.cpp @@ -642,6 +642,17 @@ EXPORT_C_(s32) GSfreeze(int mode, freezeData *data) } #ifdef __LINUX__ +void _fread(void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + static uint32 read_cnt = 0; + read_cnt++; + + size_t result = fread(ptr, size, nmemb, stream); + if (result != nmemb) { + fprintf(stderr, "Read error\n"); + exit(read_cnt); + } +} struct Packet { @@ -664,21 +675,21 @@ EXPORT_C_(void) GSReplay(char* lpszCmdLine) void* hWnd = NULL; - //_GSopen((void**)&hWnd, "", renderer); - GSopen((void**)&hWnd, "", 0); + const char* title = "replayer"; + GSopen((void**)&hWnd, (char*)title, 0); u32 crc; - fread(&crc, 4, 1, fp); + _fread(&crc, 4, 1, fp); GSsetGameCRC(crc, 0); freezeData fd; - fread(&fd.size, 4, 1, fp); + _fread(&fd.size, 4, 1, fp); fd.data = new s8[fd.size]; - fread(fd.data, fd.size, 1, fp); + _fread(fd.data, fd.size, 1, fp); GSfreeze(FREEZE_LOAD, &fd); delete [] fd.data; - fread(regs, 0x2000, 1, fp); + _fread(regs, 0x2000, 1, fp); GSvsync(1); @@ -698,8 +709,8 @@ EXPORT_C_(void) GSReplay(char* lpszCmdLine) p->param = (u8)fgetc(fp); - fread(&p->size, 4, 1, fp); - fread(&p->real_size, 4, 1, fp); + _fread(&p->size, 4, 1, fp); + _fread(&p->real_size, 4, 1, fp); switch(p->param) { @@ -707,13 +718,13 @@ EXPORT_C_(void) GSReplay(char* lpszCmdLine) p->buff.resize(0x4000); //p->addr = 0x4000 - p->size; //fread(&p->buff[p->addr], p->size, 1, fp); - fread(&p->buff[0], p->size, 1, fp); + _fread(&p->buff[0], p->size, 1, fp); break; case 1: case 2: case 3: p->buff.resize(p->size); - fread(&p->buff[0], p->size, 1, fp); + _fread(&p->buff[0], p->size, 1, fp); break; } @@ -721,14 +732,14 @@ EXPORT_C_(void) GSReplay(char* lpszCmdLine) case 1: - fread(&p->param, 4, 1, fp); + _fread(&p->param, 4, 1, fp); //p->param = (u8)fgetc(fp); break; case 2: - fread(&p->size, 4, 1, fp); + _fread(&p->size, 4, 1, fp); break; @@ -736,7 +747,7 @@ EXPORT_C_(void) GSReplay(char* lpszCmdLine) p->buff.resize(0x2000); - fread(&p->buff[0], 0x2000, 1, fp); + _fread(&p->buff[0], 0x2000, 1, fp); break; diff --git a/plugins/zzogl-pg/opengl/Linux/Linux.cpp b/plugins/zzogl-pg/opengl/Linux/Linux.cpp index 92b8e9553a..02db373943 100644 --- a/plugins/zzogl-pg/opengl/Linux/Linux.cpp +++ b/plugins/zzogl-pg/opengl/Linux/Linux.cpp @@ -435,7 +435,10 @@ void DisplayDialog() EXPORT_C_(void) GSconfigure() { char strcurdir[256]; - getcwd(strcurdir, 256); + if (getcwd(strcurdir, 256) == NULL) { + fprintf(stderr, "Failed to get current working directory\n"); + return; + } if (!(conf.loaded())) LoadConfig(); diff --git a/plugins/zzogl-pg/opengl/ZZoglCreate.cpp b/plugins/zzogl-pg/opengl/ZZoglCreate.cpp index 38fa993fa1..3f9447d5f9 100644 --- a/plugins/zzogl-pg/opengl/ZZoglCreate.cpp +++ b/plugins/zzogl-pg/opengl/ZZoglCreate.cpp @@ -270,7 +270,9 @@ __forceinline bool LoadShadersFromDat() size_t s = ftell(fres); s_lpShaderResources = new u8[s+1]; fseek(fres, 0, SEEK_SET); - fread(s_lpShaderResources, s, 1, fres); + if (fread(s_lpShaderResources, s, 1, fres) == 0) + fprintf(stderr, "Failed to read ps2hw.dat. Corrupted file?\n"); + s_lpShaderResources[s] = 0; return true; diff --git a/plugins/zzogl-pg/opengl/ZZoglShaders.cpp b/plugins/zzogl-pg/opengl/ZZoglShaders.cpp index cce839fab7..25e83d310d 100644 --- a/plugins/zzogl-pg/opengl/ZZoglShaders.cpp +++ b/plugins/zzogl-pg/opengl/ZZoglShaders.cpp @@ -221,7 +221,8 @@ bool ZZshCreateOpenShadersFile() { size_t s = ftell(fres); s_lpShaderResources = new u8[s+1]; fseek(fres, 0, SEEK_SET); - fread(s_lpShaderResources, s, 1, fres); + if (fread(s_lpShaderResources, s, 1, fres) == 0) + ZZLog::Error_Log("Cannot read ps2hw.dat in working directory."); s_lpShaderResources[s] = 0; # endif // _WIN32 #else // NOT RELEASE_TO_PUBLIC