Skip to content

Commit

Permalink
socket: Use may_alias on sockaddr structs (bug 19622)
Browse files Browse the repository at this point in the history
This supports common coding patterns.  The GCC C front end before
version 7 rejects the may_alias attribute on a struct definition
if it was not present in a previous forward declaration, so this
attribute can only be conditionally applied.

This implements the spirit of the change in Austin Group issue 1641.

Suggested-by: Marek Polacek <[email protected]>
Suggested-by: Jakub Jelinek <[email protected]>
Reviewed-by: Sam James <[email protected]>
Reviewed-by: Carlos O'Donell <[email protected]>
  • Loading branch information
fweimer-rh committed May 18, 2024
1 parent 2a99e23 commit 8d7b6b4
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 14 deletions.
4 changes: 2 additions & 2 deletions bits/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ enum __socket_type
#include <bits/sockaddr.h>

/* Structure describing a generic socket address. */
struct sockaddr
struct __attribute_struct_may_alias__ sockaddr
{
__SOCKADDR_COMMON (sa_); /* Common data: address family and length. */
char sa_data[14]; /* Address data. */
Expand All @@ -166,7 +166,7 @@ struct sockaddr
#define _SS_PADSIZE \
(_SS_SIZE - __SOCKADDR_COMMON_SIZE - sizeof (__ss_aligntype))

struct sockaddr_storage
struct __attribute_struct_may_alias__ sockaddr_storage
{
__SOCKADDR_COMMON (ss_); /* Address family, etc. */
char __ss_padding[_SS_PADSIZE];
Expand Down
8 changes: 5 additions & 3 deletions inet/netinet/in.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ extern const struct in6_addr in6addr_loopback; /* ::1 */


/* Structure describing an Internet socket address. */
struct sockaddr_in
struct __attribute_struct_may_alias__ sockaddr_in
{
__SOCKADDR_COMMON (sin_);
in_port_t sin_port; /* Port number. */
Expand All @@ -257,9 +257,11 @@ struct sockaddr_in
- sizeof (struct in_addr)];
};

#if !__USE_KERNEL_IPV6_DEFS
#if __USE_KERNEL_IPV6_DEFS
struct __attribute_struct_may_alias__ sockaddr_in6;
#else
/* Ditto, for IPv6. */
struct sockaddr_in6
struct __attribute_struct_may_alias__ sockaddr_in6
{
__SOCKADDR_COMMON (sin6_);
in_port_t sin6_port; /* Transport layer port # */
Expand Down
9 changes: 9 additions & 0 deletions misc/sys/cdefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -874,4 +874,13 @@ _Static_assert (0, "IEEE 128-bits long double requires redirection on this platf
# define __attribute_returns_twice__ /* Ignore. */
#endif

/* Mark struct types as aliasable. Restricted to compilers that
support forward declarations of structs in the presence of the
attribute. */
#if __GNUC_PREREQ (7, 1) || defined __clang__
# define __attribute_struct_may_alias__ __attribute__ ((__may_alias__))
#else
# define __attribute_struct_may_alias__
#endif

#endif /* sys/cdefs.h */
2 changes: 1 addition & 1 deletion socket/sys/un.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
__BEGIN_DECLS

/* Structure describing the address of an AF_LOCAL (aka AF_UNIX) socket. */
struct sockaddr_un
struct __attribute_struct_may_alias__ sockaddr_un
{
__SOCKADDR_COMMON (sun_);
char sun_path[108]; /* Path name. */
Expand Down
4 changes: 2 additions & 2 deletions sysdeps/mach/hurd/bits/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ enum __socket_type
#include <bits/sockaddr.h>

/* Structure describing a generic socket address. */
struct sockaddr
struct __attribute_struct_may_alias__ sockaddr
{
__SOCKADDR_COMMON (sa_); /* Common data: address family and length. */
char sa_data[14]; /* Address data. */
Expand All @@ -170,7 +170,7 @@ struct sockaddr
#define _SS_PADSIZE \
(_SS_SIZE - __SOCKADDR_COMMON_SIZE - sizeof (__ss_aligntype))

struct sockaddr_storage
struct __attribute_struct_may_alias__ sockaddr_storage
{
__SOCKADDR_COMMON (ss_); /* Address family, etc. */
char __ss_padding[_SS_PADSIZE];
Expand Down
4 changes: 2 additions & 2 deletions sysdeps/unix/sysv/linux/bits/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ typedef __socklen_t socklen_t;
#include <bits/sockaddr.h>

/* Structure describing a generic socket address. */
struct sockaddr
struct __attribute_struct_may_alias__ sockaddr
{
__SOCKADDR_COMMON (sa_); /* Common data: address family and length. */
char sa_data[14]; /* Address data. */
Expand All @@ -194,7 +194,7 @@ struct sockaddr
#define _SS_PADSIZE \
(_SS_SIZE - __SOCKADDR_COMMON_SIZE - sizeof (__ss_aligntype))

struct sockaddr_storage
struct __attribute_struct_may_alias__ sockaddr_storage
{
__SOCKADDR_COMMON (ss_); /* Address family, etc. */
char __ss_padding[_SS_PADSIZE];
Expand Down
2 changes: 1 addition & 1 deletion sysdeps/unix/sysv/linux/net/if_packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
From Linux 2.1 the AF_PACKET interface is preferred and you should
consider using it in place of this one. */

struct sockaddr_pkt
struct __attribute_struct_may_alias__ sockaddr_pkt
{
__SOCKADDR_COMMON (spkt_);
unsigned char spkt_device[14];
Expand Down
2 changes: 1 addition & 1 deletion sysdeps/unix/sysv/linux/netash/ash.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <features.h>
#include <bits/sockaddr.h>

struct sockaddr_ash
struct __attribute_struct_may_alias__ sockaddr_ash
{
__SOCKADDR_COMMON (sash_); /* Common data: address family etc. */
int sash_ifindex; /* Interface to use. */
Expand Down
2 changes: 1 addition & 1 deletion sysdeps/unix/sysv/linux/neteconet/ec.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct ec_addr
unsigned char net; /* Network number. */
};

struct sockaddr_ec
struct __attribute_struct_may_alias__ sockaddr_ec
{
__SOCKADDR_COMMON (sec_);
unsigned char port; /* Port number. */
Expand Down
2 changes: 1 addition & 1 deletion sysdeps/unix/sysv/linux/netiucv/iucv.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

__BEGIN_DECLS

struct sockaddr_iucv
struct __attribute_struct_may_alias__ sockaddr_iucv
{
__SOCKADDR_COMMON (siucv_);
unsigned short siucv_port; /* Reserved */
Expand Down

0 comments on commit 8d7b6b4

Please sign in to comment.