bits/socket.h: Define struct sockaddr with may_alias attribute [BZ #19622]

Message ID 56BDDAE2.70308@redhat.com
State Dropped
Headers

Commit Message

Florian Weimer Feb. 12, 2016, 1:15 p.m. UTC
  It is quite common to use struct sockaddr * as an opaque, void *-like
type for socket addresses.  The POSIX API encourages to some degree.
Marek Polacek suggested to add the may_alias attribute to the definition
of struct sockaddr.

Support for that was introduced in GCC 4.0, so I added a conditional to
cdefs.h.

Florian
  

Comments

Florian Weimer Feb. 12, 2016, 4:51 p.m. UTC | #1
On 02/12/2016 02:15 PM, Florian Weimer wrote:
> It is quite common to use struct sockaddr * as an opaque, void *-like
> type for socket addresses.  The POSIX API encourages to some degree.
> Marek Polacek suggested to add the may_alias attribute to the definition
> of struct sockaddr.
> 
> Support for that was introduced in GCC 4.0, so I added a conditional to
> cdefs.h.

I've been told off-list to move the attribute right after the struct,
and this is the version I will commit if approved.

This patch is a result of the Fedora mass rebuild with GCC 6, where we
now see a lot of warnings related to this (potential) aliasing violation.

Florian
  
Carlos O'Donell Feb. 12, 2016, 9:58 p.m. UTC | #2
On 02/12/2016 11:51 AM, Florian Weimer wrote:
> On 02/12/2016 02:15 PM, Florian Weimer wrote:
>> It is quite common to use struct sockaddr * as an opaque, void *-like
>> type for socket addresses.  The POSIX API encourages to some degree.
>> Marek Polacek suggested to add the may_alias attribute to the definition
>> of struct sockaddr.
>>
>> Support for that was introduced in GCC 4.0, so I added a conditional to
>> cdefs.h.
> 
> I've been told off-list to move the attribute right after the struct,
> and this is the version I will commit if approved.
> 
> This patch is a result of the Fedora mass rebuild with GCC 6, where we
> now see a lot of warnings related to this (potential) aliasing violation.

Looks good to me.

c.
  
Florian Weimer Feb. 15, 2016, 4:49 p.m. UTC | #3
On 02/12/2016 02:15 PM, Florian Weimer wrote:
> It is quite common to use struct sockaddr * as an opaque, void *-like
> type for socket addresses.  The POSIX API encourages to some degree.
> Marek Polacek suggested to add the may_alias attribute to the definition
> of struct sockaddr.
> 
> Support for that was introduced in GCC 4.0, so I added a conditional to
> cdefs.h.

It turns out that the current may_alias behavior with C++ is not what we
need.  In some places, forward declarations without the attribute are
treated as a different type as the full definition with the attribute.

Patch withdrawn.

Florian
  

Patch

2016-02-12  Florian Weimer  <fweimer@redhat.com>

	[BZ #19622]
	* misc/sys/cdefs.h (__attribute_may_alias__): Define macro.
	* bits/socket.h (struct sockaddr): Use it.
	* sysdeps/mach/hurd/bits/socket.h (struct sockaddr): Likewise.
	* sysdeps/unix/sysv/linux/bits/socket.h (struct sockaddr):
	Likewise.

diff --git a/bits/socket.h b/bits/socket.h
index ab9f242..e090c65 100644
--- a/bits/socket.h
+++ b/bits/socket.h
@@ -148,7 +148,7 @@  struct sockaddr
   {
     __SOCKADDR_COMMON (sa_);	/* Common data: address family and length.  */
     char sa_data[14];		/* Address data.  */
-  };
+  } __attribute_may_alias__;
 
 
 /* Structure large enough to hold any socket address (with the historical
diff --git a/misc/sys/cdefs.h b/misc/sys/cdefs.h
index 7fd4154..aee6ae3 100644
--- a/misc/sys/cdefs.h
+++ b/misc/sys/cdefs.h
@@ -407,6 +407,12 @@ 
       [!!sizeof (struct { int __error_if_negative: (expr) ? 2 : -1; })]
 #endif
 
+#if __GNUC_PREREQ (4,0)
+# define __attribute_may_alias__ __attribute__ ((__may_alias__))
+#else
+# define __attribute_may_alias__
+#endif
+
 #include <bits/wordsize.h>
 
 #if defined __LONG_DOUBLE_MATH_OPTIONAL && defined __NO_LONG_DOUBLE_MATH
diff --git a/sysdeps/mach/hurd/bits/socket.h b/sysdeps/mach/hurd/bits/socket.h
index 02c5dac..d0dc1a3 100644
--- a/sysdeps/mach/hurd/bits/socket.h
+++ b/sysdeps/mach/hurd/bits/socket.h
@@ -152,7 +152,7 @@  struct sockaddr
   {
     __SOCKADDR_COMMON (sa_);	/* Common data: address family and length.  */
     char sa_data[14];		/* Address data.  */
-  };
+  } __attribute_may_alias__;
 
 
 /* Structure large enough to hold any socket address (with the historical
diff --git a/sysdeps/unix/sysv/linux/bits/socket.h b/sysdeps/unix/sysv/linux/bits/socket.h
index 0581c79..420c0d3 100644
--- a/sysdeps/unix/sysv/linux/bits/socket.h
+++ b/sysdeps/unix/sysv/linux/bits/socket.h
@@ -154,7 +154,7 @@  struct sockaddr
   {
     __SOCKADDR_COMMON (sa_);	/* Common data: address family and length.  */
     char sa_data[14];		/* Address data.  */
-  };
+  } __attribute_may_alias__;
 
 
 /* Structure large enough to hold any socket address (with the historical