[PATCH] input: properly align doubles in InitValuatorClassDeviceStruct

Julien Cristau jcristau at debian.org
Fri Feb 25 15:00:39 PST 2011


Some architectures (hi, sparc!) are unhappy with unaligned memory
accesses.  So make sure the axisVal member of ValuatorClassRec has
sizeof(double) alignment to avoid crashes and test failures.

X.Org bug#34742 <https://bugs.freedesktop.org/show_bug.cgi?id=34742>

Signed-off-by: Julien Cristau <jcristau at debian.org>
---
 dix/devices.c |   10 ++++++----
 1 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/dix/devices.c b/dix/devices.c
index 6c0dc42..959817e 100644
--- a/dix/devices.c
+++ b/dix/devices.c
@@ -1225,6 +1225,7 @@ InitValuatorClassDeviceStruct(DeviceIntPtr dev, int numAxes, Atom *labels,
 {
     int i;
     ValuatorClassPtr valc;
+    int len;
 
     if (!dev)
         return FALSE;
@@ -1237,9 +1238,10 @@ InitValuatorClassDeviceStruct(DeviceIntPtr dev, int numAxes, Atom *labels,
         numAxes = MAX_VALUATORS;
     }
 
-    valc = (ValuatorClassPtr)calloc(1, sizeof(ValuatorClassRec) +
-				    numAxes * sizeof(AxisInfo) +
-				    numAxes * sizeof(double));
+    len = sizeof(ValuatorClassRec) + numAxes * sizeof(AxisInfo);
+    /* round up so that valc->axisVal is properly aligned */
+    len = (len + sizeof(double) - 1) & ~(sizeof(double) - 1);
+    valc = (ValuatorClassPtr)calloc(1, len + numAxes * sizeof(double));
     if (!valc)
 	return FALSE;
 
@@ -1252,7 +1254,7 @@ InitValuatorClassDeviceStruct(DeviceIntPtr dev, int numAxes, Atom *labels,
     valc->motionHintWindow = NullWindow;
     valc->numAxes = numAxes;
     valc->axes = (AxisInfoPtr)(valc + 1);
-    valc->axisVal = (double *)(valc->axes + numAxes);
+    valc->axisVal = (double *)((char *)valc + len);
 
     if (mode & OutOfProximity)
         InitProximityClassDeviceStruct(dev);
-- 
1.7.2.3



More information about the xorg-devel mailing list