From dbea031a8601dae57a635f28bceffb3b4134ea53 Mon Sep 17 00:00:00 2001 From: Nicolas Werner Date: Wed, 11 Aug 2021 00:21:02 +0200 Subject: [PATCH] Fix potential crash when trying to read room info too early --- src/Cache.cpp | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/Cache.cpp b/src/Cache.cpp index eb9fb028..b79a3b47 100644 --- a/src/Cache.cpp +++ b/src/Cache.cpp @@ -1586,26 +1586,32 @@ Cache::roomsWithStateUpdates(const mtx::responses::Sync &res) RoomInfo Cache::singleRoomInfo(const std::string &room_id) { - auto txn = ro_txn(env_); - auto statesdb = getStatesDb(txn, room_id); + auto txn = ro_txn(env_); - std::string_view data; + try { + auto statesdb = getStatesDb(txn, room_id); - // Check if the room is joined. - if (roomsDb_.get(txn, room_id, data)) { - try { - RoomInfo tmp = json::parse(data); - tmp.member_count = getMembersDb(txn, room_id).size(txn); - tmp.join_rule = getRoomJoinRule(txn, statesdb); - tmp.guest_access = getRoomGuestAccess(txn, statesdb); + std::string_view data; - return tmp; - } catch (const json::exception &e) { - nhlog::db()->warn("failed to parse room info: room_id ({}), {}: {}", - room_id, - std::string(data.data(), data.size()), - e.what()); + // Check if the room is joined. + if (roomsDb_.get(txn, room_id, data)) { + try { + RoomInfo tmp = json::parse(data); + tmp.member_count = getMembersDb(txn, room_id).size(txn); + tmp.join_rule = getRoomJoinRule(txn, statesdb); + tmp.guest_access = getRoomGuestAccess(txn, statesdb); + + return tmp; + } catch (const json::exception &e) { + nhlog::db()->warn("failed to parse room info: room_id ({}), {}: {}", + room_id, + std::string(data.data(), data.size()), + e.what()); + } } + } catch (const lmdb::error &e) { + nhlog::db()->warn( + "failed to read room info from db: room_id ({}), {}", room_id, e.what()); } return RoomInfo();