From a17b3e157fba5d97f57c7b42f2d3e72de24d4a10 Mon Sep 17 00:00:00 2001 From: Emin Fedar Date: Mon, 21 Feb 2022 22:39:49 +0300 Subject: [PATCH] Double free corruption fix. --- async-sockets/include/tcpserver.hpp | 1 + async-sockets/include/tcpsocket.hpp | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/async-sockets/include/tcpserver.hpp b/async-sockets/include/tcpserver.hpp index 3ed2737..6f93b12 100644 --- a/async-sockets/include/tcpserver.hpp +++ b/async-sockets/include/tcpserver.hpp @@ -77,6 +77,7 @@ class TCPServer : public BaseSocket if (!server->isClosed && newSock >= 0) { TCPSocket *newSocket = new TCPSocket(onError, newSock); + newSocket->deleteAfterClosed = true; newSocket->setAddressStruct(newSocketInfo); server->onNewConnection(newSocket); diff --git a/async-sockets/include/tcpsocket.hpp b/async-sockets/include/tcpsocket.hpp index b008128..586fc00 100644 --- a/async-sockets/include/tcpsocket.hpp +++ b/async-sockets/include/tcpsocket.hpp @@ -91,6 +91,8 @@ class TCPSocket : public BaseSocket void setAddressStruct(sockaddr_in addr) {this->address = addr;} sockaddr_in getAddressStruct() const {return this->address;} + bool deleteAfterClosed = false; + private: static void Receive(TCPSocket *socket) { @@ -111,7 +113,7 @@ class TCPSocket : public BaseSocket if(socket->onSocketClosed) socket->onSocketClosed(errno); - if (socket != nullptr) + if (socket->deleteAfterClosed && socket != nullptr) delete socket; }