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

Peter Hutterer peter.hutterer at who-t.net
Mon Oct 25 17:02:02 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>
---
 src/XListDev.c |   29 +++++++++++++++++++++--------
 1 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/src/XListDev.c b/src/XListDev.c
index c544ae0..ee1ecbd 100644
--- a/src/XListDev.c
+++ b/src/XListDev.c
@@ -58,6 +58,19 @@ 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)
 {
@@ -66,18 +79,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:
@@ -102,7 +115,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 +127,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 +139,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