[PATCH] libvirt.c: warning: dereferencing type-punned pointer will break strict-aliasing rules

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




I'm currently trying to get libvirt to compile with -Werror. One problem which came up early is the warning in $SUBJECT. The gcc info page (see -fstrict-aliasing) is pretty unclear about what exactly causes this problem, so the attached patch rewrites the code quite conservatively to avoid the problem.

Rich.

--
Emerging Technologies, Red Hat  http://et.redhat.com/~rjones/
64 Baker Street, London, W1U 7DF     Mobile: +44 7866 314 421
 "[Negative numbers] darken the very whole doctrines of the equations
 and make dark of the things which are in their nature excessively
 obvious and simple" (Francis Maseres FRS, mathematician, 1759)
Index: src/libvirt.c
===================================================================
RCS file: /data/cvs/libvirt/src/libvirt.c,v
retrieving revision 1.58
diff -u -r1.58 libvirt.c
--- src/libvirt.c	23 Feb 2007 08:51:30 -0000	1.58
+++ src/libvirt.c	2 Mar 2007 11:21:18 -0000
@@ -166,7 +168,6 @@
 static int
 _virRegisterDriver(void *driver, int isNetwork)
 {
-    void **drivers;
     int i;
 
     if (!initialized)
@@ -177,17 +178,27 @@
         virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
 	return(-1);
     }
-    drivers = isNetwork ? (void **) virNetworkDriverTab : (void **) virDriverTab;
-    for (i = 0;i < MAX_DRIVERS;i++) {
-        if (drivers[i] == driver)
-	    return(i);
-    }
-    for (i = 0;i < MAX_DRIVERS;i++) {
-        if (drivers[i] == NULL) {
-	    drivers[i] = driver;
-	    return(i);
-	}
+
+    if (!isNetwork) {
+        for (i = 0;i < MAX_DRIVERS;i++)
+            if (virDriverTab[i] == driver)
+                return(i);
+        for (i = 0;i < MAX_DRIVERS;i++)
+            if (virDriverTab[i] == NULL) {
+                virDriverTab[i] = driver;
+                return(i);
+            }
+    } else {
+        for (i = 0;i < MAX_DRIVERS;i++)
+            if (virNetworkDriverTab[i] == driver)
+                return(i);
+        for (i = 0;i < MAX_DRIVERS;i++)
+            if (virNetworkDriverTab[i] == NULL) {
+                virNetworkDriverTab[i] = driver;
+                return(i);
+            }
     }
+
     virLibConnError(NULL, VIR_ERR_INVALID_ARG, __FUNCTION__);
     return(-1);
 }

Attachment: smime.p7s
Description: S/MIME Cryptographic Signature


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]