[PATCH] fix dereferencing of type-punned pointers

Mads Martin Joergensen mmj at panther.mmj.dk
Tue Aug 26 18:56:18 EDT 2003


Hey together,

As mentioned in my other mail, here's the patch to fix all the
"dereferencing type-punned pointer will break strict-aliasing rules"
warnings. The gcc-3.3 when compiling with -O2 will use strict-aliasing
thus there's the chance of miscompiling, which we certainly want to
avoid, but also get best optimizations possible :-)

 [ This does not include the previous patch sent to the list ]

Please apply,

--- kernel/client.c
+++ kernel/client.c
@@ -1964,6 +1964,10 @@
     } else {
         guint num;
 	gint32 *dimensions;
+	union {
+            gint32 **g;
+            guint32 **gu;
+        } u_dimensions = { &dimensions };
         Rect *a;
 
         /* pick some fallbacks... */
@@ -1974,7 +1978,7 @@
         h = a->height / 2;
 
 	if (PROP_GETA32(self->window, openbox_premax, cardinal,
-                        (guint32**)&dimensions, &num)) {
+                        u_dimensions.gu, &num)) {
             if (num == 4) {
                 x = dimensions[0];
                 y = dimensions[1];
@@ -2093,6 +2097,10 @@
 	if (savearea) {
 	    gint32 dimensions[4];
 	    gint32 *readdim;
+	    union {
+                gint32 **g;
+                guint32 **gu;
+            } u_readdim = { &readdim };
             guint num;
 
 	    dimensions[0] = x;
@@ -2103,7 +2111,7 @@
 	    /* get the property off the window and use it for the dimensions
 	       we are already maxed on */
 	    if (PROP_GETA32(self->window, openbox_premax, cardinal,
-			    (guint32**)&readdim, &num)) {
+			    u_readdim.gu, &num)) {
                 if (num == 4) {
                     if (self->max_horz) {
                         dimensions[0] = readdim[0];
@@ -2123,6 +2131,11 @@
     } else {
         guint num;
 	gint32 *dimensions;
+	union {
+            gint32 **g;
+            guint32 **gu;
+        } u_dimensions = { &dimensions };
+
         Rect *a;
 
         /* pick some fallbacks... */
@@ -2137,7 +2150,7 @@
         }
 
 	if (PROP_GETA32(self->window, openbox_premax, cardinal,
-			(guint32**)&dimensions, &num)) {
+			u_dimensions.gu, &num)) {
             if (num == 4) {
                 if (dir == 0 || dir == 1) { /* horz */
                     x = dimensions[0];
--- kernel/dispatch.c
+++ kernel/dispatch.c
@@ -88,6 +88,11 @@
 void dispatch_x(XEvent *xe, ObClient *c)
 {
     EventType e;
+    union {
+        EventType *ep;
+        XkbAnyEvent *xep;
+    } u_etype = { &e };
+
     guint i;
     GSList *it;
     ObEvent obe;
@@ -117,7 +122,7 @@
     default:
 	/* XKB events */
 	if (xe->type == extensions_xkb_event_basep) {
-	    switch (((XkbAnyEvent*)&e)->xkb_type) {
+	    switch ((u_etype.xep)->xkb_type) {
 	    case XkbBellNotify:
 		e = Event_X_Bell;
 		break;
--- kernel/event.c
+++ kernel/event.c
@@ -150,6 +150,10 @@
 {
     XEvent e;
     struct timeval *wait;
+    union {
+	struct timeval **stp;
+	GTimeVal **gtp;
+    } u_wait = { &wait };
     gboolean had_event = FALSE;
 
     while (XPending(ob_display)) {
@@ -164,7 +168,7 @@
     }
 
     if (!had_event) {
-        timer_dispatch((GTimeVal**)&wait);
+        timer_dispatch(u_wait.gtp);
         selset = allset;
         select(max_fd + 1, &selset, NULL, NULL, wait);
 
--- kernel/prop.c
+++ kernel/prop.c
@@ -285,9 +285,13 @@
 {
     GSList *strs = NULL, *it;
     char *raw, *p;
+    union {
+	char **c;
+	guchar **gc;
+    } u_raw = { &raw };
     guint num, i, count = 0;
 
-    if (get_all(win, prop, prop_atoms.string, 8, (guchar**)&raw, &num)) {
+    if (get_all(win, prop, prop_atoms.string, 8, u_raw.gc, &num)) {
 
         p = raw;
         while (p < raw + num - 1) {
@@ -316,10 +320,14 @@
 gboolean prop_get_string_utf8(Window win, Atom prop, char **ret)
 {
     char *raw;
+    union {
+	char **c;
+	guchar **gc;
+    } u_raw = { &raw };
     char *str;
     guint num;
      
-    if (get_all(win, prop, prop_atoms.utf8, 8, (guchar**)&raw, &num)) {
+    if (get_all(win, prop, prop_atoms.utf8, 8, u_raw.gc, &num)) {
 	str = g_strndup(raw, num); /* grab the first string from the list */
 	g_free(raw);
         if (g_utf8_validate(str, -1, NULL)) {
@@ -335,9 +343,13 @@
 {
     GSList *strs = NULL, *it;
     char *raw, *p;
+    union {
+	char **c;
+	guchar **gc;
+    } u_raw = { &raw };
     guint num, i, count = 0;
 
-    if (get_all(win, prop, prop_atoms.utf8, 8, (guchar**)&raw, &num)) {
+    if (get_all(win, prop, prop_atoms.utf8, 8, u_raw.gc, &num)) {
 
         p = raw;
         while (p < raw + num - 1) {
--- kernel/session.c
+++ kernel/session.c
@@ -287,6 +287,10 @@
         for (it = client_list; it; it = g_list_next(it)) {
             guint num;
             gint32 *dimensions;
+	    union {
+		gint32 **g;
+		guint32 **gu;
+	    } u_dimensions = { &dimensions };
             gint prex, prey, prew, preh;
             ObClient *c = it->data;
             gchar *client_id, *t;
@@ -302,7 +306,7 @@
             prew = c->area.width;
             preh = c->area.height;
             if (PROP_GETA32(c->window, openbox_premax, cardinal,
-                            (guint32**)&dimensions, &num)) {
+                            u_dimensions.gu, &num)) {
                 if (num == 4) {
                     prex = dimensions[0];
                     prey = dimensions[1];
--- tools/kdetrayproxy/kdetrayproxy.c
+++ tools/kdetrayproxy/kdetrayproxy.c
@@ -196,6 +196,10 @@
   int ret_format;
   unsigned long ret_items, ret_bytesleft;
   unsigned long *prop_return;
+  union {
+    unsigned long **ul;
+    unsigned char **uc;
+  } u_prop_return = { &prop_return };
 
   XQueryTree(display, win, &r, &r, &children, &n);
   for (i = 0; i < n; ++i) {
@@ -206,8 +210,7 @@
   /* try me */
   XGetWindowProperty(display, win, state, 0, 1,
 		     False, state, &ret_type, &ret_format,
-		     &ret_items, &ret_bytesleft,
-		     (unsigned char**) &prop_return); 
+		     &ret_items, &ret_bytesleft, u_prop_return.uc);
   if (ret_type == None || ret_items < 1)
     return None;
   return win; /* found it! */

-- 
Mads Martin Joergensen, http://mmj.dk
"Why make things difficult, when it is possible to make them cryptic
 and totally illogical, with just a little bit more effort?"
                                -- A. P. J.



More information about the openbox mailing list