diff --git a/runtime/src/main/cpp/KString.cpp b/runtime/src/main/cpp/KString.cpp index 3bdaa05bdf3..0763cebb0d2 100644 --- a/runtime/src/main/cpp/KString.cpp +++ b/runtime/src/main/cpp/KString.cpp @@ -919,13 +919,14 @@ KInt Kotlin_StringBuilder_insertInt(KRef builder, KInt position, KInt value) { RuntimeAssert(toArray->count_ >= 11 + position, "must be true"); char cstring[12]; auto length = konan::snprintf(cstring, sizeof(cstring), "%d", value); + RuntimeAssert(length >= 0, "This should never happen"); // may be overkill + RuntimeAssert(length < sizeof(cstring), "Unexpectedly large value"); // Can't be, but this is what sNprintf for auto* from = &cstring[0]; auto* to = CharArrayAddressOfElementAt(toArray, position); - auto* end = from + length; - while (from != end) { + while (*from) { *to++ = *from++; } - return length; + return from - cstring; } diff --git a/runtime/src/main/cpp/Porting.cpp b/runtime/src/main/cpp/Porting.cpp index c2736a1e977..25b626c48e2 100644 --- a/runtime/src/main/cpp/Porting.cpp +++ b/runtime/src/main/cpp/Porting.cpp @@ -110,16 +110,21 @@ void consolePrintf(const char* format, ...) { char buffer[1024]; va_list args; va_start(args, format); - int rv = vsnprintf_impl(buffer, sizeof(buffer) - 1, format, args); + int rv = vsnprintf_impl(buffer, sizeof(buffer), format, args); + if (rv < 0) return; // TODO: this may be too much exotic, but should i try to print itoa(error) and terminate? + if (rv >= sizeof(buffer)) rv = sizeof(buffer) - 1; // TODO: Consider realloc or report truncating. va_end(args); consoleWriteUtf8(buffer, rv); } +// TODO: Avoid code duplication. void consoleErrorf(const char* format, ...) { char buffer[1024]; va_list args; va_start(args, format); - int rv = vsnprintf_impl(buffer, sizeof(buffer) - 1, format, args); + int rv = vsnprintf_impl(buffer, sizeof(buffer), format, args); + if (rv < 0) return; // TODO: this may be too much exotic, but should i try to print itoa(error) and terminate? + if (rv >= sizeof(buffer)) rv = sizeof(buffer) - 1; // TODO: Consider realloc or report truncating. va_end(args); consoleErrorUtf8(buffer, rv); }