diff options
author | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2011-12-02 00:54:23 +0100 |
---|---|---|
committer | Yohann Ferreira <yohann_dot_ferreira_at_orange_dot_efer> | 2012-01-09 06:58:35 +0100 |
commit | 9a702e64449ba54fab28706c5b9c5ff9f2a41f8a (patch) | |
tree | 311b1a8aac33a4796a2a4f12d6aad6b543246f7f | |
parent | f54cd905f49d08116bf988bcb4451beb168cbd48 (diff) | |
download | mana-9a702e64449ba54fab28706c5b9c5ff9f2a41f8a.tar.gz mana-9a702e64449ba54fab28706c5b9c5ff9f2a41f8a.tar.bz2 mana-9a702e64449ba54fab28706c5b9c5ff9f2a41f8a.tar.xz mana-9a702e64449ba54fab28706c5b9c5ff9f2a41f8a.zip |
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.
-rw-r--r-- | src/playerrelations.cpp | 71 | ||||
-rw-r--r-- | src/playerrelations.h | 4 |
2 files changed, 48 insertions, 27 deletions
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<std::pair<std::stri if (name.empty()) return container; - if (!(*container)[name]) + std::map<std::string, PlayerRelation *>::iterator it = + (*container).find(name); + if (it != (*container).end()) { int v = (int)cobj->getValue(RELATION, PlayerRelation::NEUTRAL); (*container)[name] = new PlayerRelation(static_cast<PlayerRelation::Relation>(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<std::string, PlayerRelation *>::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<std::string, PlayerRelation *>::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<std::string> * PlayerRelationsManager::getPlayers() const @@ -264,12 +277,16 @@ std::vector<std::string> * PlayerRelationsManager::getPlayers() const void PlayerRelationsManager::removePlayer(const std::string &name) { - if (mRelations[name]) - delete mRelations[name]; + std::map<std::string, PlayerRelation *>::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]; |