From 9c8c36456b61856767080afeacb85dc94e1b27c4 Mon Sep 17 00:00:00 2001 From: Dennis Felsing Date: Mon, 26 Oct 2020 12:56:21 +0100 Subject: [PATCH] Use str_truncate more often --- src/game/client/components/chat.cpp | 10 +++++----- src/game/client/components/menus_demo.cpp | 2 +- src/game/client/components/race_demo.cpp | 3 +-- src/game/editor/editor.cpp | 7 ++----- 4 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/game/client/components/chat.cpp b/src/game/client/components/chat.cpp index ef7658dde..64e827511 100644 --- a/src/game/client/components/chat.cpp +++ b/src/game/client/components/chat.cpp @@ -326,7 +326,7 @@ bool CChat::OnInput(IInput::CEvent Event) for(m_PlaceholderLength = 0; *pCursor && *pCursor != ' '; ++pCursor) ++m_PlaceholderLength; - str_copy(m_aCompletionBuffer, m_Input.GetString() + m_PlaceholderOffset, minimum(static_cast(sizeof(m_aCompletionBuffer)), m_PlaceholderLength + 1)); + str_truncate(m_aCompletionBuffer, sizeof(m_aCompletionBuffer), m_Input.GetString() + m_PlaceholderOffset, m_PlaceholderLength); } if(m_aCompletionBuffer[0] == '/') @@ -372,7 +372,7 @@ bool CChat::OnInput(IInput::CEvent Event) { char aBuf[256]; // add part before the name - str_copy(aBuf, m_Input.GetString(), minimum(static_cast(sizeof(aBuf)), m_PlaceholderOffset + 1)); + str_truncate(aBuf, sizeof(aBuf), m_Input.GetString(), m_PlaceholderOffset); // add the command str_append(aBuf, "/", sizeof(aBuf)); @@ -448,7 +448,7 @@ bool CChat::OnInput(IInput::CEvent Event) { char aBuf[256]; // add part before the name - str_copy(aBuf, m_Input.GetString(), minimum(static_cast(sizeof(aBuf)), m_PlaceholderOffset + 1)); + str_truncate(aBuf, sizeof(aBuf), m_Input.GetString(), m_PlaceholderOffset); // add the name str_append(aBuf, pCompletionString, sizeof(aBuf)); @@ -591,11 +591,11 @@ void CChat::StoreSave(const char *pText) return; char aName[16]; - str_copy(aName, pStart + 27, minimum(static_cast(pMid - pStart - 26), sizeof(aName))); + str_truncate(aName, sizeof(aName), pStart + 27, pMid - pStart - 27); char aSaveCode[64]; - str_copy(aSaveCode, pMid + 13, minimum(static_cast((pOn ? pOn : pEnd) - pMid - 12), sizeof(aSaveCode))); + str_truncate(aSaveCode, sizeof(aSaveCode), pMid + 13, (pOn ? pOn : pEnd) - pMid - 13); char aTimestamp[20]; str_timestamp_format(aTimestamp, sizeof(aTimestamp), FORMAT_SPACE); diff --git a/src/game/client/components/menus_demo.cpp b/src/game/client/components/menus_demo.cpp index 6c38595a8..3bd5cb654 100644 --- a/src/game/client/components/menus_demo.cpp +++ b/src/game/client/components/menus_demo.cpp @@ -762,7 +762,7 @@ int CMenus::DemolistFetchCallback(const char *pName, time_t Date, int IsDir, int } else { - str_copy(Item.m_aName, pName, minimum(static_cast(sizeof(Item.m_aName)), str_length(pName) - 4)); + str_truncate(Item.m_aName, sizeof(Item.m_aName), pName, str_length(pName) - 5); Item.m_InfosLoaded = false; Item.m_Date = Date; } diff --git a/src/game/client/components/race_demo.cpp b/src/game/client/components/race_demo.cpp index fd8af3417..3d3cf2327 100644 --- a/src/game/client/components/race_demo.cpp +++ b/src/game/client/components/race_demo.cpp @@ -176,13 +176,12 @@ void CRaceDemo::StopRecord(int Time) int CRaceDemo::RaceDemolistFetchCallback(const char *pName, time_t Date, int IsDir, int StorageType, void *pUser) { CDemoListParam *pParam = (CDemoListParam *)pUser; - int Length = str_length(pName); int MapLen = str_length(pParam->pMap); if(IsDir || !str_endswith(pName, ".demo") || !str_startswith(pName, pParam->pMap) || pName[MapLen] != '_') return 0; CDemoItem Item; - str_copy(Item.m_aName, pName, minimum(static_cast(sizeof(Item.m_aName)), Length - 4)); + str_truncate(Item.m_aName, sizeof(Item.m_aName), pName, str_length(pName) - 5); const char *pTime = Item.m_aName + MapLen + 1; const char *pTEnd = pTime; diff --git a/src/game/editor/editor.cpp b/src/game/editor/editor.cpp index 8a80a44c7..a8d4488f8 100644 --- a/src/game/editor/editor.cpp +++ b/src/game/editor/editor.cpp @@ -4338,7 +4338,6 @@ void CEditor::RenderSounds(CUIRect ToolBox, CUIRect View) static int EditorListdirCallback(const char *pName, int IsDir, int StorageType, void *pUser) { CEditor *pEditor = (CEditor *)pUser; - int Length = str_length(pName); if((pName[0] == '.' && (pName[1] == 0 || (pName[1] == '.' && pName[2] == 0 && (!str_comp(pEditor->m_pFileDialogPath, "maps") || !str_comp(pEditor->m_pFileDialogPath, "mapres"))))) || (!IsDir && ((pEditor->m_FileDialogFileType == CEditor::FILETYPE_MAP && !str_endswith(pName, ".map")) || @@ -4352,10 +4351,8 @@ static int EditorListdirCallback(const char *pName, int IsDir, int StorageType, str_format(Item.m_aName, sizeof(Item.m_aName), "%s/", pName); else { - if(pEditor->m_FileDialogFileType == CEditor::FILETYPE_SOUND) - str_copy(Item.m_aName, pName, minimum(static_cast(sizeof(Item.m_aName)), Length - 4)); - else - str_copy(Item.m_aName, pName, minimum(static_cast(sizeof(Item.m_aName)), Length - 3)); + int LenEnding = pEditor->m_FileDialogFileType == CEditor::FILETYPE_SOUND ? 5 : 4; + str_truncate(Item.m_aName, sizeof(Item.m_aName), pName, str_length(pName) - LenEnding); } Item.m_IsDir = IsDir != 0; Item.m_IsLink = false;