From 3b8dd289accfed6af6492aad41b6e197f26cbc59 Mon Sep 17 00:00:00 2001 From: Mark Vejvoda Date: Mon, 16 Apr 2012 19:29:37 +0000 Subject: [PATCH] - updated cmake to check for support of backtraces in gcc compiler - fixed numerous bugs / warnings from cppcheck --- CMakeLists.txt | 20 ++++++++++++++++ source/glest_game/main/battle_end.cpp | 2 +- source/glest_game/main/main.cpp | 2 +- .../menu/menu_state_connected_game.cpp | 2 +- .../menu/menu_state_custom_game.cpp | 2 +- .../menu/menu_state_masterserver.cpp | 2 +- .../glest_game/network/server_interface.cpp | 2 +- source/glest_game/type_instances/faction.cpp | 4 ++-- source/glest_game/types/tech_tree.cpp | 2 +- source/shared_lib/include/util/leak_dumper.h | 9 ++++--- .../sources/feathery_ftp/ftpTargetWin32.c | 2 +- source/shared_lib/sources/graphics/font.cpp | 10 ++++---- source/shared_lib/sources/graphics/pixmap.cpp | 24 +++++++++++++++++++ source/shared_lib/sources/map/map_preview.cpp | 4 ++-- .../platform/common/platform_common.cpp | 4 ++-- .../sources/platform/miniupnpc/miniwget.c | 20 ++++++++++++---- .../sources/platform/posix/socket.cpp | 2 +- .../sources/platform/sdl/platform_util.cpp | 9 ++++--- .../sources/platform/win32/platform_util.cpp | 2 +- 19 files changed, 92 insertions(+), 32 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fecccfcb..7cd2f021 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -121,6 +121,26 @@ IF(CMAKE_COMPILER_IS_GNUCXX OR MINGW) MESSAGE(STATUS "*WARNING: Disabled use of STREFLOP! Out of synchs may occur") ENDIF() + include(CheckCXXSourceRuns) + + check_cxx_source_runs(" + #include + #include + int main() + { + const size_t max_depth = 6; + void *stack_addrs[max_depth]; + size_t stack_depth = backtrace(stack_addrs, max_depth); + }" + HAS_GCC_BACKTRACE) + + IF(HAS_GCC_BACKTRACE) + message(STATUS "Found GCC backtrace lib, will support backtraces") + ADD_DEFINITIONS(-DHAS_GCC_BACKTRACE) + ELSE() + message(STATUS "**WARNING DID NOT Find GCC backtrace lib") + ENDIF() + # Debug compiler flags SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3") diff --git a/source/glest_game/main/battle_end.cpp b/source/glest_game/main/battle_end.cpp index 11bed200..1987011e 100644 --- a/source/glest_game/main/battle_end.cpp +++ b/source/glest_game/main/battle_end.cpp @@ -287,7 +287,7 @@ void BattleEnd::render() { break; default: - printf("Error control = %d for i\n",stats.getControl(i),i); + printf("Error control = %d for i = %d\n",stats.getControl(i),i); assert(false); }; } diff --git a/source/glest_game/main/main.cpp b/source/glest_game/main/main.cpp index fbe79a15..f57cee87 100644 --- a/source/glest_game/main/main.cpp +++ b/source/glest_game/main/main.cpp @@ -2301,7 +2301,7 @@ void runTilesetValidationReport(int argc, char** argv) { bool showDuplicateFiles = true; bool purgeUnusedFiles = false; - double purgedMegaBytes=0; + //double purgedMegaBytes=0; Config &config = Config::getInstance(); // Did the user pass a specific tileset to validate? diff --git a/source/glest_game/menu/menu_state_connected_game.cpp b/source/glest_game/menu/menu_state_connected_game.cpp index c99f1251..09504425 100644 --- a/source/glest_game/menu/menu_state_connected_game.cpp +++ b/source/glest_game/menu/menu_state_connected_game.cpp @@ -2783,7 +2783,7 @@ void MenuStateConnectedGame::update() { program->setState(new Game(program, clientInterface->getGameSettings(),false)); return; - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,__LINE__); } } diff --git a/source/glest_game/menu/menu_state_custom_game.cpp b/source/glest_game/menu/menu_state_custom_game.cpp index 26db232d..913fb2d8 100644 --- a/source/glest_game/menu/menu_state_custom_game.cpp +++ b/source/glest_game/menu/menu_state_custom_game.cpp @@ -3085,7 +3085,7 @@ void MenuStateCustomGame::loadGameSettings(GameSettings *gameSettings,bool force //printf("slot = %d serverInterface->getSlot(i)->getConnectedTime() = %d session key [%d]\n",i,serverInterface->getSlot(i)->getConnectedTime(),serverInterface->getSlot(i)->getSessionKey()); - if(clientConnectedTime == 0 || clientConnectedTime == 0 || + if(clientConnectedTime == 0 || (serverInterface->getSlot(i)->getConnectedTime() > 0 && serverInterface->getSlot(i)->getConnectedTime() < clientConnectedTime)) { clientConnectedTime = serverInterface->getSlot(i)->getConnectedTime(); gameSettings->setMasterserver_admin(serverInterface->getSlot(i)->getSessionKey()); diff --git a/source/glest_game/menu/menu_state_masterserver.cpp b/source/glest_game/menu/menu_state_masterserver.cpp index 8af75596..a2df3d8e 100644 --- a/source/glest_game/menu/menu_state_masterserver.cpp +++ b/source/glest_game/menu/menu_state_masterserver.cpp @@ -1048,7 +1048,7 @@ bool MenuStateMasterserver::connectToServer(string ipString, int port) { showMessageBox(lang.get("Couldnt connect"), lang.get("Connection failed"), false); return false; - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] connection failed\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); + //if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] connection failed\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__); } else { if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s] connected to [%s]\n",extractFileFromDirectoryPath(__FILE__).c_str(),__FUNCTION__,serverIp.getString().c_str()); diff --git a/source/glest_game/network/server_interface.cpp b/source/glest_game/network/server_interface.cpp index 83a39842..8b02907d 100644 --- a/source/glest_game/network/server_interface.cpp +++ b/source/glest_game/network/server_interface.cpp @@ -405,7 +405,7 @@ bool ServerInterface::switchSlot(int fromPlayerIndex, int toPlayerIndex) { MutexSafeWrapper safeMutex(serverSynchAccessor,CODE_AT_LINE); MutexSafeWrapper safeMutexSlot(slotAccessorMutexes[fromPlayerIndex],CODE_AT_LINE_X(fromPlayerIndex)); MutexSafeWrapper safeMutexSlot2(slotAccessorMutexes[toPlayerIndex],CODE_AT_LINE_X(toPlayerIndex)); - if(slots[toPlayerIndex] != NULL && slots[toPlayerIndex] != NULL && + if(slots[toPlayerIndex] != NULL && slots[toPlayerIndex]->isConnected() == false) { slots[fromPlayerIndex]->setPlayerIndex(toPlayerIndex); slots[toPlayerIndex]->setPlayerIndex(fromPlayerIndex); diff --git a/source/glest_game/type_instances/faction.cpp b/source/glest_game/type_instances/faction.cpp index f1dc761c..38754dc8 100644 --- a/source/glest_game/type_instances/faction.cpp +++ b/source/glest_game/type_instances/faction.cpp @@ -684,7 +684,7 @@ bool Faction::reqsOk(const RequirableType *rt) const { } if(dynamic_cast(rt) != NULL ) { - const UnitType *producedUnitType=(UnitType *) rt; + const UnitType *producedUnitType= dynamic_cast(rt); if(producedUnitType != NULL && producedUnitType->getMaxUnitCount() > 0) { if(producedUnitType->getMaxUnitCount() <= getCountForMaxUnitCount(producedUnitType)) { if(SystemFlags::getSystemSettingType(SystemFlags::debugLUA).enabled) SystemFlags::OutputDebug(SystemFlags::debugLUA,"In [%s::%s Line: %d]\n",__FILE__,__FUNCTION__, __LINE__); @@ -1080,7 +1080,7 @@ void Faction::removeUnit(Unit *unit){ } throw megaglest_runtime_error("Could not remove unit from faction!"); - assert(false); + //assert(false); } void Faction::addStore(const UnitType *unitType){ diff --git a/source/glest_game/types/tech_tree.cpp b/source/glest_game/types/tech_tree.cpp index 0a950fb2..6d9efba4 100644 --- a/source/glest_game/types/tech_tree.cpp +++ b/source/glest_game/types/tech_tree.cpp @@ -70,7 +70,7 @@ string TechTree::findPath(const string &techName) const{ //printf(">>> test path=%s\n",path.c_str()); if(isdir(path.c_str()) == true) { return path; - break; + //break; } } //return "no path found for tech: \""+techname+"\""; diff --git a/source/shared_lib/include/util/leak_dumper.h b/source/shared_lib/include/util/leak_dumper.h index 41eb5da3..64917d4b 100644 --- a/source/shared_lib/include/util/leak_dumper.h +++ b/source/shared_lib/include/util/leak_dumper.h @@ -33,7 +33,8 @@ using Shared::Platform::Mutex; // START - For gcc backtrace -#if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__FreeBSD__) && !defined(BSD) +//#if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__FreeBSD__) && !defined(BSD) +#if defined(HAS_GCC_BACKTRACE) #include #include #include @@ -78,7 +79,8 @@ public: : ptr(ptr), file(file), line(line), bytes(bytes), array(array), freetouse(false), inuse(true), stack(stacktrace) { } -#if defined(__GNUC__) && !defined(__FreeBSD__) && !defined(BSD) +//#if defined(__GNUC__) && !defined(__FreeBSD__) && !defined(BSD) +#if defined(HAS_GCC_BACKTRACE) inline static int getFileAndLine(char *function, void *address, char *file, size_t flen) { int line=-1; if(want_full_leak_stacktrace_line_numbers == true && AllocInfo::get_application_binary() != "") { @@ -171,7 +173,8 @@ public: #endif inline static string getStackTrace() { -#if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__FreeBSD__) && !defined(BSD) +//#if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__FreeBSD__) && !defined(BSD) +#if defined(HAS_GCC_BACKTRACE) if(want_full_leak_stacktrace == true) { string errMsg = "\nStack Trace:\n"; //errMsg += "To find line #'s use:\n"; diff --git a/source/shared_lib/sources/feathery_ftp/ftpTargetWin32.c b/source/shared_lib/sources/feathery_ftp/ftpTargetWin32.c index da037271..8d0ede72 100644 --- a/source/shared_lib/sources/feathery_ftp/ftpTargetWin32.c +++ b/source/shared_lib/sources/feathery_ftp/ftpTargetWin32.c @@ -547,7 +547,7 @@ int getLastSocketError() { } const char * getLastSocketErrorText(int *errNumber) { - int errId = (errNumber != NULL ? *errNumber : getLastSocketError()); + //int errId = (errNumber != NULL ? *errNumber : getLastSocketError()); //return WSAGetLastErrorMessage("",errId); return "?"; } diff --git a/source/shared_lib/sources/graphics/font.cpp b/source/shared_lib/sources/graphics/font.cpp index 3504cc47..f6ff3780 100644 --- a/source/shared_lib/sources/graphics/font.cpp +++ b/source/shared_lib/sources/graphics/font.cpp @@ -326,15 +326,15 @@ const char* findFont(const char *firstFontToTry,const char *firstFontFamilyToTry #define CHECK_FONT_PATH(filename,fontFamily) \ { \ path = filename; \ - if( !font && path && strlen(path) > 0 && fileExists(path) == true ) { \ + if( font == NULL && path != NULL && strlen(path) > 0 && fileExists(path) == true ) { \ font = strdup(path); \ } \ if(SystemFlags::VERBOSE_MODE_ENABLED) printf("#1 Searching for font file [%s] result [%s]\n",path,font); \ - if( !font && fontFamily != NULL && strlen(fontFamily) > 0) { \ + if( font != NULL && fontFamily != NULL && strlen(fontFamily) > 0) { \ string fileFound = findFontFamily(font, fontFamily); \ if(fileFound != "") { \ path = fileFound.c_str(); \ - if( !font && path && strlen(path) > 0 && fileExists(path) == true ) { \ + if( font != NULL && path && strlen(path) > 0 && fileExists(path) == true ) { \ font = strdup(path); \ } \ } \ @@ -343,8 +343,8 @@ const char* findFont(const char *firstFontToTry,const char *firstFontFamilyToTry } string tryFont = ""; - if(firstFontToTry || firstFontFamilyToTry) { - if(firstFontToTry && strlen(firstFontToTry) > 0) { + if(firstFontToTry != NULL || firstFontFamilyToTry != NULL) { + if(firstFontToTry != NULL && strlen(firstFontToTry) > 0) { tryFont = firstFontToTry; #ifdef WIN32 replaceAll(tryFont, "/", "\\"); diff --git a/source/shared_lib/sources/graphics/pixmap.cpp b/source/shared_lib/sources/graphics/pixmap.cpp index 2fb25be7..b499d8d6 100644 --- a/source/shared_lib/sources/graphics/pixmap.cpp +++ b/source/shared_lib/sources/graphics/pixmap.cpp @@ -449,6 +449,7 @@ void PixmapIoPng::openWrite(const string &path, int w, int h, int components) { void PixmapIoPng::write(uint8 *pixels) { // initialize stuff +/* std::auto_ptr imrow(new png_byte*[h]); for(int i = 0; i < h; ++i) { imrow.get()[i] = pixels+(h-1-i) * w * components; @@ -470,8 +471,31 @@ void PixmapIoPng::write(uint8 *pixels) { png_write_info(imgp, infop); png_write_image(imgp, imrow.get()); png_write_end(imgp, NULL); +*/ + png_bytep *imrow = new png_bytep[h]; + //png_bytep *imrow = (png_bytep*) malloc(sizeof(png_bytep) * height); + for(int i = 0; i < h; ++i) { + imrow[i] = pixels + (h-1-i) * w * components; + } + png_structp imgp = png_create_write_struct(PNG_LIBPNG_VER_STRING,0,0,0); + png_infop infop = png_create_info_struct(imgp); + png_init_io(imgp, file); + int color_type = PNG_COLOR_TYPE_RGB; + if(components == 4) { + color_type = PNG_COLOR_TYPE_RGBA; + } + png_set_IHDR(imgp, infop, w, h, + 8, color_type, PNG_INTERLACE_NONE, + PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); + + // write file + png_write_info(imgp, infop); + png_write_image(imgp, imrow); + png_write_end(imgp, NULL); + + delete [] imrow; /* // Allocate write & info structures diff --git a/source/shared_lib/sources/map/map_preview.cpp b/source/shared_lib/sources/map/map_preview.cpp index 1687dad4..706a4d8c 100644 --- a/source/shared_lib/sources/map/map_preview.cpp +++ b/source/shared_lib/sources/map/map_preview.cpp @@ -503,7 +503,7 @@ void MapPreview::reset(int w, int h, float alt, MapSurfaceType surf) { char szBuf[1024]=""; sprintf(szBuf,"Size of map must be at least %dx%d",MIN_MAP_CELL_DIMENSION,MIN_MAP_CELL_DIMENSION); throw megaglest_runtime_error(szBuf); - return; + //return; } if (w > MAX_MAP_CELL_DIMENSION || h > MAX_MAP_CELL_DIMENSION) { @@ -555,7 +555,7 @@ void MapPreview::resize(int w, int h, float alt, MapSurfaceType surf) { char szBuf[1024]=""; sprintf(szBuf,"Size of map must be at least %dx%d",MIN_MAP_CELL_DIMENSION,MIN_MAP_CELL_DIMENSION); throw megaglest_runtime_error(szBuf); - return; + //return; } if (w > MAX_MAP_CELL_DIMENSION || h > MAX_MAP_CELL_DIMENSION) { diff --git a/source/shared_lib/sources/platform/common/platform_common.cpp b/source/shared_lib/sources/platform/common/platform_common.cpp index 2eba912b..d3f42463 100644 --- a/source/shared_lib/sources/platform/common/platform_common.cpp +++ b/source/shared_lib/sources/platform/common/platform_common.cpp @@ -1684,9 +1684,9 @@ bool isKeyDown(int virtualKey) { // kinda hack and wrong... if(key >= 0) { - if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] keystate[key] = %d\n",__FILE__,__FUNCTION__,__LINE__,keystate[key]); + if(SystemFlags::getSystemSettingType(SystemFlags::debugSystem).enabled) SystemFlags::OutputDebug(SystemFlags::debugSystem,"In [%s::%s Line: %d] keystate[key] = %d\n",__FILE__,__FUNCTION__,__LINE__,keystate[(unsigned char)key]); - return (keystate[key] != 0); + return (keystate[(unsigned char)key] != 0); } switch(key) { case vkAdd: diff --git a/source/shared_lib/sources/platform/miniupnpc/miniwget.c b/source/shared_lib/sources/platform/miniupnpc/miniwget.c index f84f5f95..74f10512 100644 --- a/source/shared_lib/sources/platform/miniupnpc/miniwget.c +++ b/source/shared_lib/sources/platform/miniupnpc/miniwget.c @@ -90,8 +90,11 @@ getHTTPResponse(int s, int * size) int colon=0; int valuestart=0; if(header_buf_used + n > header_buf_len) { - header_buf = realloc(header_buf, header_buf_used + n); - header_buf_len = header_buf_used + n; + char *header_buf_new = realloc(header_buf, header_buf_used + n); + if(header_buf_new != NULL) { + header_buf = header_buf_new; + header_buf_len = header_buf_used + n; + } } memcpy(header_buf + header_buf_used, buf, n); header_buf_used += n; @@ -227,11 +230,15 @@ getHTTPResponse(int s, int * size) { if(content_length >= content_buf_used + (int)bytestocopy) { content_buf_len = content_length; - } else { + } + else { content_buf_len = content_buf_used + (int)bytestocopy; } - content_buf = (char *)realloc((void *)content_buf, + char *content_buf_new = (char *)realloc((void *)content_buf, content_buf_len); + if(content_buf_new) { + content_buf = content_buf_new; + } } memcpy(content_buf + content_buf_used, buf + i, bytestocopy); content_buf_used += bytestocopy; @@ -254,8 +261,11 @@ getHTTPResponse(int s, int * size) } else { content_buf_len = content_buf_used + n; } - content_buf = (char *)realloc((void *)content_buf, + char *content_buf_new = (char *)realloc((void *)content_buf, content_buf_len); + if(content_buf_new) { + content_buf = content_buf_new; + } } memcpy(content_buf + content_buf_used, buf, n); content_buf_used += n; diff --git a/source/shared_lib/sources/platform/posix/socket.cpp b/source/shared_lib/sources/platform/posix/socket.cpp index 4a857d03..b393fd59 100644 --- a/source/shared_lib/sources/platform/posix/socket.cpp +++ b/source/shared_lib/sources/platform/posix/socket.cpp @@ -1430,7 +1430,7 @@ int Socket::receive(void *data, int dataSize, bool tryReceiveUntilDataSizeMet) { else if(tryReceiveUntilDataSizeMet == true && bytesReceived < dataSize) { int newBufferSize = (dataSize - bytesReceived); if(SystemFlags::getSystemSettingType(SystemFlags::debugNetwork).enabled) SystemFlags::OutputDebug(SystemFlags::debugNetwork,"In [%s::%s Line: %d] WARNING, attempting to receive MORE data, bytesReceived = %d, dataSize = %d, newBufferSize = %d\n",__FILE__,__FUNCTION__,__LINE__,bytesReceived,dataSize,newBufferSize); - if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nIn [%s::%s Line: %d] WARNING, attempting to receive MORE data, bytesReceived = %d, dataSize = %d, newBufferSize = %d\n",__FILE__,__FUNCTION__,__LINE__,(int)bytesReceived,dataSize,newBufferSize,newBufferSize); + if(SystemFlags::VERBOSE_MODE_ENABLED) printf("\nIn [%s::%s Line: %d] WARNING, attempting to receive MORE data, bytesReceived = %d, dataSize = %d, newBufferSize = %d\n",__FILE__,__FUNCTION__,__LINE__,(int)bytesReceived,dataSize,newBufferSize); char *dataAsCharPointer = reinterpret_cast(data); int additionalBytes = receive(&dataAsCharPointer[bytesReceived], newBufferSize, tryReceiveUntilDataSizeMet); diff --git a/source/shared_lib/sources/platform/sdl/platform_util.cpp b/source/shared_lib/sources/platform/sdl/platform_util.cpp index fc3f5e12..66fea022 100644 --- a/source/shared_lib/sources/platform/sdl/platform_util.cpp +++ b/source/shared_lib/sources/platform/sdl/platform_util.cpp @@ -13,7 +13,8 @@ #include "util.h" #include "conversion.h" // For gcc backtrace on crash! -#if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__FreeBSD__) && !defined(BSD) +//#if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__FreeBSD__) && !defined(BSD) +#if defined(HAS_GCC_BACKTRACE) //#include #include @@ -65,7 +66,8 @@ void exceptionMessage(const exception &excp) { //int result = MessageBox(NULL, excp.what(), "Error", 0); } -#if defined(__GNUC__) && !defined(__FreeBSD__) && !defined(BSD) +//#if defined(__GNUC__) && !defined(__FreeBSD__) && !defined(BSD) +#if defined(HAS_GCC_BACKTRACE) static int getFileAndLine(char *function, void *address, char *file, size_t flen) { int line=-1; if(PlatformExceptionHandler::application_binary != "") { @@ -160,7 +162,8 @@ static int getFileAndLine(char *function, void *address, char *file, size_t flen string PlatformExceptionHandler::getStackTrace() { string errMsg = ""; -#if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__FreeBSD__) && !defined(BSD) +//#if defined(__GNUC__) && !defined(__MINGW32__) && !defined(__FreeBSD__) && !defined(BSD) +#if defined(HAS_GCC_BACKTRACE) // if(disableBacktrace == false && sdl_quitCalled == false) { errMsg = "\nStack Trace:\n"; //errMsg += "To find line #'s use:\n"; diff --git a/source/shared_lib/sources/platform/win32/platform_util.cpp b/source/shared_lib/sources/platform/win32/platform_util.cpp index f64eea18..5e7ee17a 100644 --- a/source/shared_lib/sources/platform/win32/platform_util.cpp +++ b/source/shared_lib/sources/platform/win32/platform_util.cpp @@ -64,7 +64,7 @@ std::string utf8_encode(const std::wstring wstr) { } // Convert an UTF8 string to a wide Unicode String -std::wstring utf8_decode(const std::string str) { +std::wstring utf8_decode(const std::string &str) { string friendly_path = str; replaceAll(friendly_path, "/", "\\"); replaceAll(friendly_path, "\\\\", "\\");