Browse Source

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

Fixes https://github.com/libsdl-org/SDL/issues/7600
Sam Lantinga 1 year ago
parent
commit
72b7acfe8a
3 changed files with 18 additions and 6 deletions
  1. 12 4
      src/SDL_error.c
  2. 1 1
      src/SDL_error_c.h
  3. 5 1
      src/thread/SDL_thread.c

+ 12 - 4
src/SDL_error.c

@@ -30,7 +30,7 @@ int SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
     if (fmt) {
         va_list ap;
         int result;
-        SDL_error *error = SDL_GetErrBuf();
+        SDL_error *error = SDL_GetErrBuf(SDL_TRUE);
 
         error->error = SDL_ErrorCodeGeneric;
 
@@ -62,7 +62,11 @@ int SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
 /* Available for backwards compatibility */
 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) {
     case SDL_ErrorCodeGeneric:
@@ -76,7 +80,11 @@ const char *SDL_GetError(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 */
@@ -84,7 +92,7 @@ int SDL_Error(SDL_errorcode code)
 {
     switch (code) {
     case SDL_ENOMEM:
-        SDL_GetErrBuf()->error = SDL_ErrorCodeOutOfMemory;
+        SDL_GetErrBuf(SDL_TRUE)->error = SDL_ErrorCodeOutOfMemory;
         return -1;
     case SDL_EFREAD:
         return SDL_SetError("Error reading from datastream");

+ 1 - 1
src/SDL_error_c.h

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

+ 5 - 1
src/thread/SDL_thread.c

@@ -212,7 +212,7 @@ static void SDLCALL SDL_FreeErrBuf(void *data)
 #endif
 
 /* Routine to get the thread-specific error variable */
-SDL_error *SDL_GetErrBuf(void)
+SDL_error *SDL_GetErrBuf(SDL_bool create)
 {
 #ifdef SDL_THREADS_DISABLED
     return SDL_GetStaticErrBuf();
@@ -223,6 +223,10 @@ SDL_error *SDL_GetErrBuf(void)
     const SDL_error *ALLOCATION_IN_PROGRESS = (SDL_error *)-1;
     SDL_error *errbuf;
 
+    if (!tls_errbuf && !create) {
+        return NULL;
+    }
+
     /* 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,
        but that's very unlikely and hopefully won't cause issues.