Bug 25797 - strict-aliasing violation compiling with gcc 4.4.0
Summary: strict-aliasing violation compiling with gcc 4.4.0
Status: RESOLVED FIXED
Alias: None
Product: Farstream
Classification: Unclassified
Component: Core (show other bugs)
Version: unspecified
Hardware: x86-64 (AMD64) Linux (All)
: medium normal
Assignee: Olivier Crête
QA Contact:
URL:
Whiteboard:
Keywords:
: 22412 (view as bug list)
Depends on:
Blocks:
 
Reported: 2009-12-25 18:44 UTC by Reimundo Heluani
Modified: 2010-04-09 14:24 UTC (History)
1 user (show)

See Also:
i915 platform:
i915 features:


Attachments

Description Reimundo Heluani 2009-12-25 18:44:07 UTC
When trying to compile farsight2 with gcc I get errors of strict aliasing violations on lines of code like 

sa = (struct sockaddr_in *) &ifr.ifr_addr;

I can get rid of these errors by using a union instead of recasting, the following diff works for me:


diff --git a/gst-libs/gst/farsight/fs-interfaces.c b/gst-libs/gst/farsight/fs-in
index 66d936b..2493454 100644
--- a/gst-libs/gst/farsight/fs-interfaces.c
+++ b/gst-libs/gst/farsight/fs-interfaces.c
@@ -325,6 +325,11 @@ fs_interfaces_get_local_ips (gboolean include_loopback)
 gchar *
 fs_interfaces_get_ip_for_interface (gchar *interface_name)
 {
+  
+       union  {
+               struct sockaddr  s_addr;
+               struct sockaddr_in sin_addr;
+       } sm;
   struct ifreq ifr;
   struct sockaddr_in *sa;
   gint sockfd;
@@ -347,7 +352,8 @@ fs_interfaces_get_ip_for_interface (gchar *interface_name)
   }
 
   close (sockfd);
-  sa = (struct sockaddr_in *) &ifr.ifr_addr;
+  sm.s_addr = ifr.ifr_addr;
+  sa = &sm.sin_addr;
   GST_DEBUG ("Address for %s: %s", interface_name, inet_ntoa (sa->sin_addr));
   return g_strdup (inet_ntoa (sa->sin_addr));
 }

But I'm no coder so I'd like to know if there's a proper way of doing it. I can't check the result either cause pass that problem I'm having unrelated compiling problems.

R.
Comment 1 Olivier Crête 2009-12-25 21:07:18 UTC
That looks really evil. Maybe we should just compile with -fno-strict-aliasing
Comment 2 Reimundo Heluani 2009-12-25 22:54:53 UTC
I agree my patch is nasty, but I think type punning via unions is quite standard to avoid strict aliasing rules violations. I'm sure someone here can write something nicer, but I don't know of any other form of avoiding it. I found this old thread in the nmap development branch:

http://seclists.org/nmap-dev/2009/q3/694

the two links there toward the end are an interesting reading.

Cheers,

R.
Comment 3 Olivier Crête 2010-02-09 10:21:53 UTC
*** Bug 22412 has been marked as a duplicate of this bug. ***
Comment 4 Youness Alaoui 2010-04-09 14:19:54 UTC
patch confirmed working.
Comment 5 Olivier Crête 2010-04-09 14:24:19 UTC
Alrgiht, put the union in the git master.


Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.