Commit f1cab8aea61242f6fed248412f8376c5b89479d7

Ozkan Sezer 2020-12-10T11:20:56

fix bug #5253: handle NULL title or message fields in SDL_MessageBoxData - SDL_video.c (SDL_ShowMessageBox): replace messageboxdata, set title or message field to "" if either of them is NULL. - SDL_video.c (SDL_ShowSimpleMessageBox): set title or message to "" if either of them is NULL for EMSCRIPTEN builds. - SDL_bmessagebox.cc: add empty string check along with NULL check for title and message fields. - SDL_windowsmessagebox.c (AddDialogString): remove NULL string check - SDL_windowsmessagebox.c (AddDialogControl): add empty string check along with the NULL check. - SDL_x11messagebox.c: revert commit 677c4cd68069 - SDL_os2messagebox.c: revert commit 2c2a489d76e7 - test/testmessage.c: Add NULL title and NULL message tests.

diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 646b000..4703ce3 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -3997,6 +3997,7 @@ SDL_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
     int show_cursor_prev;
     SDL_bool mouse_captured;
     SDL_Window *current_window;
+    SDL_MessageBoxData mbdata;
 
     if (!messageboxdata) {
         return SDL_InvalidParamError("messageboxdata");
@@ -4016,6 +4017,11 @@ SDL_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
         buttonid = &dummybutton;
     }
 
+    SDL_memcpy(&mbdata, messageboxdata, sizeof(*messageboxdata));
+    if (!mbdata.title) mbdata.title = "";
+    if (!mbdata.message) mbdata.message = "";
+    messageboxdata = &mbdata;
+
     if (_this && _this->ShowMessageBox) {
         retval = _this->ShowMessageBox(_this, messageboxdata, buttonid);
     }
@@ -4101,6 +4107,8 @@ SDL_ShowSimpleMessageBox(Uint32 flags, const char *title, const char *message, S
     /* Web browsers don't (currently) have an API for a custom message box
        that can block, but for the most common case (SDL_ShowSimpleMessageBox),
        we can use the standard Javascript alert() function. */
+    if (!title) title = "";
+    if (!message) message = "";
     EM_ASM_({
         alert(UTF8ToString($0) + "\n\n" + UTF8ToString($1));
     }, title, message);
diff --git a/src/video/haiku/SDL_bmessagebox.cc b/src/video/haiku/SDL_bmessagebox.cc
index cf33033..c7ec206 100644
--- a/src/video/haiku/SDL_bmessagebox.cc
+++ b/src/video/haiku/SDL_bmessagebox.cc
@@ -154,9 +154,9 @@ class HAIKU_SDL_MessageBox : public BAlert
 			ApplyAndParseColorScheme(aMessageBoxData->colorScheme);
 		}
 
-		(aMessageBoxData->title != NULL) ?
+		(aMessageBoxData->title && aMessageBoxData->title[0]) ?
 			SetTitle(aMessageBoxData->title) : SetTitle(HAIKU_SDL_DefTitle);
-		(aMessageBoxData->message != NULL) ?
+		(aMessageBoxData->message && aMessageBoxData->message[0]) ?
 			SetMessageText(aMessageBoxData->message) : SetMessageText(HAIKU_SDL_DefMessage);
 
 		SetType(ConvertMessageBoxType(static_cast<SDL_MessageBoxFlags>(aMessageBoxData->flags)));
diff --git a/src/video/os2/SDL_os2messagebox.c b/src/video/os2/SDL_os2messagebox.c
index e82b8d3..e3b9267 100644
--- a/src/video/os2/SDL_os2messagebox.c
+++ b/src/video/os2/SDL_os2messagebox.c
@@ -205,12 +205,9 @@ static HWND _makeDlg(const SDL_MessageBoxData *messageboxdata)
         pSDLBtnData =  (SDL_MessageBoxButtonData *)messageboxdata->buttons;
     ULONG               cSDLBtnData = messageboxdata->numbuttons;
 
-    PSZ                 pszTitle = (messageboxdata->title == NULL)? NULL :
-                                    OS2_UTF8ToSys((PSZ) messageboxdata->title);
+    PSZ                 pszTitle = OS2_UTF8ToSys((PSZ) messageboxdata->title);
     ULONG               cbTitle = (pszTitle == NULL)? 0 : strlen(pszTitle);
