diff options
author | Tobias Markmann <tm@ayena.de> | 2017-03-30 07:32:28 (GMT) |
---|---|---|
committer | Tobias Markmann <tm@ayena.de> | 2017-03-31 08:45:43 (GMT) |
commit | 7663ca75731c4313dddbcde4d85f10383644a67a (patch) | |
tree | ecb1ed4c08b71bb58efc61980166e5064fefe68e /Swift/Controllers/Roster/GroupRosterItem.cpp | |
parent | 4d0391d824aaf94fbe152778581d51fecd588f6c (diff) | |
download | swift-7663ca75731c4313dddbcde4d85f10383644a67a.zip swift-7663ca75731c4313dddbcde4d85f10383644a67a.tar.bz2 |
Return unique_ptr instead of pointer to deleted object
Coverity raised this issue.
Test-Information:
Code builds on macOS 10.12.4 and all unit tests pass; Swift
runs fine.
Change-Id: I8fb0805f6b2e0a21674ea32c0b1aee9e7b985639
Diffstat (limited to 'Swift/Controllers/Roster/GroupRosterItem.cpp')
-rw-r--r-- | Swift/Controllers/Roster/GroupRosterItem.cpp | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/Swift/Controllers/Roster/GroupRosterItem.cpp b/Swift/Controllers/Roster/GroupRosterItem.cpp index ac40afd..af5d0ca 100644 --- a/Swift/Controllers/Roster/GroupRosterItem.cpp +++ b/Swift/Controllers/Roster/GroupRosterItem.cpp @@ -1,13 +1,14 @@ /* - * Copyright (c) 2010-2016 Isode Limited. + * Copyright (c) 2010-2017 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #include <Swift/Controllers/Roster/GroupRosterItem.h> + +#include <memory> + #include <boost/bind.hpp> -//#include <boost/algorithm.hpp> -#include <iostream> namespace Swift { @@ -97,23 +98,22 @@ void GroupRosterItem::removeAll() { * Returns the removed item - but only if it's the only one, otherwise * the return result is undefined. */ -ContactRosterItem* GroupRosterItem::removeChild(const JID& jid) { - std::vector<RosterItem*>::iterator it = children_.begin(); - ContactRosterItem* removed = nullptr; +std::unique_ptr<ContactRosterItem> GroupRosterItem::removeChild(const JID& jid) { + auto it = children_.begin(); + std::unique_ptr<ContactRosterItem> removed; while (it != children_.end()) { ContactRosterItem* contact = dynamic_cast<ContactRosterItem*>(*it); if (contact && contact->getJID() == jid) { displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), contact), displayedChildren_.end()); - removed = contact; - delete contact; + removed = std::unique_ptr<ContactRosterItem>(contact); it = children_.erase(it); continue; } GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it); if (group) { - ContactRosterItem* groupRemoved = group->removeChild(jid); + auto groupRemoved = group->removeChild(jid); if (groupRemoved) { - removed = groupRemoved; + removed = std::move(groupRemoved); } } ++it; @@ -123,15 +123,14 @@ ContactRosterItem* GroupRosterItem::removeChild(const JID& jid) { return removed; } -GroupRosterItem* GroupRosterItem::removeGroupChild(const std::string& groupName) { +std::unique_ptr<GroupRosterItem> GroupRosterItem::removeGroupChild(const std::string& groupName) { std::vector<RosterItem*>::iterator it = children_.begin(); - GroupRosterItem* removed = nullptr; + std::unique_ptr<GroupRosterItem> removed; while (it != children_.end()) { GroupRosterItem* group = dynamic_cast<GroupRosterItem*>(*it); if (group && group->getDisplayName() == groupName) { displayedChildren_.erase(std::remove(displayedChildren_.begin(), displayedChildren_.end(), group), displayedChildren_.end()); - removed = group; - delete group; + removed = std::unique_ptr<GroupRosterItem>(group); it = children_.erase(it); continue; } |