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.
This commit is contained in:
Ozkan Sezer 2020-12-10 11:20:56 +03:00
parent eec73dfd20
commit f1cab8aea6
6 changed files with 34 additions and 16 deletions

View File

@ -3997,6 +3997,7 @@ SDL_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
int show_cursor_prev; int show_cursor_prev;
SDL_bool mouse_captured; SDL_bool mouse_captured;
SDL_Window *current_window; SDL_Window *current_window;
SDL_MessageBoxData mbdata;
if (!messageboxdata) { if (!messageboxdata) {
return SDL_InvalidParamError("messageboxdata"); return SDL_InvalidParamError("messageboxdata");
@ -4016,6 +4017,11 @@ SDL_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
buttonid = &dummybutton; buttonid = &dummybutton;
} }
SDL_memcpy(&mbdata, messageboxdata, sizeof(*messageboxdata));
if (!mbdata.title) mbdata.title = "";
if (!mbdata.message) mbdata.message = "";
messageboxdata = &mbdata;
if (_this && _this->ShowMessageBox) { if (_this && _this->ShowMessageBox) {
retval = _this->ShowMessageBox(_this, messageboxdata, buttonid); 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 /* Web browsers don't (currently) have an API for a custom message box
that can block, but for the most common case (SDL_ShowSimpleMessageBox), that can block, but for the most common case (SDL_ShowSimpleMessageBox),
we can use the standard Javascript alert() function. */ we can use the standard Javascript alert() function. */
if (!title) title = "";
if (!message) message = "";
EM_ASM_({ EM_ASM_({
alert(UTF8ToString($0) + "\n\n" + UTF8ToString($1)); alert(UTF8ToString($0) + "\n\n" + UTF8ToString($1));
}, title, message); }, title, message);

View File

@ -154,9 +154,9 @@ class HAIKU_SDL_MessageBox : public BAlert
ApplyAndParseColorScheme(aMessageBoxData->colorScheme); ApplyAndParseColorScheme(aMessageBoxData->colorScheme);
} }
(aMessageBoxData->title != NULL) ? (aMessageBoxData->title && aMessageBoxData->title[0]) ?
SetTitle(aMessageBoxData->title) : SetTitle(HAIKU_SDL_DefTitle); SetTitle(aMessageBoxData->title) : SetTitle(HAIKU_SDL_DefTitle);
(aMessageBoxData->message != NULL) ? (aMessageBoxData->message && aMessageBoxData->message[0]) ?
SetMessageText(aMessageBoxData->message) : SetMessageText(HAIKU_SDL_DefMessage); SetMessageText(aMessageBoxData->message) : SetMessageText(HAIKU_SDL_DefMessage);
SetType(ConvertMessageBoxType(static_cast<SDL_MessageBoxFlags>(aMessageBoxData->flags))); SetType(ConvertMessageBoxType(static_cast<SDL_MessageBoxFlags>(aMessageBoxData->flags)));

View File

@ -205,12 +205,9 @@ static HWND _makeDlg(const SDL_MessageBoxData *messageboxdata)
pSDLBtnData = (SDL_MessageBoxButtonData *)messageboxdata->buttons; pSDLBtnData = (SDL_MessageBoxButtonData *)messageboxdata->buttons;
ULONG cSDLBtnData = messageboxdata->numbuttons; ULONG cSDLBtnData = messageboxdata->numbuttons;
PSZ pszTitle = (messageboxdata->title == NULL)? NULL : PSZ pszTitle = OS2_UTF8ToSys((PSZ) messageboxdata->title);
OS2_UTF8ToSys((PSZ) messageboxdata->title);
ULONG cbTitle = (pszTitle == NULL)? 0 : strlen(pszTitle); ULONG cbTitle = (pszTitle == NULL)? 0 : strlen(pszTitle);
PSZ pszText = OS2_UTF8ToSys((PSZ) messageboxdata->message);
PSZ pszText = (messageboxdata->message == NULL)? NULL :
OS2_UTF8ToSys((PSZ) messageboxdata->message);
ULONG cbText = (pszText == NULL)? 0 : strlen(pszText); ULONG cbText = (pszText == NULL)? 0 : strlen(pszText);
PDLGTEMPLATE pTemplate; PDLGTEMPLATE pTemplate;

View File

@ -256,10 +256,6 @@ static SDL_bool AddDialogString(WIN_DialogData *dialog, const char *string)
size_t count; size_t count;
SDL_bool status; SDL_bool status;
if (!string) {
string = "";
}
wstring = WIN_UTF8ToString(string); wstring = WIN_UTF8ToString(string);
if (!wstring) { if (!wstring) {
return SDL_FALSE; return SDL_FALSE;
@ -318,7 +314,7 @@ static SDL_bool AddDialogControl(WIN_DialogData *dialog, WORD type, DWORD style,
if (!AddDialogData(dialog, &type, sizeof(type))) { if (!AddDialogData(dialog, &type, sizeof(type))) {
return SDL_FALSE; return SDL_FALSE;
} }
if (type == DLGITEMTYPEBUTTON || (type == DLGITEMTYPESTATIC && caption != NULL)) { if (type == DLGITEMTYPEBUTTON || (type == DLGITEMTYPESTATIC && caption != NULL && caption[0])) {
if (!AddDialogString(dialog, caption)) { if (!AddDialogString(dialog, caption)) {
return SDL_FALSE; return SDL_FALSE;
} }

View File

@ -408,7 +408,6 @@ X11_MessageBoxCreateWindow( SDL_MessageBoxDataX11 *data )
Display *display = data->display; Display *display = data->display;
SDL_WindowData *windowdata = NULL; SDL_WindowData *windowdata = NULL;
const SDL_MessageBoxData *messageboxdata = data->messageboxdata; const SDL_MessageBoxData *messageboxdata = data->messageboxdata;
const char *title = messageboxdata->title ? messageboxdata->title : "";
char *title_locale = NULL; char *title_locale = NULL;
if ( messageboxdata->window ) { if ( messageboxdata->window ) {
@ -453,10 +452,10 @@ X11_MessageBoxCreateWindow( SDL_MessageBoxDataX11 *data )
X11_XSetTransientForHint( display, data->window, windowdata->xwindow ); 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); _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) { if (title_locale) {
XTextProperty titleprop; XTextProperty titleprop;
Status status = X11_XStringListToTextProperty(&title_locale, 1, &titleprop); Status status = X11_XStringListToTextProperty(&title_locale, 1, &titleprop);
@ -470,7 +469,7 @@ X11_MessageBoxCreateWindow( SDL_MessageBoxDataX11 *data )
#ifdef X_HAVE_UTF8_STRING #ifdef X_HAVE_UTF8_STRING
if (SDL_X11_HAVE_UTF8) { if (SDL_X11_HAVE_UTF8) {
XTextProperty titleprop; XTextProperty titleprop;
Status status = X11_Xutf8TextListToTextProperty(display, (char **) &title, 1, Status status = X11_Xutf8TextListToTextProperty(display, (char **) &messageboxdata->title, 1,
XUTF8StringStyle, &titleprop); XUTF8StringStyle, &titleprop);
if (status == Success) { if (status == Success) {
X11_XSetTextProperty(display, data->window, &titleprop, X11_XSetTextProperty(display, data->window, &titleprop,

View File

@ -106,6 +106,24 @@ main(int argc, char *argv[])
quit(1); 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" */ /* Google says this is Traditional Chinese for "beef with broccoli" */
success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, success = SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"UTF-8 Simple MessageBox", "UTF-8 Simple MessageBox",