Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Core/GameEngine/Include/Common/AsciiString.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ class AsciiString
*/
AsciiString(const char* s);

/**
Constructs an AsciiString with the given string and length.
The length must not be larger than the actual string length.
*/
AsciiString(const char* s, int len);

/**
Destructor. Not too exciting... clean up the works and such.
*/
Expand Down Expand Up @@ -200,12 +206,20 @@ class AsciiString
refcount.)
*/
void set(const AsciiString& stringSrc);

/**
Replace the contents of self with the given string.
Note that a copy of the string is made; the input ptr is not saved.
*/
void set(const char* s);

/**
Replace the contents of self with the given string and length.
Note that a copy of the string is made; the input ptr is not saved.
The length must not be larger than the actual string length.
*/
void set(const char* s, int len);

/**
replace contents of self with the given string. Note the
nomenclature is translate rather than set; this is because
Expand Down
14 changes: 14 additions & 0 deletions Core/GameEngine/Include/Common/UnicodeString.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,12 @@ class UnicodeString
*/
explicit UnicodeString(const WideChar* s);

/**
Constructs an UnicodeString with the given string and length.
The length must not be larger than the actual string length.
*/
UnicodeString(const WideChar* s, int len);

/**
Destructor. Not too exciting... clean up the works and such.
*/
Expand Down Expand Up @@ -200,12 +206,20 @@ class UnicodeString
refcount.)
*/
void set(const UnicodeString& stringSrc);

/**
Replace the contents of self with the given string.
Note that a copy of the string is made; the input ptr is not saved.
*/
void set(const WideChar* s);

/**
Replace the contents of self with the given string and length.
Note that a copy of the string is made; the input ptr is not saved.
The length must not be larger than the actual string length.
*/
void set(const WideChar* s, int len);

