Fixed bug 1427 - integer passed to XChangeProperty() causes crash SDL-1.2
authorSam Lantinga
Mon, 20 Feb 2012 20:50:38 -0500
branchSDL-1.2
changeset 629357a55e457ef6
parent 6289 62ff1c0a103f
child 6294 7a2e0f7b30cb
Fixed bug 1427 - integer passed to XChangeProperty() causes crash

Julian Coleman 2012-02-20 06:51:12 PST

In src/video/x11/SDL_x11video.c, the result of getpid(), i.e., a pid_t is
passed to:

XChangeProperty(..., 32, ...)

However, using 32 here means that Xlib treats the value as a long, and pid_t is
an int. So, we get a bus error inside Xlib.
The fix is to make sure that anything passed to XChangeProperty() is aligned
correctly. Note, that the other calls to XChangeProperty() pass long values
here.
The proposed patch makes a union of the pid_t return type from getpid() and a
dummy long. This has been tested to fix the bus error crash on NetBSD/sparc64.
src/video/x11/SDL_x11video.c
     1.1 --- a/src/video/x11/SDL_x11video.c	Wed Feb 15 21:06:08 2012 -0500
     1.2 +++ b/src/video/x11/SDL_x11video.c	Mon Feb 20 20:50:38 2012 -0500
     1.3 @@ -418,16 +418,21 @@
     1.4      }
     1.5  
     1.6  	{
     1.7 -		pid_t pid = getpid();
     1.8 +		union align_pid {
     1.9 +			pid_t pid;
    1.10 +			long dummy;
    1.11 +		} a_pid;
    1.12  		char hostname[256];
    1.13 +		
    1.14 +		a_pid.pid = getpid();
    1.15  
    1.16 -		if (pid > 0 && gethostname(hostname, sizeof(hostname)) > -1) {
    1.17 +		if (a_pid.pid > 0 && gethostname(hostname, sizeof(hostname)) > -1) {
    1.18  			Atom _NET_WM_PID = XInternAtom(SDL_Display, "_NET_WM_PID", False);
    1.19  			Atom WM_CLIENT_MACHINE = XInternAtom(SDL_Display, "WM_CLIENT_MACHINE", False);
    1.20  			
    1.21  			hostname[sizeof(hostname)-1] = '\0';
    1.22  			XChangeProperty(SDL_Display, WMwindow, _NET_WM_PID, XA_CARDINAL, 32,
    1.23 -					PropModeReplace, (unsigned char *)&pid, 1);
    1.24 +					PropModeReplace, (unsigned char *)&(a_pid.pid), 1);
    1.25  			XChangeProperty(SDL_Display, WMwindow, WM_CLIENT_MACHINE, XA_STRING, 8,
    1.26  					PropModeReplace, (unsigned char *)hostname, SDL_strlen(hostname));
    1.27  		}