Commit cd79d8ccecebc2ca9ecd8a178303acd8ef197531

Sam Lantinga 2017-07-10T17:16:12

Fixed bug 3696 - SDL_ShowMessageBox displays different error from intended Mark Callow SDL_ShowMessageBox calls SDL_CaptureMouse which, in the UIKit driver, raises a ?That operation is not supported? error, overwriting the SDL error that an application may be trying to report. This is because UIKit SDL_CaptureMouse returns SDL_Unsupported() which ends up calling SDL_SetError() which has the following code: /* If we are in debug mode, print out an error message */ SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", SDL_GetError()); The SDL_GetError call here overwrites the static buffer?.. Although an application can avoid this by using SDL_GetErrorMsg(char* errstr, int maxlen) to avoid the static buffer, SDL should be fixed. The fix is simple. In SDL_SetError change SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", SDL_GetError()); to SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", error); where error is the pointer to the buffer where it assembled the message.

diff --git a/src/SDL_error.c b/src/SDL_error.c
index 8af9cdd..498679a 100644
--- a/src/SDL_error.c
+++ b/src/SDL_error.c
@@ -49,6 +49,8 @@ SDL_LookupString(const char *key)
 
 /* Public functions */
 
+static char *SDL_GetErrorMsg(char *errstr, int maxlen);
+
 int
 SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
 {
@@ -110,14 +112,19 @@ SDL_SetError(SDL_PRINTF_FORMAT_STRING const char *fmt, ...)
     }
     va_end(ap);
 
-    /* If we are in debug mode, print out an error message */
-    SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", SDL_GetError());
-
+    if (SDL_LogGetPriority(SDL_LOG_CATEGORY_ERROR) <= SDL_LOG_PRIORITY_DEBUG) {
+        /* If we are in debug mode, print out an error message
+         * Avoid stomping on the static buffer in GetError, just
+         * in case this is called while processing a ShowMessageBox to
+         * show an error already in that static buffer.
+         */
+        char errmsg[SDL_ERRBUFIZE];
+        SDL_GetErrorMsg(errmsg, sizeof(errmsg));
+        SDL_LogDebug(SDL_LOG_CATEGORY_ERROR, "%s", errmsg);
+    }
     return -1;
 }
 
-static char *SDL_GetErrorMsg(char *errstr, int maxlen);
-
 /* Available for backwards compatibility */
 const char *
 SDL_GetError(void)