bits/socket.h: Define struct sockaddr with may_alias attribute [BZ #19622]
Commit Message
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
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
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.
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
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.
@@ -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
@@ -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
@@ -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
@@ -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