diff --git a/soh/soh/OTRGlobals.cpp b/soh/soh/OTRGlobals.cpp index 72030657e..87cb361e1 100644 --- a/soh/soh/OTRGlobals.cpp +++ b/soh/soh/OTRGlobals.cpp @@ -1741,20 +1741,6 @@ std::wstring StringToU16(const std::string& s) { return utf16; } -int CopyStringToCharBuffer(const std::string& inputStr, char* buffer, const int maxBufferSize) { - if (!inputStr.empty()) { - // Prevent potential horrible overflow due to implicit conversion of maxBufferSize to an unsigned. Prevents - // negatives. - memset(buffer, 0, std::max(0, maxBufferSize)); - // Gaurentee that this value will be greater than 0, regardless of passed variables. - const int copiedCharLen = std::min(std::max(0, maxBufferSize - 1), inputStr.length()); - memcpy(buffer, inputStr.c_str(), copiedCharLen); - return copiedCharLen; - } - - return 0; -} - extern "C" void OTRGfxPrint(const char* str, void* printer, void (*printImpl)(void*, char)) { const std::vector hira1 = { u'を', u'ぁ', u'ぃ', u'ぅ', u'ぇ', u'ぉ', u'ゃ', u'ゅ', u'ょ', u'っ', u'-', u'あ', u'い', @@ -1986,7 +1972,7 @@ extern "C" void* getN64WeirdFrame(s32 i) { return &weirdFrameBytes[i + sizeof(n64WeirdFrames)]; } -extern "C" int GetEquipNowMessage(char* buffer, char* src, const int maxBufferSize) { +extern "C" size_t GetEquipNowMessage(char* buffer, char* src, const size_t maxBufferSize) { CustomMessage customMessage("\x04\x1A\x08" "Would you like to equip it now?" "\x09&&" @@ -2027,7 +2013,7 @@ extern "C" int GetEquipNowMessage(char* buffer, char* src, const int maxBufferSi if (!str.empty()) { memset(buffer, 0, maxBufferSize); - const int copiedCharLen = std::min(maxBufferSize - 1, str.length()); + const size_t copiedCharLen = std::min(maxBufferSize - 1, str.length()); memcpy(buffer, str.c_str(), copiedCharLen); return copiedCharLen; } @@ -2169,7 +2155,7 @@ extern "C" int CustomMessage_RetrieveIfExists(PlayState* play) { uint16_t textId = msgCtx->textId; Font* font = &msgCtx->font; char* buffer = font->msgBuf; - const int maxBufferSize = sizeof(font->msgBuf); + const size_t maxBufferSize = sizeof(font->msgBuf); CustomMessage messageEntry; s16 actorParams = 0; if (IS_RANDO) { @@ -2480,14 +2466,14 @@ extern "C" int CustomMessage_RetrieveIfExists(PlayState* play) { switch (gSaveContext.language) { case LANGUAGE_FRA: return msgCtx->msgLength = font->msgLength = - CopyStringToCharBuffer(messageEntry.GetFrench(MF_RAW), buffer, maxBufferSize); + SohUtils::CopyStringToCharBuffer(buffer, messageEntry.GetFrench(MF_RAW), maxBufferSize); case LANGUAGE_GER: return msgCtx->msgLength = font->msgLength = - CopyStringToCharBuffer(messageEntry.GetGerman(MF_RAW), buffer, maxBufferSize); + SohUtils::CopyStringToCharBuffer(buffer, messageEntry.GetGerman(MF_RAW), maxBufferSize); case LANGUAGE_ENG: default: return msgCtx->msgLength = font->msgLength = - CopyStringToCharBuffer(messageEntry.GetEnglish(MF_RAW), buffer, maxBufferSize); + SohUtils::CopyStringToCharBuffer(buffer, messageEntry.GetEnglish(MF_RAW), maxBufferSize); } return false; } diff --git a/soh/soh/OTRGlobals.h b/soh/soh/OTRGlobals.h index b14d322d2..6f83d78ad 100644 --- a/soh/soh/OTRGlobals.h +++ b/soh/soh/OTRGlobals.h @@ -121,7 +121,7 @@ int Controller_ShouldRumble(size_t slot); void Controller_BlockGameInput(); void Controller_UnblockGameInput(); void* getN64WeirdFrame(s32 i); -int GetEquipNowMessage(char* buffer, char* src, const int maxBufferSize); +size_t GetEquipNowMessage(char* buffer, char* src, const size_t maxBufferSize); u32 SpoilerFileExists(const char* spoilerFileName); Sprite* GetSeedTexture(uint8_t index); uint8_t GetSeedIconIndex(uint8_t index); diff --git a/soh/soh/util.cpp b/soh/soh/util.cpp index 1e5bb2ae8..b3b6b5c7a 100644 --- a/soh/soh/util.cpp +++ b/soh/soh/util.cpp @@ -364,8 +364,10 @@ const std::string& SohUtils::GetRandomizerCheckAreaPrefix(int32_t rcarea) { } void SohUtils::CopyStringToCharArray(char* destination, std::string source, size_t size) { - strncpy(destination, source.c_str(), size - 1); - destination[size - 1] = '\0'; + if (size > 0) { + strncpy(destination, source.c_str(), size - 1); + destination[size - 1] = '\0'; + } } std::string SohUtils::Sanitize(std::string stringValue) { @@ -388,12 +390,9 @@ std::string SohUtils::Sanitize(std::string stringValue) { } size_t SohUtils::CopyStringToCharBuffer(char* buffer, const std::string& source, const size_t maxBufferSize) { - if (!source.empty()) { - // Prevent potential horrible overflow due to implicit conversion of maxBufferSize to an unsigned. Prevents - // negatives. - memset(buffer, 0, std::max(0, maxBufferSize)); - // Gaurentee that this value will be greater than 0, regardless of passed variables. - const size_t copiedCharLen = std::min(std::max(0, maxBufferSize - 1), source.length()); + if (!source.empty() && maxBufferSize > 0) { + memset(buffer, 0, maxBufferSize); + const size_t copiedCharLen = std::min(maxBufferSize - 1, source.length()); memcpy(buffer, source.c_str(), copiedCharLen); return copiedCharLen; } @@ -408,8 +407,5 @@ bool SohUtils::IsStringEmpty(std::string str) { std::string::size_type end = str.find_last_not_of(' '); // Check if the string is empty after stripping spaces - if (start == std::string::npos || end == std::string::npos) - return true; // The string is empty - else - return false; // The string is not empty + return start == std::string::npos || end == std::string::npos; }