Blob Blame History Raw
From a076837517074986ba4253d2dd8328356729ff20 Mon Sep 17 00:00:00 2001
From: Jakub Filak <jfilak@redhat.com>
Date: Wed, 29 Jul 2015 14:23:34 +0200
Subject: [PATCH] Specify prototypes for used X11 C functions

Python does not magically resolve types of C functions imported through
ctypes. Without specifying their prototypes the return type of all
functions is c_int and all Python int arguments are converted to C int
arguments.

The return type of XOpenDisplay is 'Display *', so the correct return type is
'c_void_p'.

The argument type of XCloseDisplay is 'Display *', so the correct
argument type is 'c_void_p' too.

Without this patch, the pointers returned from XOpenDisplay and passed
to XCloseDisplay are truncated to int, which is shorter than 'void *' on
some architectures, and that causes unpredicted behavior which leads to
a crash.

Related: bugzilla.redhat.com/1244261

Signed-off-by: Jakub Filak <jfilak@redhat.com>
---
 src/fros | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/src/fros b/src/fros
index 56b4070..22a62b8 100755
--- a/src/fros
+++ b/src/fros
@@ -19,7 +19,7 @@
 
 
 import sys
-from ctypes import cdll, util
+from ctypes import cdll, util, c_void_p, c_char_p
 
 XLIB_PATH = util.find_library('X11')
 if not XLIB_PATH:
@@ -27,12 +27,20 @@ if not XLIB_PATH:
     sys.exit(1)
 
 XLIB = cdll.LoadLibrary(XLIB_PATH)
-DISPLAY = XLIB.XOpenDisplay(None)
+
+XOpenDisplay = XLIB.XOpenDisplay
+XOpenDisplay.argtypes = [c_char_p]
+XOpenDisplay.restype = c_void_p
+
+DISPLAY = XOpenDisplay(None)
 if DISPLAY == 0:
     sys.stderr.write("Cannot connect to X server\n")
     sys.exit(2)
 
-XLIB.XCloseDisplay(DISPLAY)
+XCloseDisplay = XLIB.XCloseDisplay
+XCloseDisplay.argtypes = [c_void_p]
+
+XCloseDisplay(DISPLAY)
 
 from pyfros.froslogging import error, info, set_verbosity
 from pyfros.controls import Controls
-- 
2.4.6