3195: Use str_truncate more often r=heinrich5991 a=def-

<!-- What is the motivation for the changes of this pull request -->

## Checklist

- [ ] Tested the change ingame
- [ ] Provided screenshots if it is a visual change
- [ ] Tested in combination with possibly related configuration options
- [ ] Written a unit test if it works standalone, system.c especially
- [ ] Considered possible null pointers and out of bounds array indexing
- [ ] Changed no physics that affect existing maps
- [ ] Tested the change with [ASan+UBSan or valgrind's memcheck](https://github.com/ddnet/ddnet/#using-addresssanitizer--undefinedbehavioursanitizer-or-valgrinds-memcheck) (optional)


Co-authored-by: Dennis Felsing <dennis@felsin9.de>
This commit is contained in:
bors[bot] 2020-10-26 12:08:34 +00:00 committed by GitHub
commit 9a257a76e1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 13 deletions

View file

@ -326,7 +326,7 @@ bool CChat::OnInput(IInput::CEvent Event)
for(m_PlaceholderLength = 0; *pCursor && *pCursor != ' '; ++pCursor) for(m_PlaceholderLength = 0; *pCursor && *pCursor != ' '; ++pCursor)
++m_PlaceholderLength; ++m_PlaceholderLength;
str_copy(m_aCompletionBuffer, m_Input.GetString() + m_PlaceholderOffset, minimum(static_cast<int>(sizeof(m_aCompletionBuffer)), m_PlaceholderLength + 1)); str_truncate(m_aCompletionBuffer, sizeof(m_aCompletionBuffer), m_Input.GetString() + m_PlaceholderOffset, m_PlaceholderLength);
} }
if(m_aCompletionBuffer[0] == '/') if(m_aCompletionBuffer[0] == '/')
@ -372,7 +372,7 @@ bool CChat::OnInput(IInput::CEvent Event)
{ {
char aBuf[256]; char aBuf[256];
// add part before the name // add part before the name
str_copy(aBuf, m_Input.GetString(), minimum(static_cast<int>(sizeof(aBuf)), m_PlaceholderOffset + 1)); str_truncate(aBuf, sizeof(aBuf), m_Input.GetString(), m_PlaceholderOffset);
// add the command // add the command
str_append(aBuf, "/", sizeof(aBuf)); str_append(aBuf, "/", sizeof(aBuf));
@ -448,7 +448,7 @@ bool CChat::OnInput(IInput::CEvent Event)
{ {
char aBuf[256]; char aBuf[256];
// add part before the name // add part before the name
str_copy(aBuf, m_Input.GetString(), minimum(static_cast<int>(sizeof(aBuf)), m_PlaceholderOffset + 1)); str_truncate(aBuf, sizeof(aBuf), m_Input.GetString(), m_PlaceholderOffset);
// add the name // add the name
str_append(aBuf, pCompletionString, sizeof(aBuf)); str_append(aBuf, pCompletionString, sizeof(aBuf));
@ -591,11 +591,11 @@ void CChat::StoreSave(const char *pText)
return; return;
char aName[16]; char aName[16];
str_copy(aName, pStart + 27, minimum(static_cast<size_t>(pMid - pStart - 26), sizeof(aName))); str_truncate(aName, sizeof(aName), pStart + 27, pMid - pStart - 27);
char aSaveCode[64]; char aSaveCode[64];
str_copy(aSaveCode, pMid + 13, minimum(static_cast<size_t>((pOn ? pOn : pEnd) - pMid - 12), sizeof(aSaveCode))); str_truncate(aSaveCode, sizeof(aSaveCode), pMid + 13, (pOn ? pOn : pEnd) - pMid - 13);
char aTimestamp[20]; char aTimestamp[20];
str_timestamp_format(aTimestamp, sizeof(aTimestamp), FORMAT_SPACE); str_timestamp_format(aTimestamp, sizeof(aTimestamp), FORMAT_SPACE);

View file

@ -764,7 +764,7 @@ int CMenus::DemolistFetchCallback(const char *pName, time_t Date, int IsDir, int
} }
else else
{ {
str_copy(Item.m_aName, pName, minimum(static_cast<int>(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_InfosLoaded = false;
Item.m_Date = Date; Item.m_Date = Date;
} }

View file

@ -176,13 +176,12 @@ void CRaceDemo::StopRecord(int Time)
int CRaceDemo::RaceDemolistFetchCallback(const char *pName, time_t Date, int IsDir, int StorageType, void *pUser) int CRaceDemo::RaceDemolistFetchCallback(const char *pName, time_t Date, int IsDir, int StorageType, void *pUser)
{ {
CDemoListParam *pParam = (CDemoListParam *)pUser; CDemoListParam *pParam = (CDemoListParam *)pUser;
int Length = str_length(pName);
int MapLen = str_length(pParam->pMap); int MapLen = str_length(pParam->pMap);
if(IsDir || !str_endswith(pName, ".demo") || !str_startswith(pName, pParam->pMap) || pName[MapLen] != '_') if(IsDir || !str_endswith(pName, ".demo") || !str_startswith(pName, pParam->pMap) || pName[MapLen] != '_')
return 0; return 0;
CDemoItem Item; CDemoItem Item;
str_copy(Item.m_aName, pName, minimum(static_cast<int>(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 *pTime = Item.m_aName + MapLen + 1;
const char *pTEnd = pTime; const char *pTEnd = pTime;

View file

@ -4261,7 +4261,6 @@ void CEditor::RenderSounds(CUIRect ToolBox, CUIRect View)
static int EditorListdirCallback(const char *pName, int IsDir, int StorageType, void *pUser) static int EditorListdirCallback(const char *pName, int IsDir, int StorageType, void *pUser)
{ {
CEditor *pEditor = (CEditor *)pUser; CEditor *pEditor = (CEditor *)pUser;
int Length = str_length(pName);
if((pName[0] == '.' && (pName[1] == 0 || if((pName[0] == '.' && (pName[1] == 0 ||
(pName[1] == '.' && pName[2] == 0 && (!str_comp(pEditor->m_pFileDialogPath, "maps") || !str_comp(pEditor->m_pFileDialogPath, "mapres"))))) || (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")) || (!IsDir && ((pEditor->m_FileDialogFileType == CEditor::FILETYPE_MAP && !str_endswith(pName, ".map")) ||
@ -4275,10 +4274,8 @@ static int EditorListdirCallback(const char *pName, int IsDir, int StorageType,
str_format(Item.m_aName, sizeof(Item.m_aName), "%s/", pName); str_format(Item.m_aName, sizeof(Item.m_aName), "%s/", pName);
else else
{ {
if(pEditor->m_FileDialogFileType == CEditor::FILETYPE_SOUND) int LenEnding = pEditor->m_FileDialogFileType == CEditor::FILETYPE_SOUND ? 5 : 4;
str_copy(Item.m_aName, pName, minimum(static_cast<int>(sizeof(Item.m_aName)), Length - 4)); str_truncate(Item.m_aName, sizeof(Item.m_aName), pName, str_length(pName) - LenEnding);
else
str_copy(Item.m_aName, pName, minimum(static_cast<int>(sizeof(Item.m_aName)), Length - 3));
} }
Item.m_IsDir = IsDir != 0; Item.m_IsDir = IsDir != 0;
Item.m_IsLink = false; Item.m_IsLink = false;