pidgin: a86331a4: util: Fix some strict-aliasing issues.

darkrain42 at pidgin.im darkrain42 at pidgin.im
Sun Apr 18 17:50:15 EDT 2010


-----------------------------------------------------------------
Revision: a86331a4dad54e913e6d67dfa71bd4f4823926ab
Ancestor: 229f226a4d7937c46842e33328e05fc827bf3573
Author: darkrain42 at pidgin.im
Date: 2010-04-17T21:13:50
Branch: im.pidgin.pidgin
URL: http://d.pidgin.im/viewmtn/revision/info/a86331a4dad54e913e6d67dfa71bd4f4823926ab

Modified files:
        libpurple/util.c

ChangeLog: 

util: Fix some strict-aliasing issues.

gcc on my desktop complained about these, though luckily I'm not the
only one who's encountered this issue.  Thanks, stackoverflow!

http://stackoverflow.com/questions/1429645/how-to-cast-sockaddr-storage-and-avoid-breaking-strict-aliasing-rules

-------------- next part --------------
============================================================
--- libpurple/util.c	c33cd30bdc5c6e9bc4b65a8758a2f91a20b27c15
+++ libpurple/util.c	aa071cb1fe0710e3d75ebebc70d4f20674526d5a
@@ -2969,33 +2969,38 @@ purple_running_osx(void)
 #endif
 }
 
+typedef union purple_sockaddr {
+	struct sockaddr         sa;
+	struct sockaddr_in      sa_in;
+#if defined(AF_INET6)
+	struct sockaddr_in6     sa_in6;
+#endif
+	struct sockaddr_storage sa_stor;
+} PurpleSockaddr;
+
 char *
 purple_fd_get_ip(int fd)
 {
-	struct sockaddr_storage addr;
+	PurpleSockaddr addr;
 	socklen_t namelen = sizeof(addr);
 	int family;
 
 	g_return_val_if_fail(fd != 0, NULL);
 
-	if (getsockname(fd, (struct sockaddr *)&addr, &namelen))
+	if (getsockname(fd, &(addr.sa), &namelen))
 		return NULL;
 
-	family = ((struct sockaddr *)&addr)->sa_family;
+	family = addr.sa.sa_family;
 
 	if (family == AF_INET) {
-		struct sockaddr_in *ipv4 = (struct sockaddr_in *)&addr;
-		struct in_addr addr = ipv4->sin_addr;
-		return g_strdup(inet_ntoa(addr));
+		return g_strdup(inet_ntoa(addr.sa_in.sin_addr));
 	}
 #if defined(AF_INET6) && defined(HAVE_INET_NTOP)
 	else if (family == AF_INET6) {
-		struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&addr;
-		struct in6_addr addr = ipv6->sin6_addr;
 		char host[INET6_ADDRSTRLEN];
 		const char *tmp;
 
-		tmp = inet_ntop(family, &addr, host, sizeof(host));
+		tmp = inet_ntop(family, &(addr.sa_in6.sin6_addr), host, sizeof(host));
 		return g_strdup(tmp);
 	}
 #endif
@@ -3006,22 +3011,22 @@ purple_socket_get_family(int fd)
 int
 purple_socket_get_family(int fd)
 {
-	struct sockaddr_storage addr;
+	PurpleSockaddr addr;
 	socklen_t len = sizeof(addr);
 
 	g_return_val_if_fail(fd >= 0, -1);
 
-	if (getsockname(fd, (struct sockaddr *)&addr, &len))
+	if (getsockname(fd, &(addr.sa), &len))
 		return -1;
 
-	return ((struct sockaddr *)&addr)->sa_family;
+	return addr.sa.sa_family;
 }
 
 gboolean
 purple_socket_speaks_ipv4(int fd)
 {
 	int family;
-	
+
 	g_return_val_if_fail(fd >= 0, FALSE);
 
 	family = purple_socket_get_family(fd);


More information about the Commits mailing list