/**
replace contents of self with the given string. Note the
nomenclature is translate rather than set; this is because
Expand Down
42 changes: 34 additions & 8 deletions Core/GameEngine/Source/Common/System/AsciiString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,20 @@ void AsciiString::ensureUniqueBufferOfSize(int numCharsNeeded, Bool preserveData
{
validate();

const int usableNumChars = numCharsNeeded - 1;

if (m_data &&
m_data->m_refCount == 1 &&
m_data->m_numCharsAllocated >= numCharsNeeded)
{
// no buffer manhandling is needed (it's already large enough, and unique to us)
if (strToCopy)
{
// TheSuperHackers @fix Mauller 04/04/2025 Replace strcpy with safer memmove as memory regions can overlap when part of string is copied to itself
memmove(m_data->peek(), strToCopy, strlen(strToCopy) + 1);
DEBUG_ASSERTCRASH(usableNumChars <= strlen(strToCopy), ("strToCopy is too small"));
memmove(m_data->peek(), strToCopy, usableNumChars);
m_data->peek()[usableNumChars] = 0;
}
if (strToCat)
strcat(m_data->peek(), strToCat);
return;
Expand All @@ -157,7 +163,11 @@ void AsciiString::ensureUniqueBufferOfSize(int numCharsNeeded, Bool preserveData
// do these BEFORE releasing the old buffer, so that self-copies
// or self-cats will work correctly.
if (strToCopy)
strcpy(newData->peek(), strToCopy);
{
DEBUG_ASSERTCRASH(usableNumChars <= strlen(strToCopy), ("strToCopy is too small"));
strncpy(newData->peek(), strToCopy, usableNumChars);
newData->peek()[usableNumChars] = 0;
}
if (strToCat)
strcat(newData->peek(), strToCat);

Expand Down Expand Up @@ -186,11 +196,21 @@ void AsciiString::releaseBuffer()
}

// -----------------------------------------------------
AsciiString::AsciiString(const char* s) : m_data(0)
AsciiString::AsciiString(const char* s) : m_data(NULL)
{
//DEBUG_ASSERTCRASH(isMemoryManagerOfficiallyInited(), ("Initializing AsciiStrings prior to main (ie, as static vars) can cause memory leak reporting problems. Are you sure you want to do this?"));
int len = (s)?strlen(s):0;
if (len)
int len = s ? (int)strlen(s) : 0;
if (len > 0)
{
ensureUniqueBufferOfSize(len + 1, false, s, NULL);
}
validate();
}

// -----------------------------------------------------
AsciiString::AsciiString(const char* s, int len) : m_data(NULL)
{
if (len > 0)
{
ensureUniqueBufferOfSize(len + 1, false, s, NULL);
}
Expand All @@ -215,12 +235,18 @@ void AsciiString::set(const AsciiString& stringSrc)

// -----------------------------------------------------
void AsciiString::set(const char* s)
{
int len = s ? strlen(s) : 0;
set(s, len);
}

// -----------------------------------------------------
void AsciiString::set(const char* s, int len)
{
validate();
if (!m_data || s != peek())
{
int len = s ? strlen(s) : 0;
if (len)
if (len > 0)
{
ensureUniqueBufferOfSize(len + 1, false, s, NULL);
}
Expand Down Expand Up @@ -375,7 +401,7 @@ void AsciiString::truncateBy(const Int charCount)
const size_t len = strlen(peek());
if (len > 0)
{
ensureUniqueBufferOfSize(len+1, true, NULL, NULL);
ensureUniqueBufferOfSize(len + 1, true, NULL, NULL);
size_t count = charCount;
if (charCount > len)
{
Expand Down
42 changes: 34 additions & 8 deletions Core/GameEngine/Source/Common/System/UnicodeString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,20 @@ void UnicodeString::ensureUniqueBufferOfSize(int numCharsNeeded, Bool preserveDa
{
validate();

const int usableNumChars = numCharsNeeded - 1;

if (m_data &&
m_data->m_refCount == 1 &&
m_data->m_numCharsAllocated >= numCharsNeeded)
{
// no buffer manhandling is needed (it's already large enough, and unique to us)
if (strToCopy)
{
// TheSuperHackers @fix Mauller 04/04/2025 Replace wcscpy with safer memmove as memory regions can overlap when part of string is copied to itself
memmove(m_data->peek(), strToCopy, (wcslen(strToCopy) + 1) * sizeof(WideChar));
DEBUG_ASSERTCRASH(usableNumChars <= wcslen(strToCopy), ("strToCopy is too small"));
memmove(m_data->peek(), strToCopy, usableNumChars * sizeof(WideChar));
m_data->peek()[usableNumChars] = 0;
}
if (strToCat)
wcscat(m_data->peek(), strToCat);
return;
Expand All @@ -108,7 +114,11 @@ void UnicodeString::ensureUniqueBufferOfSize(int numCharsNeeded, Bool preserveDa
// do these BEFORE releasing the old buffer, so that self-copies
// or self-cats will work correctly.
if (strToCopy)
wcscpy(newData->peek(), strToCopy);
{
DEBUG_ASSERTCRASH(usableNumChars <= wcslen(strToCopy), ("strToCopy is too small"));
wcsncpy(newData->peek(), strToCopy, usableNumChars);
newData->peek()[usableNumChars] = 0;
}
if (strToCat)
wcscat(newData->peek(), strToCat);

Expand Down Expand Up @@ -136,10 +146,20 @@ void UnicodeString::releaseBuffer()
}

// -----------------------------------------------------
UnicodeString::UnicodeString(const WideChar* s) : m_data(0)
UnicodeString::UnicodeString(const WideChar* s) : m_data(NULL)
{
int len = wcslen(s);
if (len)
int len = s ? (int)wcslen(s) : 0;
if (len > 0)
{
ensureUniqueBufferOfSize(len + 1, false, s, NULL);
}
validate();
}

// -----------------------------------------------------
UnicodeString::UnicodeString(const WideChar* s, int len) : m_data(NULL)
{
if (len > 0)
{
ensureUniqueBufferOfSize(len + 1, false, s, NULL);
}
Expand All @@ -164,12 +184,18 @@ void UnicodeString::set(const UnicodeString& stringSrc)

// -----------------------------------------------------
void UnicodeString::set(const WideChar* s)
{
int len = s ? wcslen(s) : 0;
set(s, len);
}

// -----------------------------------------------------
void UnicodeString::set(const WideChar* s, int len)
{
validate();
if (!m_data || s != peek())
{
int len = s ? wcslen(s) : 0;
if (len)
if (len > 0)
{
ensureUniqueBufferOfSize(len + 1, false, s, NULL);
}
Expand Down Expand Up @@ -307,7 +333,7 @@ void UnicodeString::truncateBy(const Int charCount)
const size_t len = wcslen(peek());
if (len > 0)
{
ensureUniqueBufferOfSize(len+1, true, NULL, NULL);
ensureUniqueBufferOfSize(len + 1, true, NULL, NULL);
size_t count = charCount;
if (charCount > len)
{
Expand Down
Loading