From 9b5ca124ab5178e9a22d5a3fa9f0be8ed4474753 Mon Sep 17 00:00:00 2001 From: David Anderson Date: Sun, 31 Mar 2013 13:30:22 -0700 Subject: [PATCH] Don't allocate/delete across static CRTs (no bug, irc r=fyren+psychonic). --- core/CoreConfig.cpp | 7 +++--- core/PlayerManager.cpp | 39 ++++++++++++----------------- core/logic/ExtensionSys.cpp | 11 +++++--- core/logic/ExtensionSys.h | 3 ++- core/logic/PluginSys.cpp | 15 +++++++---- core/logic/PluginSys.h | 3 ++- core/logic/intercom.h | 50 ++++++++++++++++++++++++++++++++++--- 7 files changed, 87 insertions(+), 41 deletions(-) diff --git a/core/CoreConfig.cpp b/core/CoreConfig.cpp index e441b13d..cc4fd8d9 100644 --- a/core/CoreConfig.cpp +++ b/core/CoreConfig.cpp @@ -545,11 +545,10 @@ void SM_ExecuteAllConfigs() engine->ServerCommand("exec sourcemod/sourcemod.cfg\n"); - CVector plugins; - scripts->ListPlugins(&plugins); - for (size_t i = 0; i < plugins.size(); i++) + AutoPluginList plugins(scripts); + for (size_t i = 0; i < plugins->size(); i++) { - SMPlugin *plugin = plugins[i]; + SMPlugin *plugin = plugins->at(i); unsigned int num = plugin->GetConfigCount(); bool can_create = true; for (unsigned int i=0; i extensions; - extsys->ListExtensions(&extensions); - - if (!extensions.size()) + AutoExtensionList extensions(extsys); + if (!extensions->size()) { ClientConsolePrint(player->GetEdict(), "[SM] No extensions found."); return; @@ -797,13 +795,10 @@ void ListExtensionsToClient(CPlayer *player, const CCommand &args) start = atoi(args.Arg(2)); } - SourceHook::CVector::iterator iter; - - for (iter = extensions.begin(); - iter != extensions.end(); - iter++) + size_t i = 0; + for (; i < extensions->size(); i++) { - IExtension *ext = (*iter); + IExtension *ext = extensions->at(i); char error[255]; if (!ext->IsRunning(error, sizeof(error))) @@ -850,16 +845,16 @@ void ListExtensionsToClient(CPlayer *player, const CCommand &args) ClientConsolePrint(player->GetEdict(), "%s", buffer); } - for (; iter != extensions.end(); iter++) + for (; i < extensions->size(); i++) { char error[255]; - if ((*iter)->IsRunning(error, sizeof(error))) + if (extensions->at(i)->IsRunning(error, sizeof(error))) { break; } } - if (iter != extensions.end()) + if (i < extensions->size()) { ClientConsolePrint(player->GetEdict(), "To see more, type \"sm exts %d\"", id); } @@ -872,10 +867,8 @@ void ListPluginsToClient(CPlayer *player, const CCommand &args) edict_t *e = player->GetEdict(); unsigned int start = 0; - CVector plugins; - scripts->ListPlugins(&plugins); - - if (!plugins.size()) + AutoPluginList plugins(scripts); + if (!plugins->size()) { ClientConsolePrint(e, "[SM] No plugins found."); return; @@ -888,10 +881,10 @@ void ListPluginsToClient(CPlayer *player, const CCommand &args) SourceHook::List m_FailList; - CVector::iterator iter = plugins.begin(); - for (; iter != plugins.end(); iter++) + size_t i = 0; + for (; i < plugins->size(); i++) { - SMPlugin *pl = *iter; + SMPlugin *pl = plugins->at(i); if (pl->GetStatus() != Plugin_Running) { @@ -929,16 +922,16 @@ void ListPluginsToClient(CPlayer *player, const CCommand &args) } /* See if we can get more plugins */ - for (; iter != plugins.end(); iter++) + for (; i < plugins->size(); i++) { - if ((*iter)->GetStatus() == Plugin_Running) + if (plugins->at(i)->GetStatus() == Plugin_Running) { break; } } /* Do we actually have more plugins? */ - if (iter != plugins.end()) + if (i < plugins->size()) { ClientConsolePrint(e, "To see more, type \"sm plugins %d\"", id); } diff --git a/core/logic/ExtensionSys.cpp b/core/logic/ExtensionSys.cpp index 1ae60761..32505fdf 100644 --- a/core/logic/ExtensionSys.cpp +++ b/core/logic/ExtensionSys.cpp @@ -1361,12 +1361,17 @@ void CExtensionManager::CallOnCoreMapStart(edict_t *pEdictList, int edictCount, } } -void CExtensionManager::ListExtensions(CVector *list) +const CVector *CExtensionManager::ListExtensions() { + CVector *list = new CVector(); for (List::iterator iter = m_Libs.begin(); iter != m_Libs.end(); iter++) - { list->push_back(*iter); - } + return list; +} + +void CExtensionManager::FreeExtensionList(const CVector *list) +{ + delete const_cast *>(list); } bool CLocalExtension::IsSameFile(const char *file) diff --git a/core/logic/ExtensionSys.h b/core/logic/ExtensionSys.h index 1aabd1f7..832bf3a9 100644 --- a/core/logic/ExtensionSys.h +++ b/core/logic/ExtensionSys.h @@ -174,7 +174,8 @@ public: bool LibraryExists(const char *library); void CallOnCoreMapStart(edict_t *pEdictList, int edictCount, int clientMax); void AddRawDependency(IExtension *ext, IdentityToken_t *other, void *iface); - void ListExtensions(CVector *list); + const CVector *ListExtensions(); + void FreeExtensionList(const CVector *list); public: CExtension *GetExtensionFromIdent(IdentityToken_t *ptr); void Shutdown(); diff --git a/core/logic/PluginSys.cpp b/core/logic/PluginSys.cpp index f1b07fe9..d440d5e1 100644 --- a/core/logic/PluginSys.cpp +++ b/core/logic/PluginSys.cpp @@ -2627,14 +2627,19 @@ void CPluginManager::SyncMaxClients(int max_clients) } } -void CPluginManager::ListPlugins(CVector *list) +const CVector *CPluginManager::ListPlugins() { - List::iterator iter; + CVector *list = new CVector(); - for (iter = m_plugins.begin(); iter != m_plugins.end(); iter++) - { + for (List::iterator iter = m_plugins.begin(); iter != m_plugins.end(); iter++) list->push_back((*iter)); - } + + return list; +} + +void CPluginManager::FreePluginList(const CVector *list) +{ + delete const_cast *>(list); } class OldPluginAPI : public IPluginManager diff --git a/core/logic/PluginSys.h b/core/logic/PluginSys.h index ee2137a2..fecacf99 100644 --- a/core/logic/PluginSys.h +++ b/core/logic/PluginSys.h @@ -325,7 +325,8 @@ public: //IScriptManager SMPlugin *FindPluginByHandle(Handle_t hndl, HandleError *errp) { return static_cast(PluginFromHandle(hndl, errp)); } - void ListPlugins(CVector *plugins); + const CVector *ListPlugins(); + void FreePluginList(const CVector *plugins); public: //SMGlobalClass void OnSourceModAllInitialized(); void OnSourceModShutdown(); diff --git a/core/logic/intercom.h b/core/logic/intercom.h index a157d811..84bfa5d5 100644 --- a/core/logic/intercom.h +++ b/core/logic/intercom.h @@ -1,5 +1,5 @@ /** - * vim: set ts=4 : + * vim: set ts=4 sw=4 tw=99 noet: * ============================================================================= * SourceMod * Copyright (C) 2004-2009 AlliedModders LLC. All rights reserved. @@ -49,7 +49,7 @@ using namespace SourceHook; * Add 1 to the RHS of this expression to bump the intercom file * This is to prevent mismatching core/logic binaries */ -#define SM_LOGIC_MAGIC (0x0F47C0DE - 19) +#define SM_LOGIC_MAGIC (0x0F47C0DE - 20) #if defined SM_LOGIC class IVEngineServer @@ -141,7 +141,8 @@ public: virtual SMPlugin *FindPluginByConsoleArg(const char *text) = 0; virtual SMPlugin *FindPluginByHandle(Handle_t hndl, HandleError *errp) = 0; virtual bool UnloadPlugin(IPlugin *plugin) = 0; - virtual void ListPlugins(CVector *plugins) = 0; + virtual const CVector *ListPlugins() = 0; + virtual void FreePluginList(const CVector *list) = 0; virtual void AddFunctionsToForward(const char *name, IChangeableForward *fwd) = 0; }; @@ -157,7 +158,48 @@ public: virtual IExtension *GetExtensionFromIdent(IdentityToken_t *token) = 0; virtual void BindChildPlugin(IExtension *ext, SMPlugin *plugin) = 0; virtual void AddRawDependency(IExtension *myself, IdentityToken_t *token, void *iface) = 0; - virtual void ListExtensions(CVector *list) = 0; + virtual const CVector *ListExtensions() = 0; + virtual void FreeExtensionList(const CVector *list) = 0; +}; + +class AutoPluginList +{ +public: + AutoPluginList(IScriptManager *scripts) + : scripts_(scripts), list_(scripts->ListPlugins()) + { + } + ~AutoPluginList() + { + scripts_->FreePluginList(list_); + } + const CVector *operator ->() + { + return list_; + } +private: + IScriptManager *scripts_; + const CVector *list_; +}; + +class AutoExtensionList +{ +public: + AutoExtensionList(IExtensionSys *extensions) + : extensions_(extensions), list_(extensions_->ListExtensions()) + { + } + ~AutoExtensionList() + { + extensions_->FreeExtensionList(list_); + } + const CVector *operator ->() + { + return list_; + } +private: + IExtensionSys *extensions_; + const CVector *list_; }; class CCommand;