From 156291b2c15895c41bd3d096d03a88433de49084 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 12 Jan 2019 20:38:50 +0100 Subject: Fix crash when sending message with no previous message --- include/dchat/Room.hpp | 23 +++++++++++++++-------- src/Room.cpp | 13 ++++++++----- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/include/dchat/Room.hpp b/include/dchat/Room.hpp index 9eef4de..6980bfc 100644 --- a/include/dchat/Room.hpp +++ b/include/dchat/Room.hpp @@ -26,6 +26,7 @@ namespace dchat class Room { DISABLE_COPY(Room) + friend class Rooms; public: Room(Rooms *rooms, std::shared_ptr id); // Throws exception on failure if we are not allowed to add the user to the group @@ -47,21 +48,26 @@ namespace dchat std::shared_ptr id; std::shared_ptr encryptionKey; std::string name; - odhtdb::Signature::MapPublicKey> userByPublicKey; - std::vector> groups; - std::vector messages; std::shared_ptr localUser; - // Used for local users - odhtdb::Signature::MapPublicKey> publicKeyToKeyPairMap; std::string inviteKey; void *userdata; + + // TODO: Move to private when we have proper support for groups + std::vector> groups; + private: + odhtdb::Signature::MapPublicKey> userByPublicKey; + // Used for local users + odhtdb::Signature::MapPublicKey> publicKeyToKeyPairMap; + + std::vector> messages; }; struct RoomAddMessageRequest { std::shared_ptr room; bool loadedFromCache; - RoomMessage message; + std::shared_ptr message; + std::shared_ptr prevMessage; }; struct UserChangeNicknameRequest @@ -134,7 +140,8 @@ namespace dchat friend Room; DISABLE_COPY(Rooms) public: - // @callbackFuncs.connectCallbackFunc can't be null + // @callbackFuncs.connectCallbackFunc can't be null. + // @address is used in another thread so it may need to live beyond the scope which Rooms::connect is called from static void connect(const char *address, u16 port, RoomCallbackFuncs callbackFuncs); // Throws on failure void loginUser(const std::string &username, const std::string &password); @@ -164,4 +171,4 @@ namespace dchat odhtdb::MapHash> waitingToJoinRoom; std::recursive_mutex waitingToJoinRoomMutex; }; -} \ No newline at end of file +} diff --git a/src/Room.cpp b/src/Room.cpp index a8a8d6a..c7135da 100644 --- a/src/Room.cpp +++ b/src/Room.cpp @@ -281,16 +281,19 @@ namespace dchat { case RoomDataType::ADD_MESSAGE: { - RoomMessage message; - message.id = *request.requestHash; - message.creator = user; - message.timestampSeconds = timestampSeconds; - message.text = std::string((const char*)request.decryptedData.data + 1, request.decryptedData.size - 1); + auto message = std::make_shared(); + message->id = *request.requestHash; + message->creator = user; + message->timestampSeconds = timestampSeconds; + message->text = std::string((const char*)request.decryptedData.data + 1, request.decryptedData.size - 1); RoomAddMessageRequest roomRequest; roomRequest.room = room; roomRequest.loadedFromCache = request.loadedFromCache; roomRequest.message = std::move(message); + roomRequest.prevMessage = nullptr; + if(!room->messages.empty()) + roomRequest.prevMessage = room->messages.back(); if(callbackFuncs.addMessageCallbackFunc) callbackFuncs.addMessageCallbackFunc(roomRequest); -- cgit v1.2.3