Migrate extensions to common AMTL string funcs (#785)

This commit is contained in:
Michael Flaherty 2018-03-25 13:00:53 -07:00 committed by Kyle Sanderson
parent 6c7e29cbdb
commit 5611ec54a2
19 changed files with 53 additions and 44 deletions

View File

@ -2050,18 +2050,18 @@ void CPlayer::UpdateAuthIds()
}
char szAuthBuffer[64];
snprintf(szAuthBuffer, sizeof(szAuthBuffer), "STEAM_%u:%u:%u", steam2universe, m_SteamId.GetAccountID() & 1, m_SteamId.GetAccountID() >> 1);
ke::SafeSprintf(szAuthBuffer, sizeof(szAuthBuffer), "STEAM_%u:%u:%u", steam2universe, m_SteamId.GetAccountID() & 1, m_SteamId.GetAccountID() >> 1);
m_Steam2Id = szAuthBuffer;
// TODO: make sure all hl2sdks' steamclientpublic.h have k_unSteamUserDesktopInstance.
if (m_SteamId.GetUnAccountInstance() == 1 /* k_unSteamUserDesktopInstance */)
{
snprintf(szAuthBuffer, sizeof(szAuthBuffer), "[U:%u:%u]", m_SteamId.GetEUniverse(), m_SteamId.GetAccountID());
ke::SafeSprintf(szAuthBuffer, sizeof(szAuthBuffer), "[U:%u:%u]", m_SteamId.GetEUniverse(), m_SteamId.GetAccountID());
}
else
{
snprintf(szAuthBuffer, sizeof(szAuthBuffer), "[U:%u:%u:%u]", m_SteamId.GetEUniverse(), m_SteamId.GetAccountID(), m_SteamId.GetUnAccountInstance());
ke::SafeSprintf(szAuthBuffer, sizeof(szAuthBuffer), "[U:%u:%u:%u]", m_SteamId.GetEUniverse(), m_SteamId.GetAccountID(), m_SteamId.GetUnAccountInstance());
}
m_Steam3Id = szAuthBuffer;

View File

@ -1090,14 +1090,14 @@ bool AdminCache::GetUnifiedSteamIdentity(const char *ident, char *out, size_t ma
else if (len >= 11 && !strncmp(ident, "STEAM_", 6) && ident[8] != '_')
{
// non-bot/lan Steam2 Id, strip off the STEAM_* part
snprintf(out, maxlen, "%s", &ident[8]);
ke::SafeStrcpy(out, maxlen, &ident[8]);
return true;
}
else if (len >= 7 && !strncmp(ident, "[U:", 3) && ident[len-1] == ']')
{
// Steam3 Id, replicate the Steam2 Post-"STEAM_" part
uint32_t accountId = strtoul(&ident[5], nullptr, 10);
snprintf(out, maxlen, "%u:%u", accountId & 1, accountId >> 1);
ke::SafeSprintf(out, maxlen, "%u:%u", accountId & 1, accountId >> 1);
return true;
}
else
@ -1124,7 +1124,7 @@ bool AdminCache::GetUnifiedSteamIdentity(const char *ident, char *out, size_t ma
&& accountType == k_EAccountTypeIndividual && accountInstance <= k_unSteamUserWebInstance
)
{
snprintf(out, maxlen, "%u:%u", accountId & 1, accountId >> 1);
ke::SafeSprintf(out, maxlen, "%u:%u", accountId & 1, accountId >> 1);
return true;
}
}

View File

