SDL_SetWindowTitle() should never set a NULL pointer for the title string.
authorRyan C. Gordon <icculus@icculus.org>
Wed, 08 Apr 2015 02:00:14 -0400
changeset 9474067b78126485
parent 9473 61f57bbbc2f3
child 9475 d5adc0c06a03
SDL_SetWindowTitle() should never set a NULL pointer for the title string.

Various backends reacted differently (or not at all) in the presence of a
NULL pointer. This simplifies things.

Fixes Bugzilla #2902.
src/video/SDL_video.c
src/video/cocoa/SDL_cocoawindow.m
src/video/directfb/SDL_DirectFB_WM.c
src/video/windows/SDL_windowswindow.c
src/video/x11/SDL_x11window.c
     1.1 --- a/src/video/SDL_video.c	Wed Apr 08 01:42:47 2015 -0400
     1.2 +++ b/src/video/SDL_video.c	Wed Apr 08 02:00:14 2015 -0400
     1.3 @@ -1502,11 +1502,8 @@
     1.4          return;
     1.5      }
     1.6      SDL_free(window->title);
     1.7 -    if (title && *title) {
     1.8 -        window->title = SDL_strdup(title);
     1.9 -    } else {
    1.10 -        window->title = NULL;
    1.11 -    }
    1.12 +
    1.13 +    window->title = SDL_strdup(title ? title : "");
    1.14  
    1.15      if (_this->SetWindowTitle) {
    1.16          _this->SetWindowTitle(_this, window);
     2.1 --- a/src/video/cocoa/SDL_cocoawindow.m	Wed Apr 08 01:42:47 2015 -0400
     2.2 +++ b/src/video/cocoa/SDL_cocoawindow.m	Wed Apr 08 02:00:14 2015 -0400
     2.3 @@ -1189,16 +1189,9 @@
     2.4  {
     2.5      NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
     2.6      NSWindow *nswindow = ((SDL_WindowData *) window->driverdata)->nswindow;
     2.7 -    NSString *string;
     2.8 -
     2.9 -    if(window->title) {
    2.10 -        string = [[NSString alloc] initWithUTF8String:window->title];
    2.11 -    } else {
    2.12 -        string = [[NSString alloc] init];
    2.13 -    }
    2.14 +    NSString *string = [[NSString alloc] initWithUTF8String:window->title];
    2.15      [nswindow setTitle:string];
    2.16      [string release];
    2.17 -
    2.18      [pool release];
    2.19  }
    2.20  
     3.1 --- a/src/video/directfb/SDL_DirectFB_WM.c	Wed Apr 08 01:42:47 2015 -0400
     3.2 +++ b/src/video/directfb/SDL_DirectFB_WM.c	Wed Apr 08 02:00:14 2015 -0400
     3.3 @@ -161,7 +161,7 @@
     3.4                 y, w - 2 * d);
     3.5  
     3.6      /* Caption */
     3.7 -    if (window->title) {
     3.8 +    if (*window->title) {
     3.9          s->SetColor(s, COLOR_EXPAND(t->font_color));
    3.10          DrawCraption(_this, s, (x - w) / 2, t->top_size + d, window->title);
    3.11      }
     4.1 --- a/src/video/windows/SDL_windowswindow.c	Wed Apr 08 01:42:47 2015 -0400
     4.2 +++ b/src/video/windows/SDL_windowswindow.c	Wed Apr 08 02:00:14 2015 -0400
     4.3 @@ -371,14 +371,8 @@
     4.4  WIN_SetWindowTitle(_THIS, SDL_Window * window)
     4.5  {
     4.6      HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
     4.7 -    LPTSTR title;
     4.8 -
     4.9 -    if (window->title) {
    4.10 -        title = WIN_UTF8ToString(window->title);
    4.11 -    } else {
    4.12 -        title = NULL;
    4.13 -    }
    4.14 -    SetWindowText(hwnd, title ? title : TEXT(""));
    4.15 +    LPTSTR title = WIN_UTF8ToString(window->title);
    4.16 +    SetWindowText(hwnd, title);
    4.17      SDL_free(title);
    4.18  }
    4.19  
     5.1 --- a/src/video/x11/SDL_x11window.c	Wed Apr 08 01:42:47 2015 -0400
     5.2 +++ b/src/video/x11/SDL_x11window.c	Wed Apr 08 02:00:14 2015 -0400
     5.3 @@ -658,39 +658,39 @@
     5.4      Display *display = data->videodata->display;
     5.5      XTextProperty titleprop, iconprop;
     5.6      Status status;
     5.7 -    const char *title = window->title;
     5.8 +    const char *title = window->title ? window->title : "";
     5.9      const char *icon = NULL;
    5.10 +    char *title_locale = NULL;
    5.11  
    5.12  #ifdef X_HAVE_UTF8_STRING
    5.13      Atom _NET_WM_NAME = data->videodata->_NET_WM_NAME;
    5.14      Atom _NET_WM_ICON_NAME = data->videodata->_NET_WM_ICON_NAME;
    5.15  #endif
    5.16  
    5.17 -    if (title != NULL) {
    5.18 -        char *title_locale = SDL_iconv_utf8_locale(title);
    5.19 -        if (!title_locale) {
    5.20 -            SDL_OutOfMemory();
    5.21 -            return;
    5.22 -        }
    5.23 -        status = X11_XStringListToTextProperty(&title_locale, 1, &titleprop);
    5.24 -        SDL_free(title_locale);
    5.25 -        if (status) {
    5.26 -            X11_XSetTextProperty(display, data->xwindow, &titleprop, XA_WM_NAME);
    5.27 +    title_locale = SDL_iconv_utf8_locale(title);
    5.28 +    if (!title_locale) {
    5.29 +        SDL_OutOfMemory();
    5.30 +        return;
    5.31 +    }
    5.32 +
    5.33 +    status = X11_XStringListToTextProperty(&title_locale, 1, &titleprop);
    5.34 +    SDL_free(title_locale);
    5.35 +    if (status) {
    5.36 +        X11_XSetTextProperty(display, data->xwindow, &titleprop, XA_WM_NAME);
    5.37 +        X11_XFree(titleprop.value);
    5.38 +    }
    5.39 +#ifdef X_HAVE_UTF8_STRING
    5.40 +    if (SDL_X11_HAVE_UTF8) {
    5.41 +        status = X11_Xutf8TextListToTextProperty(display, (char **) &title, 1,
    5.42 +                                            XUTF8StringStyle, &titleprop);
    5.43 +        if (status == Success) {
    5.44 +            X11_XSetTextProperty(display, data->xwindow, &titleprop,
    5.45 +                                 _NET_WM_NAME);
    5.46              X11_XFree(titleprop.value);
    5.47          }
    5.48 -#ifdef X_HAVE_UTF8_STRING
    5.49 -        if (SDL_X11_HAVE_UTF8) {
    5.50 -            status =
    5.51 -                X11_Xutf8TextListToTextProperty(display, (char **) &title, 1,
    5.52 -                                            XUTF8StringStyle, &titleprop);
    5.53 -            if (status == Success) {
    5.54 -                X11_XSetTextProperty(display, data->xwindow, &titleprop,
    5.55 -                                 _NET_WM_NAME);
    5.56 -                X11_XFree(titleprop.value);
    5.57 -            }
    5.58 -        }
    5.59 +    }
    5.60  #endif
    5.61 -    }
    5.62 +
    5.63      if (icon != NULL) {
    5.64          char *icon_locale = SDL_iconv_utf8_locale(icon);
    5.65          if (!icon_locale) {