From 9a702e64449ba54fab28706c5b9c5ff9f2a41f8a Mon Sep 17 00:00:00 2001 From: Yohann Ferreira Date: Fri, 2 Dec 2011 00:54:23 +0100 Subject: Hopefully fixed the crash related to player relations. I removed all the misuse of std::map::operator[] I could find, especially there: if (!(*container)[name]) where the [] was creating the entry while checking for its existence. This should avoid the memory corruption problem seen in the Mana-Mantis #400. Patch tested in game by: Ablu. Resolves: Mana Mantis #400 Reviewed-by: Ablu. --- src/playerrelations.cpp | 71 ++++++++++++++++++++++++++++++------------------- src/playerrelations.h | 4 +++ 2 files changed, 48 insertions(+), 27 deletions(-) (limited to 'src') diff --git a/src/playerrelations.cpp b/src/playerrelations.cpp index 4a70e695..635c92d0 100644 --- a/src/playerrelations.cpp +++ b/src/playerrelations.cpp @@ -60,7 +60,9 @@ class PlayerConfSerialiser : public ConfigurationListManager::iterator it = + (*container).find(name); + if (it != (*container).end()) { int v = (int)cobj->getValue(RELATION, PlayerRelation::NEUTRAL); (*container)[name] = new PlayerRelation(static_cast(v)); @@ -182,31 +184,37 @@ void PlayerRelationsManager::signalUpdate(const std::string &name) } } -unsigned int PlayerRelationsManager::checkPermissionSilently(const std::string &player_name, unsigned int flags) +unsigned int PlayerRelationsManager::checkPermissionSilently( + const std::string &playerName, + unsigned int flags) { - PlayerRelation *r = mRelations[player_name]; + PlayerRelation *r = 0; + + std::map::const_iterator it = + mRelations.find(playerName); + if (it != mRelations.end()) + r = it->second; if (!r) return mDefaultPermissions & flags; - else - { - unsigned int permissions = PlayerRelation::RELATION_PERMISSIONS[r->mRelation]; - switch (r->mRelation) - { - case PlayerRelation::NEUTRAL: - permissions = mDefaultPermissions; - break; + unsigned int permissions = + PlayerRelation::RELATION_PERMISSIONS[r->mRelation]; - case PlayerRelation::FRIEND: - permissions |= mDefaultPermissions; // widen - break; + switch (r->mRelation) + { + case PlayerRelation::NEUTRAL: + permissions = mDefaultPermissions; + break; - default: - permissions &= mDefaultPermissions; // narrow - } + case PlayerRelation::FRIEND: + permissions |= mDefaultPermissions; // widen + break; - return permissions & flags; + default: + permissions &= mDefaultPermissions; // narrow } + + return permissions & flags; } bool PlayerRelationsManager::hasPermission(Being *being, unsigned int flags) @@ -237,16 +245,21 @@ bool PlayerRelationsManager::hasPermission(const std::string &name, return permitted; } -void PlayerRelationsManager::setRelation(const std::string &player_name, +void PlayerRelationsManager::setRelation(const std::string &playerName, PlayerRelation::Relation relation) { - PlayerRelation *r = mRelations[player_name]; - if (r == NULL) - mRelations[player_name] = new PlayerRelation(relation); + PlayerRelation *r = 0; + + std::map::iterator it = + mRelations.find(playerName); + if (it != mRelations.end()) + r = it->second; + if (!r) + mRelations[playerName] = new PlayerRelation(relation); else r->mRelation = relation; - signalUpdate(player_name); + signalUpdate(playerName); } std::vector * PlayerRelationsManager::getPlayers() const @@ -264,12 +277,16 @@ std::vector * PlayerRelationsManager::getPlayers() const void PlayerRelationsManager::removePlayer(const std::string &name) { - if (mRelations[name]) - delete mRelations[name]; + std::map::iterator it = + mRelations.find(name); + if (it != mRelations.end()) + { + delete it->second; - mRelations.erase(name); + mRelations.erase(it); - signalUpdate(name); + signalUpdate(name); + } } diff --git a/src/playerrelations.h b/src/playerrelations.h index fd76aa03..653b4c63 100644 --- a/src/playerrelations.h +++ b/src/playerrelations.h @@ -37,6 +37,10 @@ struct PlayerRelation static const unsigned int WHISPER = (1 << 3); static const unsigned int TRADE = (1 << 4); + /** + * There are four types of relations: + * NEUTRAL, FRIEND, DISREGARDED, and IGNORED. + */ static const unsigned int RELATIONS_NR = 4; static const unsigned int RELATION_PERMISSIONS[RELATIONS_NR]; -- cgit v1.2.3-70-g09d2