stdlib: Reworked SDL_vswprintf to be more efficient and return correct values.

Fixes #11729.
This commit is contained in:
Ryan C. Gordon
2025-01-04 21:53:05 -05:00
parent 8509041a09
commit 181995b44f

View File

@@ -2346,9 +2346,7 @@ int SDL_vsnprintf(SDL_OUT_Z_CAP(maxlen) char *text, size_t maxlen, SDL_PRINTF_FO
int SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, const wchar_t *fmt, va_list ap) int SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, const wchar_t *fmt, va_list ap)
{ {
char *text_utf8 = NULL, *fmt_utf8 = NULL; char *fmt_utf8 = NULL;
int result;
if (fmt) { if (fmt) {
fmt_utf8 = SDL_iconv_string("UTF-8", "WCHAR_T", (const char *)fmt, (SDL_wcslen(fmt) + 1) * sizeof(wchar_t)); fmt_utf8 = SDL_iconv_string("UTF-8", "WCHAR_T", (const char *)fmt, (SDL_wcslen(fmt) + 1) * sizeof(wchar_t));
if (!fmt_utf8) { if (!fmt_utf8) {
@@ -2356,33 +2354,55 @@ int SDL_vswprintf(SDL_OUT_Z_CAP(maxlen) wchar_t *text, size_t maxlen, const wcha
} }
} }
if (!maxlen) { char tinybuf[64]; // for really small strings, calculate it once.
// We still need to generate the text to find the final text length
maxlen = 1024; // generate the text to find the final text length
} va_list aq;
text_utf8 = (char *)SDL_malloc(maxlen * 4); va_copy(aq, ap);
if (!text_utf8) { const int utf8len = SDL_vsnprintf(tinybuf, sizeof (tinybuf), fmt_utf8, aq);
va_end(aq);
if (utf8len < 0) {
SDL_free(fmt_utf8); SDL_free(fmt_utf8);
return -1; return -1;
} }
result = SDL_vsnprintf(text_utf8, maxlen * 4, fmt_utf8, ap); bool isstack = false;
char *smallbuf = NULL;
char *utf8buf;
int result;
if (result >= 0) { if (utf8len < sizeof (tinybuf)) { // whole thing fit in the stack buffer, just use that copy.
wchar_t *text_wchar = (wchar_t *)SDL_iconv_string("WCHAR_T", "UTF-8", text_utf8, SDL_strlen(text_utf8) + 1); utf8buf = tinybuf;
if (text_wchar) { } else { // didn't fit in the stack buffer, allocate the needed space and run it again.
if (text) { utf8buf = smallbuf = SDL_small_alloc(char, utf8len + 1, &isstack);
SDL_wcslcpy(text, text_wchar, maxlen); if (!smallbuf) {
SDL_free(fmt_utf8);
return -1; // oh well.
} }
result = (int)SDL_wcslen(text_wchar); const int utf8len2 = SDL_vsnprintf(smallbuf, utf8len + 1, fmt_utf8, ap);
SDL_free(text_wchar); if (utf8len2 > utf8len) {
SDL_free(fmt_utf8);
return SDL_SetError("Formatted output changed between two runs"); // race condition on the parameters, and we no longer have room...yikes.
}
}
SDL_free(fmt_utf8);
wchar_t *wbuf = (wchar_t *)SDL_iconv_string("WCHAR_T", "UTF-8", utf8buf, utf8len + 1);
if (wbuf) {
if (text) {
SDL_wcslcpy(text, wbuf, maxlen);
}
result = (int)SDL_wcslen(wbuf);
SDL_free(wbuf);
} else { } else {
result = -1; result = -1;
} }
}
SDL_free(text_utf8); if (smallbuf != NULL) {
SDL_free(fmt_utf8); SDL_small_free(smallbuf, isstack);
}
return result; return result;
} }