[PATCH libXi] Force class alignment to a multiple of sizeof(XID).

Matthieu Herrb matthieu.herrb at laas.fr
Tue Jan 17 12:26:14 PST 2012


From: Peter Hutterer <peter.hutterer at who-t.net>

Calculate length field to a multiples of sizeof(XID). XIDs are typedefs
to ulong and thus may be 8 bytes on some platforms. This can trigger a
SIGBUS if a class ends up not being 8-aligned (e.g. after XAxisInfo).

Reported-by: Nicolai Stange <nicolai.stange at zmaw.de>
Signed-off-by: Peter Hutterer <peter.hutterer at who-t.net>
Signed-off-by: Matthieu Herrb <matthieu.herrb at laas.fr>
---
 src/XListDev.c |   27 +++++++++++++++++++--------
 1 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/src/XListDev.c b/src/XListDev.c
index 6a16da4..6b91238 100644
--- a/src/XListDev.c
+++ b/src/XListDev.c
@@ -61,6 +61,17 @@ SOFTWARE.
 #include <X11/extensions/extutil.h>
 #include "XIint.h"
 
+/* Calculate length field to a multiples of sizeof(XID). XIDs are typedefs
+ * to ulong and thus may be 8 bytes on some platforms. This can trigger a
+ * SIGBUS if a class ends up not being 8-aligned (e.g. after XAxisInfo).
+ */
+static int pad_to_xid(int base_size)
+{
+    int padsize = sizeof(XID);
+
+    return ((base_size + padsize - 1)/padsize) * padsize;
+}
+
 static int
 SizeClassInfo(xAnyClassPtr *any, int num_classes)
 {
@@ -69,18 +80,18 @@ SizeClassInfo(xAnyClassPtr *any, int num_classes)
     for (j = 0; j < num_classes; j++) {
         switch ((*any)->class) {
             case KeyClass:
-                size += sizeof(XKeyInfo);
+                size += pad_to_xid(sizeof(XKeyInfo));
                 break;
             case ButtonClass:
-                size += sizeof(XButtonInfo);
+                size += pad_to_xid(sizeof(XButtonInfo));
                 break;
             case ValuatorClass:
                 {
                     xValuatorInfoPtr v;
 
                     v = (xValuatorInfoPtr) *any;
-                    size += sizeof(XValuatorInfo) +
-                        (v->num_axes * sizeof(XAxisInfo));
+                    size += pad_to_xid(sizeof(XValuatorInfo) +
+                        (v->num_axes * sizeof(XAxisInfo)));
                     break;
                 }
             default:
@@ -105,7 +116,7 @@ ParseClassInfo(xAnyClassPtr *any, XAnyClassPtr *Any, int num_classes)
                     xKeyInfoPtr k = (xKeyInfoPtr) *any;
 
                     K->class = KeyClass;
-                    K->length = sizeof(XKeyInfo);
+                    K->length = pad_to_xid(sizeof(XKeyInfo));
                     K->min_keycode = k->min_keycode;
                     K->max_keycode = k->max_keycode;
                     K->num_keys = k->num_keys;
@@ -117,7 +128,7 @@ ParseClassInfo(xAnyClassPtr *any, XAnyClassPtr *Any, int num_classes)
                     xButtonInfoPtr b = (xButtonInfoPtr) *any;
 
                     B->class = ButtonClass;
-                    B->length = sizeof(XButtonInfo);
+                    B->length = pad_to_xid(sizeof(XButtonInfo));
                     B->num_buttons = b->num_buttons;
                     break;
                 }
@@ -129,8 +140,8 @@ ParseClassInfo(xAnyClassPtr *any, XAnyClassPtr *Any, int num_classes)
                     xAxisInfoPtr a;
 
                     V->class = ValuatorClass;
-                    V->length = sizeof(XValuatorInfo) +
-                        (v->num_axes * sizeof(XAxisInfo));
+                    V->length = pad_to_xid(sizeof(XValuatorInfo) +
+                        (v->num_axes * sizeof(XAxisInfo)));
                     V->num_axes = v->num_axes;
                     V->motion_buffer = v->motion_buffer_size;
                     V->mode = v->mode;
-- 
1.7.6



More information about the xorg-devel mailing list