[runtime] Fix snprintf usage (#4109)
This commit is contained in:
@@ -919,13 +919,14 @@ KInt Kotlin_StringBuilder_insertInt(KRef builder, KInt position, KInt value) {
|
|||||||
RuntimeAssert(toArray->count_ >= 11 + position, "must be true");
|
RuntimeAssert(toArray->count_ >= 11 + position, "must be true");
|
||||||
char cstring[12];
|
char cstring[12];
|
||||||
auto length = konan::snprintf(cstring, sizeof(cstring), "%d", value);
|
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* from = &cstring[0];
|
||||||
auto* to = CharArrayAddressOfElementAt(toArray, position);
|
auto* to = CharArrayAddressOfElementAt(toArray, position);
|
||||||
auto* end = from + length;
|
while (*from) {
|
||||||
while (from != end) {
|
|
||||||
*to++ = *from++;
|
*to++ = *from++;
|
||||||
}
|
}
|
||||||
return length;
|
return from - cstring;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -110,16 +110,21 @@ void consolePrintf(const char* format, ...) {
|
|||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
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);
|
va_end(args);
|
||||||
consoleWriteUtf8(buffer, rv);
|
consoleWriteUtf8(buffer, rv);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Avoid code duplication.
|
||||||
void consoleErrorf(const char* format, ...) {
|
void consoleErrorf(const char* format, ...) {
|
||||||
char buffer[1024];
|
char buffer[1024];
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
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);
|
va_end(args);
|
||||||
consoleErrorUtf8(buffer, rv);
|
consoleErrorUtf8(buffer, rv);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user