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

Peter Hutterer peter.hutterer at who-t.net
Sun Oct 17 17:51:09 PDT 2010


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>
---

> That would break ABI, though, so we can't do that...  I think we should
> make sure the length field of a XAnyClassInfo is a multiple of
> sizeof(XID).

something like this?


 src/XListDev.c |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)

diff --git a/src/XListDev.c b/src/XListDev.c
index c544ae0..a9a42a1 100644
--- a/src/XListDev.c
+++ b/src/XListDev.c
@@ -89,6 +89,18 @@ SizeClassInfo(xAnyClassPtr *any, int num_classes)
     return size;
 }
 
+/* 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 void
 ParseClassInfo(xAnyClassPtr *any, XAnyClassPtr *Any, int num_classes)
 {
@@ -102,7 +114,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;
@@ -114,7 +126,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;
                 }
@@ -126,8 +138,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.2.3



More information about the xorg-devel mailing list