@ -176,7 +176,7 @@ bool CLocalExtension::Load(char *error, size_t maxlength)
{
m_pLib->CloseLibrary();
m_pLib = NULL;
snprintf(error, maxlength, "Unable to find extension entry point");
ke::SafeStrcpy(error, maxlength, "Unable to find extension entry point");
return false;
}
@ -246,7 +246,7 @@ void CLocalExtension::Unload()
bool CRemoteExtension::Reload(char *error, size_t maxlength)
{
snprintf(error, maxlength, "Remote extensions do not support reloading");
ke::SafeStrcpy(error, maxlength, "Remote extensions do not support reloading");
return false;
}
@ -280,13 +280,13 @@ bool CExtension::PerformAPICheck(char *error, size_t maxlength)
{
if (!m_pAPI)
{
snprintf(error, maxlength, "No IExtensionInterface instance provided");
ke::SafeStrcpy(error, maxlength, "No IExtensionInterface instance provided");
return false;
}
if (m_pAPI->GetExtensionVersion() > SMINTERFACE_EXTENSIONAPI_VERSION)
{
snprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
ke::SafeSprintf(error, maxlength, "Extension version is too new to load (%d, max is %d)", m_pAPI->GetExtensionVersion(), SMINTERFACE_EXTENSIONAPI_VERSION);
return false;
}
@ -493,7 +493,7 @@ bool CExtension::IsRunning(char *error, size_t maxlength)
{
if (error)
{
snprintf(error, maxlength, "%s", m_Error.c_str());
ke::SafeStrcpy(error, maxlength, m_Error.c_str());
}
return false;
}
@ -1136,7 +1136,7 @@ void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const ICommand
if (pExt->unload_code == (unsigned)atoi(unload))
{
char filename[PLATFORM_MAX_PATH];
snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
ke::SafeStrcpy(filename, PLATFORM_MAX_PATH, pExt->GetFilename());
UnloadExtension(pExt);
rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
}
@ -1151,7 +1151,7 @@ void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const ICommand
|| (!pExt->m_ChildDeps.size() && !pExt->m_Dependents.size()))
{
char filename[PLATFORM_MAX_PATH];
snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
ke::SafeStrcpy(filename, PLATFORM_MAX_PATH, pExt->GetFilename());
UnloadExtension(pExt);
rootmenu->ConsolePrint("[SM] Extension %s is now unloaded.", filename);
return;
@ -1252,7 +1252,7 @@ void CExtensionManager::OnRootConsoleCommand(const char *cmdname, const ICommand
char filename[PLATFORM_MAX_PATH];
char error[255];
snprintf(filename, PLATFORM_MAX_PATH, "%s", pExt->GetFilename());
ke::SafeStrcpy(filename, PLATFORM_MAX_PATH, pExt->GetFilename());
if (pExt->Reload(error, sizeof(error)))
{

View File

@ -66,7 +66,7 @@ CDirectory::CDirectory(const char *path)
{
#if defined PLATFORM_WINDOWS
char newpath[PLATFORM_MAX_PATH];
snprintf(newpath, sizeof(newpath), "%s\\*.*", path);
ke::SafeSprintf(newpath, sizeof(newpath), "%s\\*.*", path);
m_dir = FindFirstFileA(newpath, &m_fd);
if (!IsValid())
{
@ -78,7 +78,7 @@ CDirectory::CDirectory(const char *path)
{
/* :TODO: we need to read past "." and ".."! */
ep = readdir(m_dir);
snprintf(m_origpath, PLATFORM_MAX_PATH, "%s", path);
ke::SafeStrcpy(m_origpath, PLATFORM_MAX_PATH, path);
} else {
ep = NULL;
}
@ -125,7 +125,7 @@ bool CDirectory::IsEntryDirectory()
return ((m_fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
#elif defined PLATFORM_POSIX
char temppath[PLATFORM_MAX_PATH];
int ret = snprintf(temppath, sizeof(temppath), "%s/%s", m_origpath, GetEntryName());
int ret = ke::SafeSprintf(temppath, sizeof(temppath), "%s/%s", m_origpath, GetEntryName());
if (static_cast<size_t>(ret) >= sizeof(temppath))
return false;
return ke::file::IsDirectory(temppath);
@ -138,7 +138,7 @@ bool CDirectory::IsEntryFile()
return !(m_fd.dwFileAttributes & (FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_DEVICE));
#elif defined PLATFORM_POSIX
char temppath[PLATFORM_MAX_PATH];
int ret = snprintf(temppath, sizeof(temppath), "%s/%s", m_origpath, GetEntryName());
int ret = ke::SafeSprintf(temppath, sizeof(temppath), "%s/%s", m_origpath, GetEntryName());
if (static_cast<size_t>(ret) >= sizeof(temppath))
return false;
return ke::file::IsFile(temppath);

View File

@ -166,7 +166,7 @@ void RootConsoleMenu::DrawGenericOption(const char *cmd, const char *text)
{
buffer[len++] = ' ';
}
len += snprintf(&buffer[len], sizeof(buffer) - len, " - %s", text);
len += ke::SafeSprintf(&buffer[len], sizeof(buffer) - len, " - %s", text);
ConsolePrint("%s", buffer);
}
}

View File

@ -321,7 +321,7 @@ static cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params)
{
size_t len = strlen(path);
char wildcardedPath[PLATFORM_MAX_PATH];
snprintf(wildcardedPath, sizeof(wildcardedPath), "%s%s*", path, (path[len-1] != '/' && path[len-1] != '\\') ? "/" : "");
ke::SafeSprintf(wildcardedPath, sizeof(wildcardedPath), "%s%s*", path, (path[len-1] != '/' && path[len-1] != '\\') ? "/" : "");
char *pathID;
if ((err=pContext->LocalToStringNULL(params[3], &pathID)) != SP_ERROR_NONE)

View File

@ -397,7 +397,7 @@ static cell_t SteamIdToLocal(IPluginContext *pCtx, int index, AuthIdType authTyp
}
char szAuth[64];
snprintf(szAuth, sizeof(szAuth), "%" PRIu64, steamId);
ke::SafeSprintf(szAuth, sizeof(szAuth), "%" PRIu64, steamId);
pCtx->StringToLocal(local_addr, bytes, szAuth);
}

View File

@ -110,7 +110,7 @@ bool SourceMod_Core::Load(PluginId id, ISmmAPI *ismm, char *error, size_t maxlen
{
if (error)
{
snprintf(error, maxlen, "Unable to find interface %s", MMIFACE_PLMANAGER);
ke::SafeSprintf(error, maxlen, "Unable to find interface %s", MMIFACE_PLMANAGER);
}
return false;
}

View File

@ -66,7 +66,7 @@ bool ClientPrefs::SDK_OnLoad(char *error, size_t maxlength, bool late)
if (DBInfo == NULL)
{
snprintf(error, maxlength, "Could not find any suitable database configs");
ke::SafeStrcpy(error, maxlength, "Could not find any suitable database configs");
return false;
}
@ -81,7 +81,7 @@ bool ClientPrefs::SDK_OnLoad(char *error, size_t maxlength, bool late)
if (Driver == NULL)
{
snprintf(error, maxlength, "Could not load DB Driver \"%s\"", DBInfo->driver);
ke::SafeSprintf(error, maxlength, "Could not load DB Driver \"%s\"", DBInfo->driver);
return false;
}

View File

@ -63,7 +63,7 @@ bool CStrike::SDK_OnLoad(char *error, size_t maxlength, bool late)
#if SOURCE_ENGINE != SE_CSGO
if (strcmp(g_pSM->GetGameFolderName(), "cstrike") != 0)
{
snprintf(error, maxlength, "Cannot Load Cstrike Extension on mods other than CS:S and CS:GO");
ke::SafeStrcpy(error, maxlength, "Cannot Load Cstrike Extension on mods other than CS:S and CS:GO");
return false;
}
#endif
@ -76,7 +76,7 @@ bool CStrike::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
if (error)
{
snprintf(error, maxlength, "Could not read sm-cstrike.games: %s", conf_error);
ke::SafeSprintf(error, maxlength, "Could not read sm-cstrike.games: %s", conf_error);
}
return false;
}

View File

@ -357,7 +357,7 @@ ItemDefHashValue *GetHashValueFromWeapon(const char *szWeapon)
{
char tempWeapon[MAX_WEAPON_NAME_LENGTH];
Q_strncpy(tempWeapon, szWeapon, sizeof(tempWeapon));
ke::SafeStrcpy(tempWeapon, sizeof(tempWeapon), szWeapon);
Q_strlower(tempWeapon);
if (strstr(tempWeapon, "weapon_") == NULL && strstr(tempWeapon, "item_") == NULL)
@ -367,7 +367,7 @@ ItemDefHashValue *GetHashValueFromWeapon(const char *szWeapon)
for (unsigned int i = 0; i < SM_ARRAYSIZE(szClassPrefixs); i++)
{
char classname[MAX_WEAPON_NAME_LENGTH];
Q_snprintf(classname, sizeof(classname), "%s%s", szClassPrefixs[i], tempWeapon);
ke::SafeSprintf(classname, sizeof(classname), "%s%s", szClassPrefixs[i], tempWeapon);
ClassnameMap::Result res = g_mapClassToDefIdx.find(classname);

View File

@ -32,6 +32,7 @@
#include <sourcemod_version.h>
#include "extension.h"
#include "GeoIP.h"
#include "am-string.h"
/**
* @file extension.cpp
@ -51,7 +52,7 @@ bool GeoIP_Extension::SDK_OnLoad(char *error, size_t maxlength, bool late)
if (!gi)
{
snprintf(error, maxlength, "Could not load configs/geoip/GeoIP.dat");
ke::SafeStrcpy(error, maxlength, "Could not load configs/geoip/GeoIP.dat");
return false;
}

View File

@ -32,6 +32,7 @@
#include "MyDriver.h"
#include "MyDatabase.h"
#include "smsdk_ext.h"
#include "am-string.h"
MyDriver g_MyDriver;
@ -133,7 +134,7 @@ MYSQL *Connect(const DatabaseInfo *info, char *error, size_t maxlength)
M_CLIENT_MULTI_RESULTS))
{
/* :TODO: expose UTIL_Format from smutil! */
snprintf(error, maxlength, "[%d]: %s", mysql_errno(mysql), mysql_error(mysql));
ke::SafeSprintf(error, maxlength, "[%d]: %s", mysql_errno(mysql), mysql_error(mysql));
mysql_close(mysql);
return NULL;
}

View File

@ -46,6 +46,8 @@
#include "clientnatives.h"
#include "teamnatives.h"
#include "filesystem.h"
#include "am-string.h"
/**
* @file extension.cpp
* @brief Implements SDK Tools extension code.
@ -128,7 +130,7 @@ bool SDKTools::SDK_OnLoad(char *error, size_t maxlength, bool late)
g_CallHandle = handlesys->CreateType("ValveCall", this, 0, NULL, NULL, myself->GetIdentity(), &err);
if (g_CallHandle == 0)
{
snprintf(error, maxlength, "Could not create call handle type (err: %d)", err);
ke::SafeSprintf(error, maxlength, "Could not create call handle type (err: %d)", err);
return false;
}
@ -142,7 +144,7 @@ bool SDKTools::SDK_OnLoad(char *error, size_t maxlength, bool late)
{
handlesys->RemoveType(g_CallHandle, myself->GetIdentity());
g_CallHandle = 0;
snprintf(error, maxlength, "Could not create traceray handle type (err: %d)", err);
ke::SafeSprintf(error, maxlength, "Could not create traceray handle type (err: %d)", err);
return false;
}
@ -385,7 +387,7 @@ bool SDKTools::LevelInit(char const *pMapName, char const *pMapEntities, char co
while (n <= count)
{
snprintf(key, sizeof(key), "SlapSound%d", n);
ke::SafeSprintf(key, sizeof(key), "SlapSound%d", n);
if ((name=g_pGameConf->GetKeyValue(key)))
{
engsound->PrecacheSound(name, true);
@ -436,7 +438,7 @@ bool SDKTools::ProcessCommandTarget(cmd_target_info_t *info)
info->num_targets = 1;
info->reason = COMMAND_TARGET_VALID;
info->target_name_style = COMMAND_TARGETNAME_RAW;
snprintf(info->target_name, info->target_name_maxlength, "%s", pTarget->GetName());
ke::SafeStrcpy(info->target_name, info->target_name_maxlength, pTarget->GetName());
return true;
}
else if (strcmp(info->pattern, "@spec") == 0)
@ -462,7 +464,7 @@ bool SDKTools::ProcessCommandTarget(cmd_target_info_t *info)
}
info->reason = info->num_targets > 0 ? COMMAND_TARGET_VALID : COMMAND_TARGET_EMPTY_FILTER;
info->target_name_style = COMMAND_TARGETNAME_ML;
snprintf(info->target_name, info->target_name_maxlength, "all spectators");
ke::SafeStrcpy(info->target_name, info->target_name_maxlength, "all spectators");
return true;
}

View File

@ -31,6 +31,7 @@
#include "extension.h"
#include "output.h"
#include "am-string.h"
ISourcePawnEngine *spengine = NULL;
EntityOutputManager g_OutputManager;
@ -120,7 +121,7 @@ bool EntityOutputManager::FireEventDetour(void *pOutput, CBaseEntity *pActivator
}
char sOutput[20];
Q_snprintf(sOutput, sizeof(sOutput), "%p", pOutput);
ke::SafeSprintf(sOutput, sizeof(sOutput), "%p", pOutput);
// attempt to directly lookup a hook using the pOutput pointer
OutputNameStruct *pOutputName = NULL;

View File

@ -40,6 +40,7 @@
#include <inetchannel.h>
#include <iclient.h>
#include "iserver.h"
#include "am-string.h"
SourceHook::List<ValveCall *> g_RegCalls;
SourceHook::List<ICallWrapper *> g_CallWraps;
@ -623,7 +624,7 @@ static cell_t SlapPlayer(IPluginContext *pContext, const cell_t *params)
int maxClients = playerhelpers->GetMaxClients();
int r = (rand() % s_sound_count) + 1;
snprintf(name, sizeof(name), "SlapSound%d", r);
ke::SafeSprintf(name, sizeof(name), "SlapSound%d", r);
if ((sound_name = g_pGameConf->GetKeyValue(name)) != NULL)
{

View File

@ -34,6 +34,7 @@
#include <IShareSys.h>
#include <ILibrarySys.h>
#include "am-string.h"
/**
* @file IExtensionSys.h
@ -443,7 +444,7 @@ namespace SourceMod
{ \
if (errbuf) \
{ \
size_t len = snprintf(errbuf, \
size_t len = ke::SafeSprintf(errbuf, \
errsize, \
"Could not find interface: %s (version: %d)", \
SM_IFACEPAIR(prefix)); \

View File

@ -32,6 +32,7 @@
#include <stdio.h>
#include <stdlib.h>
#include "smsdk_ext.h"
#include "am-string.h"
/**
* @file smsdk_ext.cpp
@ -128,7 +129,7 @@ bool SDKExtension::OnExtensionLoad(IExtension *me, IShareSys *sys, char *error,
{
if (error)
{
snprintf(error, maxlength, "Metamod attach failed");
ke::SafeStrcpy(error, maxlength, "Metamod attach failed");
}
return false;
}
@ -376,7 +377,7 @@ bool SDKExtension::Unload(char *error, size_t maxlen)
{
if (error)
{
snprintf(error, maxlen, "This extension must be unloaded by SourceMod.");
ke::SafeStrcpy(error, maxlen, "This extension must be unloaded by SourceMod.");
}
return false;
}
@ -390,7 +391,7 @@ bool SDKExtension::Pause(char *error, size_t maxlen)
{
if (error)
{
snprintf(error, maxlen, "This extension must be paused by SourceMod.");
ke::SafeStrcpy(error, maxlen, "This extension must be paused by SourceMod.");
}
return false;
}
@ -406,7 +407,7 @@ bool SDKExtension::Unpause(char *error, size_t maxlen)
{
if (error)
{
snprintf(error, maxlen, "This extension must be unpaused by SourceMod.");
ke::SafeStrcpy(error, maxlen, "This extension must be unpaused by SourceMod.");
}
return false;
}

View File

@ -43,6 +43,7 @@
#include <sp_vm_api.h>
#include <sm_platform.h>
#include <ISourceMod.h>
#include "am-string.h"
#if defined SMEXT_ENABLE_FORWARDSYS
#include <IForwardSys.h>
#endif //SMEXT_ENABLE_FORWARDSYS
@ -327,7 +328,7 @@ extern IServerGameDLL *gamedll;
{ \
if (error != NULL && maxlength) \
{ \
size_t len = snprintf(error, maxlength, "Could not find interface: %s", SMINTERFACE_##prefix##_NAME); \
size_t len = ke::SafeSprintf(error, maxlength, "Could not find interface: %s", SMINTERFACE_##prefix##_NAME); \
if (len >= maxlength) \
{ \
error[maxlength - 1] = '\0'; \
@ -344,7 +345,7 @@ extern IServerGameDLL *gamedll;
{ \
if (error != NULL && maxlength) \
{ \
size_t len = snprintf(error, maxlength, "Could not find interface: %s", SMINTERFACE_##prefix##_NAME); \
size_t len = ke::SafeSprintf(error, maxlength, "Could not find interface: %s", SMINTERFACE_##prefix##_NAME); \
if (len >= maxlength) \
{ \
error[maxlength - 1] = '\0'; \