Fix off-by-one/buffer over-read error in StrMakeValid

See: https://github.com/OpenTTD/OpenTTD/issues/11644
This commit is contained in:
Jonathan G Rennison 2023-12-29 23:55:31 +00:00
parent 74a2cd5123
commit e62c912c10
2 changed files with 3 additions and 3 deletions

View File

@ -290,7 +290,7 @@ void IniLoadFile::LoadFromDisk(const std::string &filename, Subdirectory subdir,
if (!quoted && e == t) { if (!quoted && e == t) {
item.value.reset(); item.value.reset();
} else { } else {
item.value = StrMakeValid(std::string(t)); item.value = StrMakeValid(std::string_view(t));
} }
} else { } else {
/* it's an orphan item */ /* it's an orphan item */

View File

@ -268,7 +268,7 @@ static void StrMakeValid(T &dst, const char *str, const char *last, StringValida
* would also reach the "last" byte of the string and a normal '\0' * would also reach the "last" byte of the string and a normal '\0'
* termination will be placed after it. * termination will be placed after it.
*/ */
if (len == 0 || str + len > last || len != Utf8Decode(&c, str)) { if (len == 0 || str + len > last + 1 || len != Utf8Decode(&c, str)) {
/* Maybe the next byte is still a valid character? */ /* Maybe the next byte is still a valid character? */
str++; str++;
continue; continue;
@ -341,7 +341,7 @@ void StrMakeValidInPlace(char *str, StringValidationSettings settings)
std::string StrMakeValid(std::string_view str, StringValidationSettings settings) std::string StrMakeValid(std::string_view str, StringValidationSettings settings)
{ {
auto buf = str.data(); auto buf = str.data();
auto last = buf + str.size(); auto last = buf + str.size() - 1;
std::ostringstream dst; std::ostringstream dst;
std::ostreambuf_iterator<char> dst_iter(dst); std::ostreambuf_iterator<char> dst_iter(dst);