From 8d5b270812f5cf72c60672942306ddb46b3f8573 Mon Sep 17 00:00:00 2001 From: Gunnar Beutner Date: Mon, 16 Feb 2015 11:39:34 +0100 Subject: [PATCH] Fix another crash in SocketEvents::OnEvent fixes #8446 --- lib/base/socketevents.cpp | 17 ++++++++++------- lib/base/socketevents.hpp | 5 +++-- lib/base/tlsstream.cpp | 2 +- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/lib/base/socketevents.cpp b/lib/base/socketevents.cpp index 64731855e..81f463120 100644 --- a/lib/base/socketevents.cpp +++ b/lib/base/socketevents.cpp @@ -34,6 +34,7 @@ struct SocketEventDescriptor { int Events; SocketEvents *EventInterface; + Object *LifesupportObject; SocketEventDescriptor(void) : Events(0) @@ -125,7 +126,8 @@ void SocketEvents::ThreadProc(void) desc = it->second; /* We must hold a ref-counted reference to the event object to keep it alive. */ - ltref = dynamic_cast(desc.EventInterface); + ltref = desc.LifesupportObject; + VERIFY(ltref); } desc.EventInterface->OnEvent(pfds[i].revents); @@ -143,31 +145,32 @@ void SocketEvents::WakeUpThread(void) /** * Constructor for the SocketEvents class. */ -SocketEvents::SocketEvents(const Socket::Ptr& socket) +SocketEvents::SocketEvents(const Socket::Ptr& socket, Object *lifesupportObject) : m_FD(socket->GetFD()) { boost::call_once(l_SocketIOOnceFlag, &SocketEvents::InitializeThread); - Register(); + Register(lifesupportObject); } SocketEvents::~SocketEvents(void) { - ASSERT(m_FD == INVALID_SOCKET); + VERIFY(m_FD == INVALID_SOCKET); } -void SocketEvents::Register(void) +void SocketEvents::Register(Object *lifesupportObject) { - ASSERT(m_FD != INVALID_SOCKET); + VERIFY(m_FD != INVALID_SOCKET); SocketEventDescriptor desc; desc.Events = 0; desc.EventInterface = this; + desc.LifesupportObject = lifesupportObject; { boost::mutex::scoped_lock lock(l_SocketIOMutex); - ASSERT(l_SocketIOSockets.find(m_FD) == l_SocketIOSockets.end()); + VERIFY(l_SocketIOSockets.find(m_FD) == l_SocketIOSockets.end()); l_SocketIOSockets[m_FD] = desc; } diff --git a/lib/base/socketevents.hpp b/lib/base/socketevents.hpp index 0d3a34242..645de789b 100644 --- a/lib/base/socketevents.hpp +++ b/lib/base/socketevents.hpp @@ -38,13 +38,12 @@ public: virtual void OnEvent(int revents); - void Register(void); void Unregister(void); void ChangeEvents(int events); protected: - SocketEvents(const Socket::Ptr& socket); + SocketEvents(const Socket::Ptr& socket, Object *lifesupportObject); private: SOCKET m_FD; @@ -55,6 +54,8 @@ private: static void WakeUpThread(void); int GetPollEvents(void) const; + + void Register(Object *lifesupportObject); }; } diff --git a/lib/base/tlsstream.cpp b/lib/base/tlsstream.cpp index 9057d902e..c1773a73f 100644 --- a/lib/base/tlsstream.cpp +++ b/lib/base/tlsstream.cpp @@ -40,7 +40,7 @@ bool I2_EXPORT TlsStream::m_SSLIndexInitialized = false; * @param sslContext The SSL context for the client. */ TlsStream::TlsStream(const Socket::Ptr& socket, ConnectionRole role, const boost::shared_ptr& sslContext) - : SocketEvents(socket), m_Eof(false), m_HandshakeOK(false), m_VerifyOK(true), m_CloseOK(false), m_ErrorCode(0), + : SocketEvents(socket, this), m_Eof(false), m_HandshakeOK(false), m_VerifyOK(true), m_CloseOK(false), m_ErrorCode(0), m_ErrorOccurred(false), m_Socket(socket), m_Role(role), m_SendQ(new FIFO()), m_RecvQ(new FIFO()), m_CurrentAction(TlsActionNone), m_Retry(false) { -- 2.40.0