From 4303c3c2007b7b76f8b7599a3cc337d7d3aab019 Mon Sep 17 00:00:00 2001 From: psychon Date: Thu, 11 Mar 2010 17:29:36 +0000 Subject: [PATCH] Fix a crash with WebMods and deleting User If a user who is currently logged in to WebMods is deleted, his CWebSession still kept a stale CUser pointer around. The next time he loaded a web page, Bad Things(tm) happened. This is fixed by deleting all of a user's sessions when that user is deleted. Thanks to DarthGandalf for spotting the bug and writing the patch. git-svn-id: https://znc.svn.sourceforge.net/svnroot/znc/trunk@1827 726aef4b-f618-498e-8847-2d620e286838 --- Utils.h | 2 +- WebModules.cpp | 13 ++++++++++++- WebModules.h | 11 ++++++++++- znc.cpp | 1 + 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/Utils.h b/Utils.h index 440e8d5d..c6119d4f 100644 --- a/Utils.h +++ b/Utils.h @@ -358,7 +358,7 @@ public: // Getters unsigned int GetTTL() { return m_uTTL; } // !Getters -private: +protected: typedef pair value; typedef typename map::iterator iterator; map m_mItems; //!< Map of cached items. The value portion of the map is for the expire time diff --git a/WebModules.cpp b/WebModules.cpp index ebc238f6..fd6cc026 100644 --- a/WebModules.cpp +++ b/WebModules.cpp @@ -12,7 +12,7 @@ #include // Sessions are valid for a day, (24h, ...) -TCacheMap > CWebSock::m_mspSessions(24 * 60 * 60 * 1000); +CWebSessionMap CWebSock::m_mspSessions(24 * 60 * 60 * 1000); CZNCTagHandler::CZNCTagHandler(CWebSock& WebSock) : CTemplateTagHandler(), m_WebSock(WebSock) { } @@ -66,6 +66,17 @@ size_t CWebSession::AddSuccess(const CString& sMessage) { return m_vsSuccessMsgs.size(); } +void CWebSessionMap::FinishUserSessions(const CUser& User) { + iterator it = m_mItems.begin(); + + while (it != m_mItems.end()) { + if (it->second.second->GetUser() == &User) { + m_mItems.erase(it++); + } else { + ++it; + } + } +} void CWebAuth::AcceptedLogin(CUser& User) { if (m_pWebSock) { diff --git a/WebModules.h b/WebModules.h index 3a3044e4..cab73253 100644 --- a/WebModules.h +++ b/WebModules.h @@ -104,6 +104,11 @@ protected: CWebSock* m_pWebSock; }; +class CWebSessionMap : public TCacheMap > { + public: + CWebSessionMap(unsigned int uTTL = 5000) : TCacheMap >(uTTL) {} + void FinishUserSessions(const CUser& User); +}; class CWebSock : public CHTTPSock { public: @@ -148,6 +153,10 @@ public: CString GetCookie(const CString& sKey) const; bool SetCookie(const CString& sKey, const CString& sValue); + static void FinishUserSessions(const CUser& User) { + m_mspSessions.FinishUserSessions(User); + } + private: bool m_bPathsSet; CTemplate m_Template; @@ -158,7 +167,7 @@ private: CString m_sPage; // Gets filled by ResolveModule() CSmartPtr m_spSession; - static TCacheMap > m_mspSessions; + static CWebSessionMap m_mspSessions; }; #endif // !_WEBMODULES_H diff --git a/znc.cpp b/znc.cpp index 4342dea9..384db20e 100644 --- a/znc.cpp +++ b/znc.cpp @@ -208,6 +208,7 @@ bool CZNC::HandleUserDeletion() pUser->DelClients(); pUser->DelModules(); + CWebSock::FinishUserSessions(*pUser); AddBytesRead(pUser->BytesRead()); AddBytesWritten(pUser->BytesWritten()); delete pUser;