From 3ca25da82007a2413a87c38bbe25fbb769f9ef26 Mon Sep 17 00:00:00 2001 From: Amir Date: Sat, 15 Aug 2026 20:27:01 -0500 Subject: [PATCH] fix(compat): terminate itoa output reliably Replace the non-portable stringbuf-backed implementation with direct integer conversion so callers receive a terminated string on Unix. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../Code/CompatLib/Source/string_compat.cpp | 44 +++++++++++++++---- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/GeneralsMD/Code/CompatLib/Source/string_compat.cpp b/GeneralsMD/Code/CompatLib/Source/string_compat.cpp index ab4f49ae321..e73ed921597 100644 --- a/GeneralsMD/Code/CompatLib/Source/string_compat.cpp +++ b/GeneralsMD/Code/CompatLib/Source/string_compat.cpp @@ -1,16 +1,44 @@ #include "string_compat.h" -#include -#include -#include +#include +#include +#include char* itoa(int value, char* str, int base) { - // Create stringbuf from str - std::stringbuf buf; - buf.pubsetbuf(str, 33); - std::ostream os(&buf); - os << value << '\0'; + if (base < 2 || base > 36) + { + str[0] = '\0'; + return str; + } + + static const char digits[] = "0123456789abcdefghijklmnopqrstuvwxyz"; + const bool is_negative = value < 0 && base == 10; + unsigned int magnitude = static_cast(value); + if (is_negative) + { + magnitude = 0U - magnitude; + } + + char* output = str; + do + { + *output++ = digits[magnitude % static_cast(base)]; + magnitude /= static_cast(base); + } while (magnitude != 0); + + if (is_negative) + { + *output++ = '-'; + } + *output = '\0'; + + for (char* left = str, *right = output - 1; left < right; ++left, --right) + { + const char temp = *left; + *left = *right; + *right = temp; + } return str; }