From 8bd9871b8e8579980a494f49e7e628e3aeafca84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20M=C3=BCller?= Date: Sat, 21 Oct 2023 21:50:51 +0200 Subject: [PATCH] Check for skin name length when loading, refactoring Remove obsolete check for duplicate skins, as the storage handles this already. --- src/game/client/components/skins.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/src/game/client/components/skins.cpp b/src/game/client/components/skins.cpp index 3789432168c..4c181f6caf8 100644 --- a/src/game/client/components/skins.cpp +++ b/src/game/client/components/skins.cpp @@ -56,27 +56,29 @@ struct SSkinScanUser int CSkins::SkinScan(const char *pName, int IsDir, int DirType, void *pUser) { - auto *pUserReal = (SSkinScanUser *)pUser; + auto *pUserReal = static_cast(pUser); CSkins *pSelf = pUserReal->m_pThis; - if(IsDir || !str_endswith(pName, ".png")) + const char *pSuffix = str_endswith(pName, ".png"); + if(IsDir || pSuffix == nullptr) return 0; - char aNameWithoutPng[128]; - str_copy(aNameWithoutPng, pName); - aNameWithoutPng[str_length(aNameWithoutPng) - 4] = 0; - - if(g_Config.m_ClVanillaSkinsOnly && !IsVanillaSkin(aNameWithoutPng)) + char aSkinName[IO_MAX_PATH_LENGTH]; + str_truncate(aSkinName, sizeof(aSkinName), pName, pSuffix - pName); + if(str_length(aSkinName) >= (int)CSkin::MAX_NAME_LENGTH) + { + char aBuf[64 + IO_MAX_PATH_LENGTH]; + str_format(aBuf, sizeof(aBuf), "Skin name too long (maximum %d characters): '%s'", (int)CSkin::MAX_NAME_LENGTH - 1, aSkinName); + pSelf->Console()->Print(IConsole::OUTPUT_LEVEL_STANDARD, "game", aBuf); return 0; + } - // Don't add duplicate skins (one from user's config directory, other from - // client itself) - if(pSelf->m_Skins.find(aNameWithoutPng) != pSelf->m_Skins.end()) + if(g_Config.m_ClVanillaSkinsOnly && !IsVanillaSkin(aSkinName)) return 0; - char aBuf[IO_MAX_PATH_LENGTH]; - str_format(aBuf, sizeof(aBuf), "skins/%s", pName); - pSelf->LoadSkin(aNameWithoutPng, aBuf, DirType); + char aPath[IO_MAX_PATH_LENGTH]; + str_format(aPath, sizeof(aPath), "skins/%s", pName); + pSelf->LoadSkin(aSkinName, aPath, DirType); pUserReal->m_SkinLoadedFunc((int)pSelf->m_Skins.size()); return 0; }