OTRGlobals: use SohUtils::CopyStringToCharBuffer

This commit is contained in:
Demur Rumed 2025-04-05 16:59:55 +00:00
parent f37c3aa228
commit 1e5d1670c0
2 changed files with 5 additions and 20 deletions

View file

@ -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<int>(0, maxBufferSize));
// Gaurentee that this value will be greater than 0, regardless of passed variables.
const int copiedCharLen = std::min<int>(std::max<int>(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<uint32_t> hira1 = {
u'', u'', u'', u'', u'', u'', u'', u'', u'', u'', u'-', u'', u'',
@ -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;
}

View file

@ -389,10 +389,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.
// Prevent potential overflow due to implicit conversion of maxBufferSize to an unsigned. Prevents negatives.
memset(buffer, 0, std::max<size_t>(0, maxBufferSize));
// Gaurentee that this value will be greater than 0, regardless of passed variables.
// Guarantee that this value will be greater than 0, regardless of passed variables.
const size_t copiedCharLen = std::min<size_t>(std::max<size_t>(0, maxBufferSize - 1), source.length());
memcpy(buffer, source.c_str(), copiedCharLen);
return copiedCharLen;