Skip to content

Commit 39d7229

Browse files
committed
refactor(string): make ensureUniqueBufferOfSize always null-terminate at requested size
1 parent d82dd69 commit 39d7229

2 files changed

Lines changed: 4 additions & 8 deletions

File tree

Core/GameEngine/Source/Common/System/AsciiString.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ void AsciiString::ensureUniqueBufferOfSize(int numCharsNeeded, Bool preserveData
138138
// TheSuperHackers @fix Mauller 04/04/2025 Replace strcpy with safer memmove as memory regions can overlap when part of string is copied to itself
139139
DEBUG_ASSERTCRASH(usableNumChars <= strlen(strToCopy), ("strToCopy is too small"));
140140
memmove(m_data->peek(), strToCopy, usableNumChars);
141-
m_data->peek()[usableNumChars] = 0;
142141
}
142+
m_data->peek()[usableNumChars] = 0;
143143
if (strToCat)
144144
strcat(m_data->peek(), strToCat);
145145
return;
@@ -167,8 +167,8 @@ void AsciiString::ensureUniqueBufferOfSize(int numCharsNeeded, Bool preserveData
167167
{
168168
DEBUG_ASSERTCRASH(usableNumChars <= strlen(strToCopy), ("strToCopy is too small"));
169169
strncpy(newData->peek(), strToCopy, usableNumChars);
170-
newData->peek()[usableNumChars] = 0;
171170
}
171+
newData->peek()[usableNumChars] = 0;
172172
if (strToCat)
173173
strcat(newData->peek(), strToCat);
174174

@@ -286,8 +286,6 @@ void AsciiString::translate(const UnicodeString& stringSrc)
286286
char* buf = peek();
287287
if (!Unicode_To_Utf8(buf, src, srcLen, size))
288288
clear();
289-
else
290-
buf[size] = '\0';
291289
validate();
292290
}
293291

Core/GameEngine/Source/Common/System/UnicodeString.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ void UnicodeString::ensureUniqueBufferOfSize(int numCharsNeeded, Bool preserveDa
8989
// TheSuperHackers @fix Mauller 04/04/2025 Replace wcscpy with safer memmove as memory regions can overlap when part of string is copied to itself
9090
DEBUG_ASSERTCRASH(usableNumChars <= wcslen(strToCopy), ("strToCopy is too small"));
9191
memmove(m_data->peek(), strToCopy, usableNumChars * sizeof(WideChar));
92-
m_data->peek()[usableNumChars] = 0;
9392
}
93+
m_data->peek()[usableNumChars] = 0;
9494
if (strToCat)
9595
wcscat(m_data->peek(), strToCat);
9696
return;
@@ -118,8 +118,8 @@ void UnicodeString::ensureUniqueBufferOfSize(int numCharsNeeded, Bool preserveDa
118118
{
119119
DEBUG_ASSERTCRASH(usableNumChars <= wcslen(strToCopy), ("strToCopy is too small"));
120120
wcsncpy(newData->peek(), strToCopy, usableNumChars);
121-
newData->peek()[usableNumChars] = 0;
122121
}
122+
newData->peek()[usableNumChars] = 0;
123123
if (strToCat)
124124
wcscat(newData->peek(), strToCat);
125125

@@ -235,8 +235,6 @@ void UnicodeString::translate(const AsciiString& stringSrc)
235235
WideChar* buf = peek();
236236
if (!Utf8_To_Unicode(buf, src, srcLen, size))
237237
clear();
238-
else
239-
buf[size] = L'\0';
240238
validate();
241239
}
242240

0 commit comments

Comments
 (0)