From 3b9c74ce96507514d741006c5bcde701bd2184b3 Mon Sep 17 00:00:00 2001 From: Benoist <14257866+Kenzzer@users.noreply.github.com> Date: Wed, 19 Feb 2025 13:10:37 +0100 Subject: [PATCH] Fix storing garbage strings (#2257) --- extensions/cstrike/util_cstrike.cpp | 9 ++------ extensions/sdktools/gamerulesnatives.cpp | 29 ++++++++++++++---------- extensions/sdktools/teamnatives.cpp | 18 +++++++++------ extensions/sdktools/variant-t.cpp | 5 +++- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/extensions/cstrike/util_cstrike.cpp b/extensions/cstrike/util_cstrike.cpp index e51dced03..b3a7c5504 100644 --- a/extensions/cstrike/util_cstrike.cpp +++ b/extensions/cstrike/util_cstrike.cpp @@ -240,15 +240,10 @@ void CreateHashMaps() if (!pSchema) return; - static const char *pPriceKey = NULL; - + const char *pPriceKey = g_pGameConf->GetKeyValue("PriceKey"); if (!pPriceKey) { - pPriceKey = g_pGameConf->GetKeyValue("PriceKey"); - if (!pPriceKey) - { - return; - } + return; } static int iHashMapOffset = -1; diff --git a/extensions/sdktools/gamerulesnatives.cpp b/extensions/sdktools/gamerulesnatives.cpp index ca6c8728b..ad13e2e85 100644 --- a/extensions/sdktools/gamerulesnatives.cpp +++ b/extensions/sdktools/gamerulesnatives.cpp @@ -33,11 +33,16 @@ #include "gamerulesnatives.h" #include "vglobals.h" -const char *g_szGameRulesProxy; +char g_szGameRulesProxy[64] = { 0 }; void GameRulesNativesInit() { - g_szGameRulesProxy = g_pGameConf->GetKeyValue("GameRulesProxy"); + auto key = g_pGameConf->GetKeyValue("GameRulesProxy"); + if (key) + { + strncpy(g_szGameRulesProxy, key, sizeof(g_szGameRulesProxy)); + g_szGameRulesProxy[sizeof(g_szGameRulesProxy) - 1] = '\0'; + } } static CBaseEntity *FindEntityByNetClass(int start, const char *classname) @@ -202,7 +207,7 @@ static cell_t GameRules_GetProp(IPluginContext *pContext, const cell_t *params) void *pGameRules = GameRules(); - if (!pGameRules || !g_szGameRulesProxy || !strcmp(g_szGameRulesProxy, "")) + if (!pGameRules || !g_szGameRulesProxy[0]) return pContext->ThrowNativeError("Gamerules lookup failed."); pContext->LocalToString(params[1], &prop); @@ -274,7 +279,7 @@ static cell_t GameRules_SetProp(IPluginContext *pContext, const cell_t *params) if ((pProxy = GetGameRulesProxyEnt()) == NULL) return pContext->ThrowNativeError("Couldn't find gamerules proxy entity"); - if (!pGameRules || !g_szGameRulesProxy || !strcmp(g_szGameRulesProxy, "")) + if (!pGameRules || !g_szGameRulesProxy[0]) return pContext->ThrowNativeError("Gamerules lookup failed"); pContext->LocalToString(params[1], &prop); @@ -335,7 +340,7 @@ static cell_t GameRules_GetPropFloat(IPluginContext *pContext, const cell_t *par void *pGameRules = GameRules(); - if (!pGameRules || !g_szGameRulesProxy || !strcmp(g_szGameRulesProxy, "")) + if (!pGameRules || !g_szGameRulesProxy[0]) return pContext->ThrowNativeError("Gamerules lookup failed."); pContext->LocalToString(params[1], &prop); @@ -360,7 +365,7 @@ static cell_t GameRules_SetPropFloat(IPluginContext *pContext, const cell_t *par if ((pProxy = GetGameRulesProxyEnt()) == NULL) return pContext->ThrowNativeError("Couldn't find gamerules proxy entity."); - if (!pGameRules || !g_szGameRulesProxy || !strcmp(g_szGameRulesProxy, "")) + if (!pGameRules || !g_szGameRulesProxy[0]) return pContext->ThrowNativeError("Gamerules lookup failed."); pContext->LocalToString(params[1], &prop); @@ -394,7 +399,7 @@ static cell_t GameRules_GetPropEnt(IPluginContext *pContext, const cell_t *param void *pGameRules = GameRules(); - if (!pGameRules || !g_szGameRulesProxy || !strcmp(g_szGameRulesProxy, "")) + if (!pGameRules || !g_szGameRulesProxy[0]) return pContext->ThrowNativeError("Gamerules lookup failed."); pContext->LocalToString(params[1], &prop); @@ -425,7 +430,7 @@ static cell_t GameRules_SetPropEnt(IPluginContext *pContext, const cell_t *param if ((pProxy = GetGameRulesProxyEnt()) == NULL) return pContext->ThrowNativeError("Couldn't find gamerules proxy entity."); - if (!pGameRules || !g_szGameRulesProxy || !strcmp(g_szGameRulesProxy, "")) + if (!pGameRules || !g_szGameRulesProxy[0]) return pContext->ThrowNativeError("Gamerules lookup failed."); pContext->LocalToString(params[1], &prop); @@ -475,7 +480,7 @@ static cell_t GameRules_GetPropVector(IPluginContext *pContext, const cell_t *pa void *pGameRules = GameRules(); - if (!pGameRules || !g_szGameRulesProxy || !strcmp(g_szGameRulesProxy, "")) + if (!pGameRules || !g_szGameRulesProxy[0]) return pContext->ThrowNativeError("Gamerules lookup failed."); pContext->LocalToString(params[1], &prop); @@ -507,7 +512,7 @@ static cell_t GameRules_SetPropVector(IPluginContext *pContext, const cell_t *pa if ((pProxy = GetGameRulesProxyEnt()) == NULL) return pContext->ThrowNativeError("Couldn't find gamerules proxy entity."); - if (!pGameRules || !g_szGameRulesProxy || !strcmp(g_szGameRulesProxy, "")) + if (!pGameRules || !g_szGameRulesProxy[0]) return pContext->ThrowNativeError("Gamerules lookup failed."); pContext->LocalToString(params[1], &prop); @@ -551,7 +556,7 @@ static cell_t GameRules_GetPropString(IPluginContext *pContext, const cell_t *pa void *pGameRules = GameRules(); - if (!pGameRules || !g_szGameRulesProxy || !strcmp(g_szGameRulesProxy, "")) + if (!pGameRules || !g_szGameRulesProxy[0]) return pContext->ThrowNativeError("Gamerules lookup failed."); pContext->LocalToString(params[1], &prop); @@ -600,7 +605,7 @@ static cell_t GameRules_SetPropString(IPluginContext *pContext, const cell_t *pa if ((pProxy = GetGameRulesProxyEnt()) == NULL) return pContext->ThrowNativeError("Couldn't find gamerules proxy entity."); - if (!pGameRules || !g_szGameRulesProxy || !strcmp(g_szGameRulesProxy, "")) + if (!pGameRules || !g_szGameRulesProxy[0]) return pContext->ThrowNativeError("Gamerules lookup failed."); pContext->LocalToString(params[1], &prop); diff --git a/extensions/sdktools/teamnatives.cpp b/extensions/sdktools/teamnatives.cpp index 8f770fe5b..c35bb0b5a 100644 --- a/extensions/sdktools/teamnatives.cpp +++ b/extensions/sdktools/teamnatives.cpp @@ -39,7 +39,7 @@ struct TeamInfo CBaseEntity *pEnt; }; -const char *m_iScore; +char m_iScore[64] = { 0 }; SourceHook::CVector g_Teams; @@ -137,13 +137,15 @@ static cell_t GetTeamScore(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("Team index %d is invalid", teamindex); } - if (!m_iScore) + if (!m_iScore[0]) { - m_iScore = g_pGameConf->GetKeyValue("m_iScore"); - if (!m_iScore) + auto key = g_pGameConf->GetKeyValue("m_iScore"); + if (!key || !key[0]) { return pContext->ThrowNativeError("Failed to get m_iScore key"); } + strncpy(m_iScore, key, sizeof(m_iScore)); + m_iScore[sizeof(m_iScore) - 1] = '\0'; } static int offset = -1; @@ -175,13 +177,15 @@ static cell_t SetTeamScore(IPluginContext *pContext, const cell_t *params) return pContext->ThrowNativeError("Team index %d is invalid", teamindex); } - if (m_iScore == NULL) + if (!m_iScore[0]) { - m_iScore = g_pGameConf->GetKeyValue("m_iScore"); - if (m_iScore == NULL) + auto key = g_pGameConf->GetKeyValue("m_iScore"); + if (!key || !key[0]) { return pContext->ThrowNativeError("Failed to get m_iScore key"); } + strncpy(m_iScore, key, sizeof(m_iScore)); + m_iScore[sizeof(m_iScore) - 1] = '\0'; } static int offset = -1; diff --git a/extensions/sdktools/variant-t.cpp b/extensions/sdktools/variant-t.cpp index adb8b0f8c..7a891ca51 100644 --- a/extensions/sdktools/variant-t.cpp +++ b/extensions/sdktools/variant-t.cpp @@ -34,6 +34,7 @@ #include variant_t g_Variant_t; +char g_Variant_str_Value[128]; // copy this definition as the original file includes cbase.h which explodes in a shower of compile errors void variant_t::SetEntity( CBaseEntity *val ) @@ -59,7 +60,9 @@ static cell_t SetVariantString(IPluginContext *pContext, const cell_t *params) { char *str; pContext->LocalToString(params[1], &str); - g_Variant_t.SetString(MAKE_STRING(str)); + strncpy(g_Variant_str_Value, str, sizeof(g_Variant_str_Value)); + g_Variant_str_Value[sizeof(g_Variant_str_Value) - 1] = '\0'; + g_Variant_t.SetString(MAKE_STRING(g_Variant_str_Value)); return 1; }