diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-04-03 18:32:14 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-04-03 18:32:14 +0900 |
commit | d76b2fbe30a503009d33c4df06848d7356fc39a6 (patch) | |
tree | f2c2476ed6f89cab6c4d5b3b8247fdccc041c865 /lib/events/directchatevent.cpp | |
parent | d9f09be82b716c5261a36feb6006c4978d989d9e (diff) | |
download | libquotient-d76b2fbe30a503009d33c4df06848d7356fc39a6.tar.gz libquotient-d76b2fbe30a503009d33c4df06848d7356fc39a6.zip |
DirectChatEvent: be careful with range-for over temporaries
...because temporaries returned by temporaries tend to disappear before you enter the loop body (see the bottom of http://en.cppreference.com/w/cpp/language/range-for#Explanation).
Diffstat (limited to 'lib/events/directchatevent.cpp')
-rw-r--r-- | lib/events/directchatevent.cpp | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/lib/events/directchatevent.cpp b/lib/events/directchatevent.cpp index 7049d967..63d638a3 100644 --- a/lib/events/directchatevent.cpp +++ b/lib/events/directchatevent.cpp @@ -29,8 +29,15 @@ DirectChatEvent::DirectChatEvent(const QJsonObject& obj) QMultiHash<QString, QString> DirectChatEvent::usersToDirectChats() const { QMultiHash<QString, QString> result; - for (auto it = contentJson().begin(); it != contentJson().end(); ++it) - for (auto roomIdValue: it.value().toArray()) + const auto json = contentJson(); + for (auto it = json.begin(); it != json.end(); ++it) + { + // Beware of range-for's over temporary returned from temporary + // (see the bottom of + // http://en.cppreference.com/w/cpp/language/range-for#Explanation) + const auto roomIds = it.value().toArray(); + for (const auto& roomIdValue: roomIds) result.insert(it.key(), roomIdValue.toString()); + } return result; } |