From f5218cdfc30dd28739600dcf7196fda7958f4965 Mon Sep 17 00:00:00 2001 From: thunder2 Date: Sun, 21 Jul 2013 01:50:13 +0000 Subject: [PATCH] Fixed crash after last commit in p3idservice.cc. Fixed memory leaks and possible crashs in ::getGroupData - p3gxschannels - p3gxscircles - p3gxsforums - p3idservice - p3posted - p3photoservice git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@6524 b45a01b8-16f6-495d-af2f-9b41ad6348cc --- libretroshare/src/gxs/rsgenexchange.cc | 65 +++++++-------- libretroshare/src/services/p3gxschannels.cc | 11 +-- libretroshare/src/services/p3gxscircles.cc | 17 +++- libretroshare/src/services/p3gxsforums.cc | 28 ++++--- libretroshare/src/services/p3idservice.cc | 84 +++++++++++--------- libretroshare/src/services/p3photoservice.cc | 25 +++--- libretroshare/src/services/p3posted.cc | 22 +++-- 7 files changed, 143 insertions(+), 109 deletions(-) diff --git a/libretroshare/src/gxs/rsgenexchange.cc b/libretroshare/src/gxs/rsgenexchange.cc index 81fe033b2..5133d79ca 100644 --- a/libretroshare/src/gxs/rsgenexchange.cc +++ b/libretroshare/src/gxs/rsgenexchange.cc @@ -1109,7 +1109,6 @@ bool RsGenExchange::getMsgRelatedMeta(const uint32_t &token, GxsMsgRelatedMetaMa bool RsGenExchange::getGroupData(const uint32_t &token, std::vector& grpItem) { - std::list nxsGrps; bool ok = mDataAccess->getGroupData(token, nxsGrps); @@ -1118,7 +1117,7 @@ bool RsGenExchange::getGroupData(const uint32_t &token, std::vectormeta = *((*lit)->metaData); - grpItem.push_back(gItem); + grpItem.push_back(gItem); } else { @@ -1148,46 +1147,42 @@ bool RsGenExchange::getGroupData(const uint32_t &token, std::vectorgetMsgData(token, msgResult); + NxsMsgDataResult::iterator mit = msgResult.begin(); - RsStackMutex stack(mGenMtx); - NxsMsgDataResult msgResult; - bool ok = mDataAccess->getMsgData(token, msgResult); - NxsMsgDataResult::iterator mit = msgResult.begin(); + if(ok) + { + for(; mit != msgResult.end(); mit++) + { + std::vector gxsMsgItems; + const RsGxsGroupId& grpId = mit->first; + std::vector& nxsMsgsV = mit->second; + std::vector::iterator vit = nxsMsgsV.begin(); + for(; vit != nxsMsgsV.end(); vit++) + { + RsNxsMsg*& msg = *vit; + RsItem* item = NULL; - if(ok) - { - for(; mit != msgResult.end(); mit++) - { - std::vector gxsMsgItems; - const RsGxsGroupId& grpId = mit->first; - std::vector& nxsMsgsV = mit->second; - std::vector::iterator vit - = nxsMsgsV.begin(); - for(; vit != nxsMsgsV.end(); vit++) - { - RsNxsMsg*& msg = *vit; - RsItem* item = NULL; - - if(msg->msg.bin_len != 0) - item = mSerialiser->deserialise(msg->msg.bin_data, - &msg->msg.bin_len); + if(msg->msg.bin_len != 0) + item = mSerialiser->deserialise(msg->msg.bin_data, &msg->msg.bin_len); if (item) { RsGxsMsgItem* mItem = dynamic_cast(item); if (mItem) { - mItem->meta = *((*vit)->metaData); // get meta info from nxs msg - gxsMsgItems.push_back(mItem); + mItem->meta = *((*vit)->metaData); // get meta info from nxs msg + gxsMsgItems.push_back(mItem); } else { @@ -1205,12 +1200,12 @@ bool RsGenExchange::getMsgData(const uint32_t &token, std::cerr << std::endl; #endif } - delete msg; - } - msgItems[grpId] = gxsMsgItems; - } + delete msg; } - return ok; + msgItems[grpId] = gxsMsgItems; + } + } + return ok; } bool RsGenExchange::getMsgRelatedData(const uint32_t &token, GxsMsgRelatedDataMap &msgItems) diff --git a/libretroshare/src/services/p3gxschannels.cc b/libretroshare/src/services/p3gxschannels.cc index b2d5655e7..96fee6a69 100644 --- a/libretroshare/src/services/p3gxschannels.cc +++ b/libretroshare/src/services/p3gxschannels.cc @@ -175,7 +175,7 @@ bool p3GxsChannels::getGroupData(const uint32_t &token, std::vector grpData; bool ok = RsGenExchange::getGroupData(token, grpData); - + if(ok) { std::vector::iterator vit = grpData.begin(); @@ -194,6 +194,7 @@ bool p3GxsChannels::getGroupData(const uint32_t &token, std::vectorfirst; std::vector& msgItems = mit->second; std::vector::iterator vit = msgItems.begin(); - + for(; vit != msgItems.end(); vit++) { RsGxsChannelPostItem* item = dynamic_cast(*vit); - + if(item) { RsGxsChannelPost msg; @@ -253,7 +254,7 @@ bool p3GxsChannels::getPostData(const uint32_t &token, std::vector(*vit); - RsGxsCircleGroup group; - item->convertTo(group); + if (item) + { + RsGxsCircleGroup group; + item->convertTo(group); - // If its cached - add that info (TODO). - groups.push_back(group); + // If its cached - add that info (TODO). + groups.push_back(group); + delete(item); + } + else + { + std::cerr << "Not a RsGxsCircleGroupItem, deleting!" << std::endl; + delete *vit; + } } } diff --git a/libretroshare/src/services/p3gxsforums.cc b/libretroshare/src/services/p3gxsforums.cc index a01eee405..6010ddc3d 100644 --- a/libretroshare/src/services/p3gxsforums.cc +++ b/libretroshare/src/services/p3gxsforums.cc @@ -102,11 +102,19 @@ bool p3GxsForums::getGroupData(const uint32_t &token, std::vector(*vit); - RsGxsForumGroup grp = item->mGroup; - item->mGroup.mMeta = item->meta; - grp.mMeta = item->mGroup.mMeta; - delete item; - groups.push_back(grp); + if (item) + { + RsGxsForumGroup grp = item->mGroup; + item->mGroup.mMeta = item->meta; + grp.mMeta = item->mGroup.mMeta; + delete item; + groups.push_back(grp); + } + else + { + std::cerr << "Not a GxsForumGrpItem, deleting!" << std::endl; + delete *vit; + } } } return ok; @@ -121,21 +129,21 @@ bool p3GxsForums::getMsgData(const uint32_t &token, std::vector & { GxsMsgDataMap msgData; bool ok = RsGenExchange::getMsgData(token, msgData); - + if(ok) { GxsMsgDataMap::iterator mit = msgData.begin(); - + for(; mit != msgData.end(); mit++) { RsGxsGroupId grpId = mit->first; std::vector& msgItems = mit->second; std::vector::iterator vit = msgItems.begin(); - + for(; vit != msgItems.end(); vit++) { RsGxsForumMsgItem* item = dynamic_cast(*vit); - + if(item) { RsGxsForumMsg msg = item->mMsg; @@ -151,7 +159,7 @@ bool p3GxsForums::getMsgData(const uint32_t &token, std::vector & } } } - + return ok; } diff --git a/libretroshare/src/services/p3idservice.cc b/libretroshare/src/services/p3idservice.cc index 210c77883..a01cb38de 100644 --- a/libretroshare/src/services/p3idservice.cc +++ b/libretroshare/src/services/p3idservice.cc @@ -397,72 +397,78 @@ bool p3IdService::getReputation(const RsGxsId &id, GixsReputation &rep) bool p3IdService::getGroupData(const uint32_t &token, std::vector &groups) { + 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); - RsGxsIdGroup group = item->group; - group.mMeta = item->meta; - - // Decode information from serviceString. - SSGxsIdGroup ssdata; - if (ssdata.load(group.mMeta.mServiceString)) + for(; vit != grpData.end(); vit++) + { + RsGxsIdGroupItem* item = dynamic_cast(*vit); + if (item) { - group.mPgpKnown = ssdata.pgp.idKnown; - group.mPgpId = ssdata.pgp.pgpId; + RsGxsIdGroup group = item->group; + group.mMeta = item->meta; + + // Decode information from serviceString. + SSGxsIdGroup ssdata; + if (ssdata.load(group.mMeta.mServiceString)) + { + group.mPgpKnown = ssdata.pgp.idKnown; + group.mPgpId = ssdata.pgp.pgpId; #ifdef DEBUG_IDS - std::cerr << "p3IdService::getGroupData() Success decoding ServiceString"; - std::cerr << std::endl; - std::cerr << "\t mGpgKnown: " << group.mPgpKnown; - std::cerr << std::endl; - std::cerr << "\t mGpgId: " << group.mPgpId; - std::cerr << std::endl; + std::cerr << "p3IdService::getGroupData() Success decoding ServiceString"; + std::cerr << std::endl; + std::cerr << "\t mGpgKnown: " << group.mPgpKnown; + std::cerr << std::endl; + std::cerr << "\t mGpgId: " << group.mPgpId; + std::cerr << std::endl; #endif // DEBUG_IDS + } + else + { + group.mPgpKnown = false; + group.mPgpId = ""; + + std::cerr << "p3IdService::getGroupData() Failed to decode ServiceString"; + std::cerr << std::endl; + } + + groups.push_back(group); + delete(item); } else { - group.mPgpKnown = false; - group.mPgpId = ""; - - std::cerr << "p3IdService::getGroupData() Failed to decode ServiceString"; - std::cerr << std::endl; + std::cerr << "Not a Id Item, deleting!" << std::endl; + delete(*vit); } + } + } - groups.push_back(group); - } - delete *vit ; - } - - return ok; + return ok; } - bool p3IdService::getMsgData(const uint32_t &token, std::vector &opinions) { GxsMsgDataMap msgData; bool ok = RsGenExchange::getMsgData(token, msgData); - + if(ok) { GxsMsgDataMap::iterator mit = msgData.begin(); - + for(; mit != msgData.end(); mit++) { RsGxsGroupId grpId = mit->first; std::vector& msgItems = mit->second; std::vector::iterator vit = msgItems.begin(); - + for(; vit != msgItems.end(); vit++) { RsGxsIdOpinionItem* item = dynamic_cast(*vit); - + if (item) { RsGxsIdOpinion opinion = item->opinion; diff --git a/libretroshare/src/services/p3photoservice.cc b/libretroshare/src/services/p3photoservice.cc index de93a1a96..d1138c6bb 100644 --- a/libretroshare/src/services/p3photoservice.cc +++ b/libretroshare/src/services/p3photoservice.cc @@ -207,18 +207,25 @@ bool p3PhotoService::getAlbum(const uint32_t& token, std::vector& for(; vit != grpData.end(); vit++) { RsGxsPhotoAlbumItem* item = dynamic_cast(*vit); - RsPhotoAlbum album = item->album; - item->album.mMeta = item->meta; - album.mMeta = item->album.mMeta; - delete item; - albums.push_back(album); + if (item) + { + RsPhotoAlbum album = item->album; + item->album.mMeta = item->meta; + album.mMeta = item->album.mMeta; + delete item; + albums.push_back(album); + } + else + { + std::cerr << "Not a RsGxsPhotoAlbumItem, deleting!" << std::endl; + delete *vit; + } } } return ok; } - bool p3PhotoService::getPhoto(const uint32_t& token, PhotoResult& photos) { GxsMsgDataMap msgData; @@ -240,14 +247,14 @@ bool p3PhotoService::getPhoto(const uint32_t& token, PhotoResult& photos) if(item) { - RsPhotoPhoto photo = item->photo; + RsPhotoPhoto photo = item->photo; photo.mMeta = item->meta; photos[grpId].push_back(photo); delete item; }else { - std::cerr << "Not a photo Item, deleting!" << std::endl; - delete *vit; + std::cerr << "Not a photo Item, deleting!" << std::endl; + delete *vit; } } } diff --git a/libretroshare/src/services/p3posted.cc b/libretroshare/src/services/p3posted.cc index dafde24a7..d2be791ac 100644 --- a/libretroshare/src/services/p3posted.cc +++ b/libretroshare/src/services/p3posted.cc @@ -179,11 +179,19 @@ bool p3Posted::getGroupData(const uint32_t &token, std::vector &g for(; vit != grpData.end(); vit++) { RsGxsPostedGroupItem* item = dynamic_cast(*vit); - RsPostedGroup grp = item->mGroup; - item->mGroup.mMeta = item->meta; - grp.mMeta = item->mGroup.mMeta; - delete item; - groups.push_back(grp); + if (item) + { + RsPostedGroup grp = item->mGroup; + item->mGroup.mMeta = item->meta; + grp.mMeta = item->mGroup.mMeta; + delete item; + groups.push_back(grp); + } + else + { + std::cerr << "Not a RsGxsPostedGroupItem, deleting!" << std::endl; + delete *vit; + } } } return ok; @@ -194,7 +202,7 @@ bool p3Posted::getPostData(const uint32_t &token, std::vector &msg GxsMsgDataMap msgData; bool ok = RsGenExchange::getMsgData(token, msgData); time_t now = time(NULL); - + if(ok) { GxsMsgDataMap::iterator mit = msgData.begin(); @@ -208,7 +216,7 @@ bool p3Posted::getPostData(const uint32_t &token, std::vector &msg for(; vit != msgItems.end(); vit++) { RsGxsPostedPostItem* item = dynamic_cast(*vit); - + if(item) { RsPostedPost msg = item->mPost;