error: Alternate between two buffers in SDL_SetError.

This way you can always safely use SDL_GetError() in your formatted string:

```c
SDL_SetError("Couldn't open '%s': %s", filename, SDL_GetError());
```

This problem was hidden on platforms that use the dynamic API, because it
would format the new error string to a separate buffer first, to deal with
the varargs entry point.

Fixes #15456.
This commit is contained in:
Ryan C. Gordon
2026-04-25 14:56:51 -04:00
parent 30b0d2d8d0
commit 559d226fc6
3 changed files with 41 additions and 21 deletions

View File

@@ -262,9 +262,12 @@ void SDL_Generic_QuitTLSData(void)
static SDL_error *SDL_GetStaticErrBuf(void)
{
static SDL_error SDL_global_error;
static char SDL_global_error_str[128];
SDL_global_error.str = SDL_global_error_str;
SDL_global_error.len = sizeof(SDL_global_error_str);
static char SDL_global_error_str1[128];
static char SDL_global_error_str2[128];
SDL_global_error.info[0].str = SDL_global_error_str1;
SDL_global_error.info[0].len = sizeof(SDL_global_error_str1);
SDL_global_error.info[1].str = SDL_global_error_str2;
SDL_global_error.info[1].len = sizeof(SDL_global_error_str2);
return &SDL_global_error;
}
@@ -272,9 +275,11 @@ static SDL_error *SDL_GetStaticErrBuf(void)
static void SDLCALL SDL_FreeErrBuf(void *data)
{
SDL_error *errbuf = (SDL_error *)data;
if (errbuf->str) {
errbuf->free_func(errbuf->str);
if (errbuf->info[0].str) {
errbuf->free_func(errbuf->info[0].str);
}
if (errbuf->info[1].str) {
errbuf->free_func(errbuf->info[1].str);
}
errbuf->free_func(errbuf);
}