Hi Richard,

On 1/23/23 17:28, Richard Biener wrote:
The common initial sequence of structures is only allowed if the structures form
part of a union (which is why to avoid UB you need a union; and still, you need
to make sure you don't invoke UB in a different way).
<https://port70.net/%7Ensz/c/c11/n1570.html#6.5.2.3p6>

GCC only allows it if the union is visible as part of the access, that
is, it allows it
under its rule of allowing punning for union accesses and not specially because
of the initial sequence rule.  So

  u.a.x = 1;
  ... = u.b.x;

is allowed but

   struct A *p = &u.a;
   p->x = 1;
   struct B *q = &u.b;
   ... = q->x;

is UB with GCC if struct A and B are the union members with a common
initial sequence.

Yep. That's why we need a union that is defined in libc, so that it can be used both in and out of glibc. sockaddr_storage can be reconverted to that purpose.

Cheers,

Alex

--
<http://www.alejandro-colomar.es/>

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

Reply via email to