[runtime] Fix snprintf usage (#4109)

This commit is contained in:
Vladimir Ivanov
2020-04-21 13:36:38 +03:00
committed by GitHub
parent 0dd957e561
commit f282ff0f55
2 changed files with 11 additions and 5 deletions
+4 -3
View File
@@ -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;
}
+7 -2
View File
@@ -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);
}