From e27053fae607ff8c923f180ee0ee06e8d780ea08 Mon Sep 17 00:00:00 2001 From: Wiebe Cazemier Date: Sun, 10 Jul 2022 16:03:27 +0200 Subject: [PATCH] Prevent accidental deletion of wrong client in removeQueuedClients --- threaddata.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/threaddata.cpp b/threaddata.cpp index 75dadde..55a3aff 100644 --- a/threaddata.cpp +++ b/threaddata.cpp @@ -245,8 +245,8 @@ void ThreadData::sendAllDisconnects() void ThreadData::removeQueuedClients() { - std::vector fds; - fds.reserve(1024); // 1024 is arbitrary... + // Using shared pointers to have a claiming reference in case we lose the clients between the two locks. + std::vector> clients; { std::lock_guard lck2(clientsToRemoveMutex); @@ -256,8 +256,7 @@ void ThreadData::removeQueuedClients() std::shared_ptr client = c.lock(); if (client) { - int fd = client->getFd(); - fds.push_back(fd); + clients.push_back(client); } } @@ -266,8 +265,9 @@ void ThreadData::removeQueuedClients() { std::lock_guard lck(clients_by_fd_mutex); - for(int fd : fds) + for(const std::shared_ptr &client : clients) { + int fd = client->getFd(); clients_by_fd.erase(fd); } } -- libgit2 0.21.4