Don't create a TLS error buffer if we're just clearing the error

Fixes https://github.com/libsdl-org/SDL/issues/7600
This commit is contained in:
Sam Lantinga 2023-12-16 21:27:44 -08:00
parent 240e7747c8
commit 72b7acfe8a
3 changed files with 18 additions and 6 deletions

View file

@ -30,7 +30,7 @@ int SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
if (fmt) { if (fmt) {
va_list ap; va_list ap;
int result; int result;
SDL_error *error = SDL_GetErrBuf(); SDL_error *error = SDL_GetErrBuf(SDL_TRUE);
error->error = SDL_ErrorCodeGeneric; error->error = SDL_ErrorCodeGeneric;
@ -62,7 +62,11 @@ int SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
/* Available for backwards compatibility */ /* Available for backwards compatibility */
const char *SDL_GetError(void) const char *SDL_GetError(void)
{ {
const SDL_error *error = SDL_GetErrBuf(); const SDL_error *error = SDL_GetErrBuf(SDL_FALSE);
if (!error) {
return "";
}
switch (error->error) { switch (error->error) {
case SDL_ErrorCodeGeneric: case SDL_ErrorCodeGeneric:
@ -76,7 +80,11 @@ const char *SDL_GetError(void)
void SDL_ClearError(void) void SDL_ClearError(void)
{ {
SDL_GetErrBuf()->error = SDL_ErrorCodeNone; SDL_error *error = SDL_GetErrBuf(SDL_FALSE);
if (error) {
error->error = SDL_ErrorCodeNone;
}
} }
/* Very common errors go here */ /* Very common errors go here */
@ -84,7 +92,7 @@ int SDL_Error(SDL_errorcode code)
{ {
switch (code) { switch (code) {
case SDL_ENOMEM: case SDL_ENOMEM:
SDL_GetErrBuf()->error = SDL_ErrorCodeOutOfMemory; SDL_GetErrBuf(SDL_TRUE)->error = SDL_ErrorCodeOutOfMemory;
return -1; return -1;
case SDL_EFREAD: case SDL_EFREAD:
return SDL_SetError("Error reading from datastream"); return SDL_SetError("Error reading from datastream");

View file

@ -44,6 +44,6 @@ typedef struct SDL_error
} SDL_error; } SDL_error;
/* Defined in SDL_thread.c */ /* Defined in SDL_thread.c */
extern SDL_error *SDL_GetErrBuf(void); extern SDL_error *SDL_GetErrBuf(SDL_bool create);
#endif /* SDL_error_c_h_ */ #endif /* SDL_error_c_h_ */

View file

@ -212,7 +212,7 @@ static void SDLCALL SDL_FreeErrBuf(void *data)
#endif #endif
/* Routine to get the thread-specific error variable */ /* Routine to get the thread-specific error variable */
SDL_error *SDL_GetErrBuf(void) SDL_error *SDL_GetErrBuf(SDL_bool create)
{ {
#ifdef SDL_THREADS_DISABLED #ifdef SDL_THREADS_DISABLED
return SDL_GetStaticErrBuf(); return SDL_GetStaticErrBuf();
@ -223,6 +223,10 @@ SDL_error *SDL_GetErrBuf(void)
const SDL_error *ALLOCATION_IN_PROGRESS = (SDL_error *)-1; const SDL_error *ALLOCATION_IN_PROGRESS = (SDL_error *)-1;
SDL_error *errbuf; SDL_error *errbuf;
if (!tls_errbuf && !create) {
return NULL;
}
/* tls_being_created is there simply to prevent recursion if SDL_CreateTLS() fails. /* tls_being_created is there simply to prevent recursion if SDL_CreateTLS() fails.
It also means it's possible for another thread to also use SDL_global_errbuf, It also means it's possible for another thread to also use SDL_global_errbuf,
but that's very unlikely and hopefully won't cause issues. but that's very unlikely and hopefully won't cause issues.