From c3303a1969069dbf3f7bf6372b52a846c56c2d23 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Tue, 23 Jul 2013 01:31:36 +0000 Subject: [PATCH] GXS: - fixed some possible crashs with dynamic_cast - fixed memory leak in p3GxsCircles::cache_load_for_token and p3Posted::background_loadMsgs git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@6532 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/services/p3gxscircles.cc | 7 ++ libretroshare/src/services/p3idservice.cc | 84 ++++++++++++---------- libretroshare/src/services/p3posted.cc | 2 + 3 files changed, 55 insertions(+), 38 deletions(-) diff --git a/libretroshare/src/services/p3gxscircles.cc b/libretroshare/src/services/p3gxscircles.cc index 27c414494..8d9e98f30 100644 --- a/libretroshare/src/services/p3gxscircles.cc +++ b/libretroshare/src/services/p3gxscircles.cc @@ -840,6 +840,12 @@ bool p3GxsCircles::cache_load_for_token(uint32_t token) for(; vit != grpData.end(); vit++) { RsGxsCircleGroupItem *item = dynamic_cast(*vit); + if (!item) + { + std::cerr << "Not a RsGxsCircleGroupItem Item, deleting!" << std::endl; + delete(*vit); + continue; + } RsGxsCircleGroup group; item->convertTo(group); @@ -861,6 +867,7 @@ bool p3GxsCircles::cache_load_for_token(uint32_t token) std::cerr << "p3GxsCircles::cache_load_for_token() Load ERROR: "; std::cerr << item->meta; std::cerr << std::endl; + delete(item); // ERROR. continue; } diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index 9c96639c4..96e0e6569 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -950,16 +950,22 @@ bool p3IdService::cache_load_for_token(uint32_t token) std::cerr << std::endl; #endif // DEBUG_IDS - std::vector grpData; - bool ok = RsGenExchange::getGroupData(token, grpData); + std::vector grpData; + bool ok = RsGenExchange::getGroupData(token, grpData); - if(ok) - { - std::vector::iterator vit = grpData.begin(); + if(ok) + { + std::vector::iterator vit = grpData.begin(); - for(; vit != grpData.end(); vit++) - { - RsGxsIdGroupItem* item = dynamic_cast(*vit); + for(; vit != grpData.end(); vit++) + { + RsGxsIdGroupItem* item = dynamic_cast(*vit); + if (!item) + { + std::cerr << "Not a RsGxsIdGroupItem Item, deleting!" << std::endl; + delete(*vit); + continue; + } #ifdef DEBUG_IDS std::cerr << "p3IdService::cache_load_for_token() Loaded Id with Meta: "; @@ -970,8 +976,8 @@ bool p3IdService::cache_load_for_token(uint32_t token) /* cache the data */ cache_store(item); delete item; - } - } + } + } else { std::cerr << "p3IdService::cache_load_for_token() ERROR no data"; @@ -982,8 +988,6 @@ bool p3IdService::cache_load_for_token(uint32_t token) return true; } - - bool p3IdService::cache_update_if_cached(const RsGxsId &id, std::string serviceString) { /* if these entries are cached - update with new info */ @@ -1055,45 +1059,50 @@ bool p3IdService::cache_load_ownids(uint32_t token) std::cerr << std::endl; #endif // DEBUG_IDS - std::vector grpData; - bool ok = RsGenExchange::getGroupData(token, grpData); + std::vector grpData; + bool ok = RsGenExchange::getGroupData(token, grpData); - if(ok) - { - std::vector::iterator vit = grpData.begin(); + if(ok) + { + std::vector::iterator vit = grpData.begin(); - // Save List + // Save List { RsStackMutex stack(mIdMtx); /********** STACK LOCKED MTX ******/ mOwnIds.clear(); - for(vit = grpData.begin(); vit != grpData.end(); vit++) - { - RsGxsIdGroupItem* item = dynamic_cast(*vit); - + for(vit = grpData.begin(); vit != grpData.end(); vit++) + { + RsGxsIdGroupItem* item = dynamic_cast(*vit); + if (!item) + { + std::cerr << "Not a IdOpinion Item, deleting!" << std::endl; + delete(*vit); + continue; + } if (item->meta.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN) { - mOwnIds.push_back(item->meta.mGroupId); - } - delete item ; - } + mOwnIds.push_back(item->meta.mGroupId); + } + delete item ; + } } -// No need to cache these items... -// as it just causes the cache to be flushed. + // No need to cache these items... + // as it just causes the cache to be flushed. #if 0 // Cache Items too. - for(vit = grpData.begin(); vit != grpData.end(); vit++) - { + for(vit = grpData.begin(); vit != grpData.end(); vit++) + { RsGxsIdGroupItem* item = dynamic_cast(*vit); if (item->meta.mSubscribeFlags & GXS_SERV::GROUP_SUBSCRIBE_ADMIN) { - + std::cerr << "p3IdService::cache_load_ownids() Loaded Id with Meta: "; std::cerr << item->meta; std::cerr << std::endl; - + /* cache the data */ cache_store(item); } @@ -1101,7 +1110,7 @@ bool p3IdService::cache_load_ownids(uint32_t token) } #endif - } + } else { std::cerr << "p3IdService::cache_load_ownids() ERROR no data"; @@ -1112,7 +1121,6 @@ bool p3IdService::cache_load_ownids(uint32_t token) return true; } - /************************************************************************************/ /************************************************************************************/ @@ -1565,16 +1573,16 @@ bool p3IdService::pgphash_handlerequest(uint32_t token) std::vector groupsToProcess; bool ok = getGroupData(token, groups); - if(ok) - { + if(ok) + { #ifdef DEBUG_IDS std::cerr << "p3IdService::pgphash_request() Have " << groups.size() << " Groups"; std::cerr << std::endl; #endif // DEBUG_IDS std::vector::iterator vit; - for(vit = groups.begin(); vit != groups.end(); vit++) - { + for(vit = groups.begin(); vit != groups.end(); vit++) + { #ifdef DEBUG_IDS std::cerr << "p3IdService::pgphash_request() Group Id: " << vit->mMeta.mGroupId; std::cerr << std::endl; diff --git a/libretroshare/src/services/p3posted.cc b/libretroshare/src/services/p3posted.cc index d2be791ac..b6afe5e2d 100644 --- a/libretroshare/src/services/p3posted.cc +++ b/libretroshare/src/services/p3posted.cc @@ -902,6 +902,7 @@ void p3Posted::background_loadMsgs(const uint32_t &token, bool unprocessed) std::cerr << std::endl; std::cerr << "p3Posted::background_loadMsgs() ERROR This should not happen"; std::cerr << std::endl; + delete(*vit); continue; } } @@ -999,6 +1000,7 @@ void p3Posted::background_loadMsgs(const uint32_t &token, bool unprocessed) RsGxsGrpMsgIdPair msgId = std::make_pair(groupId, (*vit)->meta.mMsgId); RsGenExchange::setMsgStatusFlags(token_a, msgId, 0, GXS_SERV::GXS_MSG_STATUS_UNPROCESSED); } + delete(*vit); } }