diff options
author | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-08-11 15:40:42 +0900 |
---|---|---|
committer | Kitsune Ral <Kitsune-Ral@users.sf.net> | 2018-08-11 15:40:42 +0900 |
commit | 4442a820c8617fbb5352c44456105c0571143f58 (patch) | |
tree | 5dfacfc16958f6c70652ccf73a97d5436956d99d | |
parent | b028dff6bf6074fdeea5d6f50e97df074f53b45b (diff) | |
download | libquotient-4442a820c8617fbb5352c44456105c0571143f58.tar.gz libquotient-4442a820c8617fbb5352c44456105c0571143f58.zip |
Connection: don't crash on invalid user ids coming from the server
Closes #230.
-rw-r--r-- | lib/connection.cpp | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp index ec29bbc1..73fd786f 100644 --- a/lib/connection.cpp +++ b/lib/connection.cpp @@ -364,14 +364,18 @@ void Connection::onSyncSuccess(SyncData &&data) { DirectChatsMap additions; for (auto it = usersToDCs.begin(); it != usersToDCs.end(); ++it) { - const auto* u = user(it.key()); - if (!d->directChats.contains(u, it.value())) + if (const auto* u = user(it.key())) { - additions.insert(u, it.value()); - d->directChats.insert(u, it.value()); - qCDebug(MAIN) << "Marked room" << it.value() - << "as a direct chat with" << u->id(); - } + if (!d->directChats.contains(u, it.value())) + { + additions.insert(u, it.value()); + d->directChats.insert(u, it.value()); + qCDebug(MAIN) << "Marked room" << it.value() + << "as a direct chat with" << u->id(); + } + } else + qCWarning(MAIN) + << "Couldn't get a user object for" << it.key(); } if (!additions.isEmpty() || !removals.isEmpty()) emit directChatsListChanged(additions, removals); @@ -591,6 +595,7 @@ void Connection::doInDirectChat(const User* u, << "has been created as" << j->roomId(); operation(room(j->roomId(), JoinState::Join)); }); + } CreateRoomJob* Connection::createDirectChat(const QString& userId, @@ -701,7 +706,13 @@ Room* Connection::invitation(const QString& roomId) const User* Connection::user(const QString& userId) { - Q_ASSERT(userId.startsWith('@') && userId.contains(':')); + if (userId.isEmpty()) + return nullptr; + if (userId.front() != '@' || !userId.contains(':')) + { + qCCritical(MAIN) << "Malformed userId:" << userId; + return nullptr; + } if( d->userMap.contains(userId) ) return d->userMap.value(userId); auto* user = userFactory(this, userId); @@ -712,12 +723,12 @@ User* Connection::user(const QString& userId) const User* Connection::user() const { - return d->userId.isEmpty() ? nullptr : d->userMap.value(d->userId, nullptr); + return d->userMap.value(d->userId, nullptr); } User* Connection::user() { - return d->userId.isEmpty() ? nullptr : user(d->userId); + return user(d->userId); } QString Connection::userId() const |