mirror of
https://github.com/SWG-Source/stationapi.git
synced 2026-01-15 22:04:17 -05:00
Fixed a potential situation where the list of rooms returned for an avatar could potentially contain a dangling pointer.
This commit is contained in:
@@ -89,19 +89,3 @@ bool ChatAvatar::IsIgnored(const ChatAvatar* avatar) {
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<ChatRoom*> ChatAvatar::GetJoinedRooms() {
|
||||
return rooms_;
|
||||
}
|
||||
|
||||
void ChatAvatar::JoinRoom(ChatRoom * room) {
|
||||
rooms_.push_back(room);
|
||||
}
|
||||
|
||||
void ChatAvatar::UnjoinRoom(ChatRoom * room) {
|
||||
if (rooms_.empty()) return;
|
||||
|
||||
rooms_.erase(std::remove_if(std::begin(rooms_), std::end(rooms_), [room](const auto joinedRoom) {
|
||||
return room->GetRoomId() == joinedRoom->GetRoomId();
|
||||
}));
|
||||
}
|
||||
|
||||
@@ -75,10 +75,6 @@ public:
|
||||
|
||||
const std::vector<IgnoreContact> GetIgnoreList() const { return ignoreList_; }
|
||||
|
||||
std::vector<ChatRoom*> GetJoinedRooms();
|
||||
void JoinRoom(ChatRoom* room);
|
||||
void UnjoinRoom(ChatRoom* room);
|
||||
|
||||
private:
|
||||
friend class ChatAvatarService;
|
||||
|
||||
|
||||
@@ -64,7 +64,6 @@ void ChatRoom::EnterRoom(ChatAvatar* avatar, const std::u16string& password) {
|
||||
}
|
||||
|
||||
avatars_.push_back(avatar);
|
||||
avatar->JoinRoom(this);
|
||||
}
|
||||
|
||||
bool ChatRoom::IsInRoom(ChatAvatar* avatar) const { return IsInRoom(avatar->GetAvatarId()); }
|
||||
@@ -76,8 +75,6 @@ bool ChatRoom::IsInRoom(uint32_t avatarId) const {
|
||||
}
|
||||
|
||||
void ChatRoom::LeaveRoom(ChatAvatar* avatar) {
|
||||
avatar->UnjoinRoom(this);
|
||||
|
||||
auto avatarsIter = std::remove_if(std::begin(avatars_), std::end(avatars_),
|
||||
[avatar](auto roomAvatar) { return roomAvatar->GetAvatarId() == avatar->GetAvatarId(); });
|
||||
|
||||
|
||||
@@ -208,6 +208,18 @@ ChatRoom* ChatRoomService::GetRoom(const std::u16string& roomAddress) {
|
||||
return room;
|
||||
}
|
||||
|
||||
std::vector<ChatRoom*> ChatRoomService::GetJoinedRooms(const ChatAvatar * avatar) {
|
||||
std::vector<ChatRoom*> rooms;
|
||||
|
||||
for (auto& room : rooms_) {
|
||||
if (room->IsInRoom(avatar->GetAvatarId())) {
|
||||
rooms.push_back(room.get());
|
||||
}
|
||||
}
|
||||
|
||||
return rooms;
|
||||
}
|
||||
|
||||
void ChatRoomService::DeleteRoom(ChatRoom* room) {
|
||||
sqlite3_stmt* stmt;
|
||||
char sql[] = "DELETE FROM room WHERE id = @id";
|
||||
|
||||
@@ -37,6 +37,8 @@ public:
|
||||
bool RoomExists(const std::u16string& roomAddress) const;
|
||||
ChatRoom* GetRoom(const std::u16string& roomAddress);
|
||||
|
||||
std::vector<ChatRoom*> GetJoinedRooms(const ChatAvatar* avatar);
|
||||
|
||||
private:
|
||||
friend class ChatRoom;
|
||||
void DeleteRoom(ChatRoom* room);
|
||||
|
||||
@@ -196,7 +196,7 @@ DestroyAvatar::DestroyAvatar(
|
||||
}
|
||||
|
||||
// Remove From All Rooms
|
||||
for (auto room : avatar->GetJoinedRooms()) {
|
||||
for (auto room : roomService_->GetJoinedRooms(avatar)) {
|
||||
auto addresses = room->GetConnectedAddresses();
|
||||
room->LeaveRoom(avatar);
|
||||
|
||||
@@ -287,7 +287,7 @@ FailoverReLoginAvatar::FailoverReLoginAvatar(
|
||||
client->SendFriendLoginUpdates(avatar);
|
||||
}
|
||||
|
||||
for (auto room : avatar->GetJoinedRooms()) {
|
||||
for (auto room : roomService_->GetJoinedRooms(avatar)) {
|
||||
client->SendEnterRoomUpdate(avatar, room);
|
||||
}
|
||||
}
|
||||
@@ -462,7 +462,7 @@ LogoutAvatar::LogoutAvatar(GatewayClient* client, const RequestType& request, Re
|
||||
|
||||
auto avatar = avatarService_->GetAvatar(request.avatarId);
|
||||
|
||||
for (auto room : avatar->GetJoinedRooms()) {
|
||||
for (auto room : roomService_->GetJoinedRooms(avatar)) {
|
||||
auto addresses = room->GetConnectedAddresses();
|
||||
room->LeaveRoom(avatar);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user