From 4a17403f9adad9b4390f7e0010c0f7e23a718f7b Mon Sep 17 00:00:00 2001 From: Tobias Fella <9750016+TobiasFella@users.noreply.github.com> Date: Fri, 10 Dec 2021 16:25:18 +0100 Subject: Apply suggestions from code review Co-authored-by: Alexey Rusakov --- lib/room.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'lib') diff --git a/lib/room.cpp b/lib/room.cpp index 458f870d..0a4fcc68 100644 --- a/lib/room.cpp +++ b/lib/room.cpp @@ -1509,16 +1509,18 @@ void Room::handleRoomKeyEvent(const RoomKeyEvent& roomKeyEvent, qCWarning(E2EE) << "added new inboundGroupSession:" << d->groupSessions.size(); for (const auto& eventId : d->undecryptedEvents[roomKeyEvent.sessionId()]) { - if (!d->eventsIndex.contains(eventId)) { + const auto pIdx = d->eventsIndex.constFind(eventId); + if (pIdx == d->eventsIndex.cend()) continue; - } - auto event = d->timeline.rend() - (d->eventsIndex.value(eventId) - minTimelineIndex() + 1); - if (auto encryptedEvent = event->viewAs()) { + auto& ti = d->timeline[Timeline::size_type(*pIdx - minTimelineIndex())]; + if (auto encryptedEvent = ti.viewAs()) { auto decrypted = decryptMessage(*encryptedEvent); if(decrypted) { - auto oldEvent = event->replaceEvent(std::move(decrypted)); - decrypted->setOriginalEvent(std::move(oldEvent)); - emit replacedEvent(event->event(), rawPtr(oldEvent)); + // The reference will survive the pointer being moved + auto& decryptedEvent = *decrypted; + auto oldEvent = ti.replaceEvent(std::move(decrypted)); + decryptedEvent.setOriginalEvent(std::move(oldEvent)); + emit replacedEvent(ti.event(), decrypted->originalEvent()); d->undecryptedEvents[roomKeyEvent.sessionId()] -= eventId; } } -- cgit v1.2.3