-
-    PSZ                 pszText = (messageboxdata->message == NULL)? NULL :
-                                   OS2_UTF8ToSys((PSZ) messageboxdata->message);
+    PSZ                 pszText = OS2_UTF8ToSys((PSZ) messageboxdata->message);
     ULONG               cbText = (pszText == NULL)? 0 : strlen(pszText);
 
     PDLGTEMPLATE        pTemplate;
diff --git a/src/video/windows/SDL_windowsmessagebox.c b/src/video/windows/SDL_windowsmessagebox.c
index 1a7dcad..5bf464f 100644
--- a/src/video/windows/SDL_windowsmessagebox.c
+++ b/src/video/windows/SDL_windowsmessagebox.c
@@ -256,10 +256,6 @@ static SDL_bool AddDialogString(WIN_DialogData *dialog, const char *string)
     size_t count;
     SDL_bool status;
 
-    if (!string) {
-        string = "";
-    }
-
     wstring = WIN_UTF8ToString(string);
     if (!wstring) {
         return SDL_FALSE;
@@ -318,7 +314,7 @@ static SDL_bool AddDialogControl(WIN_DialogData *dialog, WORD type, DWORD style,
     if (!AddDialogData(dialog, &type, sizeof(type))) {
         return SDL_FALSE;
     }
-    if (type == DLGITEMTYPEBUTTON || (type == DLGITEMTYPESTATIC && caption != NULL)) {
+    if (type == DLGITEMTYPEBUTTON || (type == DLGITEMTYPESTATIC && caption != NULL && caption[0])) {
         if (!AddDialogString(dialog, caption)) {
             return SDL_FALSE;
         }
diff --git a/src/video/x11/SDL_x11messagebox.c b/src/video/x11/SDL_x11messagebox.c
index 5f8825a..776a4af 100644
--- a/src/video/x11/SDL_x11messagebox.c
+++ b/src/video/x11/SDL_x11messagebox.c
@@ -408,7 +408,6 @@ X11_MessageBoxCreateWindow( SDL_MessageBoxDataX11 *data )
     Display *display = data->display;
     SDL_WindowData *windowdata = NULL;
     const SDL_MessageBoxData *messageboxdata = data->messageboxdata;
-    const char *title = messageboxdata->title ? messageboxdata->title : "";
     char *title_locale = NULL;
 
     if ( messageboxdata->window ) {
@@ -453,10 +452,10 @@ X11_MessageBoxCreateWindow( SDL_MessageBoxDataX11 *data )
         X11_XSetTransientForHint( display, data->window, windowdata->xwindow );
     }
 
-    X11_XStoreName( display, data->window, title);
+    X11_XStoreName( display, data->window, messageboxdata->title );
     _NET_WM_NAME = X11_XInternAtom(display, "_NET_WM_NAME", False);
 
-    title_locale = SDL_iconv_utf8_locale(title);
+    title_locale = SDL_iconv_utf8_locale(messageboxdata->title);
     if (title_locale) {
         XTextProperty titleprop;
         Status status = X11_XStringListToTextProperty(&title_locale, 1, &titleprop);
@@ -470,7 +469,7 @@ X11_MessageBoxCreateWindow( SDL_MessageBoxDataX11 *data )
 #ifdef X_HAVE_UTF8_STRING
     if (SDL_X11_HAVE_UTF8) {
         XTextProperty titleprop;
-        Status status = X11_Xutf8TextListToTextProperty(display, (char **) &title, 1,
+        Status status = X11_Xutf8TextListToTextProperty(display, (char **) &messageboxdata->title, 1,
                                             XUTF8StringStyle, &titleprop);
         if (status == Success) {
             X11_XSetTextProperty(display, data->window, &titleprop,
diff --git a/test/testmessage.c b/test/testmessage.c
index 946d458..295b24b 100644
--- a/test/testmessage.c
+++ b/test/testmessage.c
@@ -106,6 +106,24 @@ main(int argc, char *argv[])
         quit(1);
     }
 
+    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
+                NULL,
+                "NULL Title",
+                NULL);
+    if (success == -1) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
+        quit(1);
+    }
+
+    success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
+                "NULL Message",
+                NULL,
+                NULL);
+    if (success == -1) {
+        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Error Presenting MessageBox: %s\n", SDL_GetError());
+        quit(1);
+    }
+
     /* Google says this is Traditional Chinese for "beef with broccoli" */
     success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
                 "UTF-8 Simple MessageBox",