Segfault when retrieving windowname on X11

Hey Everybody,
I was having an issues creating an Ogre3d window then trying to call
SDL_CreateWindowFrom() to create an SDL window. The same code appears to
work currently on another platform so I tried to narrow it down the best I
could. For reference I am using Ubuntu 11.04 with Unity3d.

Specifically in the file src/video/x11/SDL_x11window.c in the function
char * X11_GetWindowTitle(_THIS, Window xwindow) I found a bug. The
function GetWindowTitle assumes that if XGetWindowProperty returns success
it will not return a null pointer. I have modified to check for null
pointers. I have attached a patch file of this change.

In the original it assumes that if the check for _NET_WM_NAME is
successful it will get a valid pointer, which seems reasonable to me, but
turned out not to be the case. So I checks to propdata pointer so that it
will check for an XA_WM_NAME, and if that fails or returns a null pointer it
will then copy a blank string. On my specific setup it finds a null pointer
for _NET_WM_NAME then successfully finds a valid XA_WM_NAME.

*Original - *------------------------------
char *
X11_GetWindowTitle(_THIS, Window xwindow)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
Display *display = data->display;
int status, real_format;
Atom real_type;
unsigned long items_read, items_left;
unsigned char *propdata;
char *title = NULL;

status = XGetWindowProperty(display, xwindow, data->_NET_WM_NAME,
            0L, 8192L, False, data->UTF8_STRING, &real_type,

&real_format,
&items_read, &items_left, &propdata);
if (status == Success) {
title = SDL_strdup(SDL_static_cast(char*, propdata));
XFree(propdata);
} else {
status = XGetWindowProperty(display, xwindow, XA_WM_NAME,
0L, 8192L, False, XA_STRING, &real_type, &real_format,
&items_read, &items_left, &propdata);
if (status == Success) {
title = SDL_iconv_string(“UTF-8”, “”, SDL_static_cast(char*,
propdata), items_read+1);
} else {
title = SDL_strdup("");
}
}
return title;
}

Fixed -

char *
X11_GetWindowTitle(_THIS, Window xwindow)
{
SDL_VideoData *data = (SDL_VideoData *) _this->driverdata;
Display *display = data->display;
int status, real_format;
Atom real_type;
unsigned long items_read, items_left;
unsigned char *propdata;
char *title = NULL;

status = XGetWindowProperty(display, xwindow, data->_NET_WM_NAME,
            0L, 8192L, False, data->UTF8_STRING, &real_type,

&real_format,
&items_read, &items_left, &propdata);
if (status == Success && propdata) {
title = SDL_strdup(SDL_static_cast(char*, propdata));
XFree(propdata);
} else {
status = XGetWindowProperty(display, xwindow, XA_WM_NAME,
0L, 8192L, False, XA_STRING, &real_type, &real_format,
&items_read, &items_left, &propdata);
if (status == Success && propdata) {
title = SDL_iconv_string(“UTF-8”, “”, SDL_static_cast(char*,
propdata), items_read+1);
} else {
title = SDL_strdup("");
}
}
return title;
}

Diff -

622c622
< if (status == Success) {

if (status == Success && propdata) {

629c629
< if (status == Success) {

    if (status == Success && propdata) {

  • Joe Toppi
    (402) 714-7539
    BlackTopp Studios Inc.
    Lead Software Developer
    Toppij at BlackToppStudios.com

Sorry for the multiple postings, but I just resolved an issue with posting
to the mailing list. This message and my previous one’s are what I failed to
send before.

On the Windows side when SDL window data being setup SDL determines what has
focus and reacts accordingly. This patch fixes that little bit for x11
windows.

As for my issue, which has been posted to the forums (
http://forums.libsdl.org/viewtopic.php?t=7618 ) and I have sent to this
mailing list, not much has changed. My application still receives input on
the windows side, and still does not retrieve input on the Linux side.
However, one tiny behavior did change. Whenever I launch my application from
my IDE, it opens a console window with some debugging messages. Before when
I launched the application, the game window would remain open even if I
closed the console window; the easiest way or me to close the whole
application was xkill. Now if I close that debugging/console window my
application closes.

Here is the patch for SDL_x11window.c:
218a219,233

{
    Window FocalWindow;
    int RevertTo=0;
    XGetInputFocus(data->videodata->display, &FocalWindow, &RevertTo);
    if (FocalWindow==w)
    {
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
        SDL_SetKeyboardFocus(data->window);
    }

    if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
        /* Tell x11 to clip mouse */
    }
}

I have attached each patch as its own file.
-------------- next part --------------
A non-text attachment was scrubbed…
Name: SDL_x11window.c.Sqeaky.Oct12.2011.patch
Type: application/octet-stream
Size: 438 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20111014/1e3d9c37/attachment.obj
-------------- next part --------------
A non-text attachment was scrubbed…
Name: SDL_x11window.c.Sqeaky.Oct8.2011.patch
Type: application/octet-stream
Size: 179 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20111014/1e3d9c37/attachment-0001.obj

Sorry for the multiple postings, but I just resolved an issue with posting
to the mailing list. This message and my previous one’s are what I failed to
send before.

I fixed something else in my long chain of fixes that do not actually
resolve my problem (not just SDL but my code and other places too).

On the Windows side when SDL window data being setup SDL determines what has
focus and reacts accordingly. This patch fixes that little bit for x11
windows.

As for my issue, which has been posted to the forums (
http://forums.libsdl.org/viewtopic.php?t=7618 ) and I have sent to this
mailing list, not much has changed. My application still receives input on
the windows side, and still does not retrieve input on the Linux side.
However, one tiny behavior did change. Whenever I launch my application from
my IDE, it opens a console window with some debugging messages. Before when
I launched the application, the game window would remain open even if I
closed the console window; the easiest way or me to close the whole
application was xkill. Now if I close that debugging/console window my
application closes.

Here is the patch for SDL_x11window.c:
218a219,233

{
    Window FocalWindow;
    int RevertTo=0;
    XGetInputFocus(data->videodata->display, &FocalWindow, &RevertTo);
    if (FocalWindow==w)
    {
        window->flags |= SDL_WINDOW_INPUT_FOCUS;
        SDL_SetKeyboardFocus(data->window);
    }

    if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
        /* Tell x11 to clip mouse */
    }
}

I have attached each patch as its own file.–