From 5105d2cb966d411bb7d6146446d0eb99f35a73f4 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Tue, 19 Jan 2021 14:15:02 +0100 Subject: [PATCH] Fix crash when persisting null relations --- src/Cache.cpp | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index 389df0a3..e2aecca2 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -2688,10 +2688,13 @@ Cache::saveTimelineMessages(lmdb::txn &txn, if (event.contains("content") && event["content"].contains("m.relates_to")) { - auto temp = event["content"]["m.relates_to"]; - std::string relates_to = temp.contains("m.in_reply_to") - ? temp["m.in_reply_to"]["event_id"] - : temp["event_id"]; + auto temp = event["content"]["m.relates_to"]; + json relates_to_j = temp.contains("m.in_reply_to") && + temp["m.in_reply_to"].is_object() + ? temp["m.in_reply_to"]["event_id"] + : temp["event_id"]; + std::string relates_to = + relates_to_j.is_string() ? relates_to_j.get() : ""; if (!relates_to.empty()) { lmdb::dbi_del(txn, @@ -2780,10 +2783,13 @@ Cache::saveTimelineMessages(lmdb::txn &txn, if (event.contains("content") && event["content"].contains("m.relates_to")) { - auto temp = event["content"]["m.relates_to"]; - std::string relates_to = temp.contains("m.in_reply_to") - ? temp["m.in_reply_to"]["event_id"] - : temp["event_id"]; + auto temp = event["content"]["m.relates_to"]; + json relates_to_j = temp.contains("m.in_reply_to") && + temp["m.in_reply_to"].is_object() + ? temp["m.in_reply_to"]["event_id"] + : temp["event_id"]; + std::string relates_to = + relates_to_j.is_string() ? relates_to_j.get() : ""; if (!relates_to.empty()) lmdb::dbi_put( @@ -2869,10 +2875,13 @@ Cache::saveOldMessages(const std::string &room_id, const mtx::responses::Message } if (event.contains("content") && event["content"].contains("m.relates_to")) { - auto temp = event["content"]["m.relates_to"]; - std::string relates_to = temp.contains("m.in_reply_to") - ? temp["m.in_reply_to"]["event_id"] - : temp["event_id"]; + auto temp = event["content"]["m.relates_to"]; + json relates_to_j = + temp.contains("m.in_reply_to") && temp["m.in_reply_to"].is_object() + ? temp["m.in_reply_to"]["event_id"] + : temp["event_id"]; + std::string relates_to = + relates_to_j.is_string() ? relates_to_j.get() : ""; if (!relates_to.empty()) lmdb::dbi_put(txn, relationsDb, lmdb::val(relates_to), event_id);