Remove error outparams from first-pass internal plugin loading commands.

This commit is contained in:
David Anderson 2015-09-17 14:53:00 -07:00
parent 5f19fc036d
commit 9b1678bd18
2 changed files with 28 additions and 47 deletions

View File

@ -133,7 +133,7 @@ Handle_t CPlugin::GetMyHandle()
return m_handle; return m_handle;
} }
CPlugin *CPlugin::CreatePlugin(const char *file, char *error, size_t maxlength) CPlugin *CPlugin::Create(const char *file)
{ {
char fullpath[PLATFORM_MAX_PATH]; char fullpath[PLATFORM_MAX_PATH];
g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "plugins/%s", file); g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "plugins/%s", file);
@ -142,9 +142,7 @@ CPlugin *CPlugin::CreatePlugin(const char *file, char *error, size_t maxlength)
CPlugin *pPlugin = new CPlugin(file); CPlugin *pPlugin = new CPlugin(file);
if (!fp) { if (!fp) {
if (error) pPlugin->SetErrorState(Plugin_BadLoad, "Unable to open file");
ke::SafeSprintf(error, maxlength, "Unable to open file");
pPlugin->m_status = Plugin_BadLoad;
return pPlugin; return pPlugin;
} }
@ -428,7 +426,7 @@ void *CPlugin::GetPluginStructure()
} }
// Only called during plugin construction. // Only called during plugin construction.
bool CPlugin::TryCompile(char *error, size_t maxlength) bool CPlugin::TryCompile()
{ {
char fullpath[PLATFORM_MAX_PATH]; char fullpath[PLATFORM_MAX_PATH];
g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "plugins/%s", m_filename); g_pSM->BuildPath(Path_SM, fullpath, sizeof(fullpath), "plugins/%s", m_filename);
@ -436,16 +434,13 @@ bool CPlugin::TryCompile(char *error, size_t maxlength)
char loadmsg[255]; char loadmsg[255];
m_pRuntime = g_pSourcePawn2->LoadBinaryFromFile(fullpath, loadmsg, sizeof(loadmsg)); m_pRuntime = g_pSourcePawn2->LoadBinaryFromFile(fullpath, loadmsg, sizeof(loadmsg));
if (!m_pRuntime) { if (!m_pRuntime) {
ke::SafeSprintf(error, maxlength, "Unable to load plugin (%s)", loadmsg); SetErrorState(Plugin_BadLoad, "Unable to load plugin (%s)", loadmsg);
m_status = Plugin_BadLoad;
return false; return false;
} }
// ReadInfo() sets its own error state. // ReadInfo() sets its own error state.
if (!ReadInfo()) { if (!ReadInfo())
ke::SafeSprintf(error, maxlength, "%s", m_errormsg);
return false; return false;
}
m_status = Plugin_Created; m_status = Plugin_Created;
return true; return true;
@ -874,7 +869,7 @@ void CPluginManager::LoadPluginsFromDir(const char *basedir, const char *localpa
libsys->CloseDirectory(dir); libsys->CloseDirectory(dir);
} }
LoadRes CPluginManager::LoadPlugin(CPlugin **aResult, const char *path, bool debug, PluginType type, char error[], size_t maxlength) LoadRes CPluginManager::LoadPlugin(CPlugin **aResult, const char *path, bool debug, PluginType type)
{ {
if (m_LoadingLocked) if (m_LoadingLocked)
return LoadRes_NeverLoad; return LoadRes_NeverLoad;
@ -900,7 +895,7 @@ LoadRes CPluginManager::LoadPlugin(CPlugin **aResult, const char *path, bool deb
} }
} }
CPlugin *plugin = CompileAndPrep(path, error, maxlength); CPlugin *plugin = CompileAndPrep(path);
// Assign our outparam so we can return early. It must be set. // Assign our outparam so we can return early. It must be set.
*aResult = plugin; *aResult = plugin;
@ -921,8 +916,9 @@ IPlugin *CPluginManager::LoadPlugin(const char *path, bool debug, PluginType typ
LoadRes res; LoadRes res;
*wasloaded = false; *wasloaded = false;
if ((res=LoadPlugin(&pl, path, true, PluginType_MapUpdated, error, maxlength)) == LoadRes_Failure) if ((res=LoadPlugin(&pl, path, true, PluginType_MapUpdated)) == LoadRes_Failure)
{ {
ke::SafeStrcpy(error, maxlength, pl->m_errormsg);
delete pl; delete pl;
return NULL; return NULL;
} }
@ -933,19 +929,11 @@ IPlugin *CPluginManager::LoadPlugin(const char *path, bool debug, PluginType typ
return pl; return pl;
} }
if (res == LoadRes_NeverLoad) if (res == LoadRes_NeverLoad) {
{ if (m_LoadingLocked)
if (error) ke::SafeSprintf(error, maxlength, "There is a global plugin loading lock in effect");
{ else
if (m_LoadingLocked) ke::SafeSprintf(error, maxlength, "This plugin is blocked from loading (see plugin_settings.cfg)");
{
ke::SafeSprintf(error, maxlength, "There is a global plugin loading lock in effect");
}
else
{
ke::SafeSprintf(error, maxlength, "This plugin is blocked from loading (see plugin_settings.cfg)");
}
}
return NULL; return NULL;
} }
@ -969,15 +957,9 @@ void CPluginManager::LoadAutoPlugin(const char *plugin)
{ {
CPlugin *pl = NULL; CPlugin *pl = NULL;
LoadRes res; LoadRes res;
char error[255] = "Unknown error"; if ((res=LoadPlugin(&pl, plugin, false, PluginType_MapUpdated)) == LoadRes_Failure)
if ((res=LoadPlugin(&pl, plugin, false, PluginType_MapUpdated, error, sizeof(error))) == LoadRes_Failure)
{ {
g_Logger.LogError("[SM] Failed to load plugin \"%s\": %s.", plugin, error); g_Logger.LogError("[SM] Failed to load plugin \"%s\": %s.", plugin, pl->m_errormsg);
pl->SetErrorState(
pl->GetStatus() <= Plugin_Created ? Plugin_BadLoad : pl->GetStatus(),
"%s",
error);
} }
if (res == LoadRes_Successful || res == LoadRes_Failure) if (res == LoadRes_Successful || res == LoadRes_Failure)
@ -1179,19 +1161,19 @@ bool CPluginManager::RequireExtensions(CPlugin *pPlugin, char *error, size_t max
return pPlugin->ForEachExtVar(ke::Move(callback)); return pPlugin->ForEachExtVar(ke::Move(callback));
} }
CPlugin *CPluginManager::CompileAndPrep(const char *path, char *error, size_t maxlength) CPlugin *CPluginManager::CompileAndPrep(const char *path)
{ {
CPlugin *plugin = CPlugin::CreatePlugin(path, error, maxlength); CPlugin *plugin = CPlugin::Create(path);
if (plugin->GetStatus() != Plugin_Uncompiled) { if (plugin->GetStatus() != Plugin_Uncompiled) {
assert(plugin->GetStatus() == Plugin_BadLoad); assert(plugin->GetStatus() == Plugin_BadLoad);
return plugin; return plugin;
} }
if (!plugin->TryCompile(error, maxlength)) if (!plugin->TryCompile())
return plugin; return plugin;
assert(plugin->GetStatus() == Plugin_Created); assert(plugin->GetStatus() == Plugin_Created);
if (!MalwareCheckPass(plugin, error, maxlength)) if (!MalwareCheckPass(plugin))
return plugin; return plugin;
assert(plugin->GetStatus() == Plugin_Created); assert(plugin->GetStatus() == Plugin_Created);
@ -1201,7 +1183,7 @@ CPlugin *CPluginManager::CompileAndPrep(const char *path, char *error, size_t ma
} }
bool CPluginManager::MalwareCheckPass(CPlugin *pPlugin, char *error, size_t maxlength) bool CPluginManager::MalwareCheckPass(CPlugin *pPlugin)
{ {
unsigned char *pCodeHash = pPlugin->GetRuntime()->GetCodeHash(); unsigned char *pCodeHash = pPlugin->GetRuntime()->GetCodeHash();
@ -1216,11 +1198,10 @@ bool CPluginManager::MalwareCheckPass(CPlugin *pPlugin, char *error, size_t maxl
if (m_bBlockBadPlugins) { if (m_bBlockBadPlugins) {
if (bulletinUrl[0] != '\0') { if (bulletinUrl[0] != '\0') {
ke::SafeSprintf(error, maxlength, "Known malware detected and blocked. See %s for more info", bulletinUrl); pPlugin->SetErrorState(Plugin_BadLoad, "Known malware detected and blocked. See %s for more info", bulletinUrl);
} else { } else {
ke::SafeSprintf(error, maxlength, "Possible malware or illegal plugin detected and blocked"); pPlugin->SetErrorState(Plugin_BadLoad, "Possible malware or illegal plugin detected and blocked");
} }
pPlugin->m_status = Plugin_BadLoad;
return false; return false;
} }

View File

@ -172,7 +172,7 @@ public:
* If an error buffer is not specified, the error will be copied to an internal buffer and * If an error buffer is not specified, the error will be copied to an internal buffer and
* a valid (but error-stated) CPlugin will be returned. * a valid (but error-stated) CPlugin will be returned.
*/ */
static CPlugin *CreatePlugin(const char *file, char *error, size_t maxlength); static CPlugin *Create(const char *file);
static inline bool matches(const char *file, const CPlugin *plugin) static inline bool matches(const char *file, const CPlugin *plugin)
{ {
@ -259,7 +259,7 @@ public:
protected: protected:
bool ReadInfo(); bool ReadInfo();
void DependencyDropped(CPlugin *pOwner); void DependencyDropped(CPlugin *pOwner);
bool TryCompile(char *error, size_t maxlength); bool TryCompile();
private: private:
time_t GetFileTimeStamp(); time_t GetFileTimeStamp();
@ -452,7 +452,7 @@ public:
void ListPluginsToClient(CPlayer *player, const CCommand &args); void ListPluginsToClient(CPlayer *player, const CCommand &args);
private: private:
LoadRes LoadPlugin(CPlugin **pPlugin, const char *path, bool debug, PluginType type, char error[], size_t maxlength); LoadRes LoadPlugin(CPlugin **pPlugin, const char *path, bool debug, PluginType type);
void LoadAutoPlugin(const char *plugin); void LoadAutoPlugin(const char *plugin);
@ -469,8 +469,8 @@ private:
/** /**
* First pass for loading a plugin, and its helpers. * First pass for loading a plugin, and its helpers.
*/ */
CPlugin *CompileAndPrep(const char *path, char *error, size_t maxlength); CPlugin *CompileAndPrep(const char *path);
bool MalwareCheckPass(CPlugin *pPlugin, char *error, size_t maxlength); bool MalwareCheckPass(CPlugin *pPlugin);
/** /**
* Runs the second loading pass on a plugin. * Runs the second loading pass on a plugin.