From 1ce3ff99cf5aac60410b8f34a8c2308aec8877c6 Mon Sep 17 00:00:00 2001 From: Demur Rumed Date: Sat, 5 Apr 2025 18:57:45 +0000 Subject: [PATCH] fix issues when max buffer size is 0 --- soh/soh/util.cpp | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/soh/soh/util.cpp b/soh/soh/util.cpp index d67269d2c..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,11 +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 overflow due to implicit conversion of maxBufferSize to an unsigned. Prevents negatives. - memset(buffer, 0, std::max(0, maxBufferSize)); - // Guarantee 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; } @@ -407,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; }