public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Alex Colomar <alx.manpages@gmail.com>
To: Richard Biener <richard.guenther@gmail.com>
Cc: "Stefan Puiu" <stefan.puiu@gmail.com>,
	"GNU C Library" <libc-alpha@sourceware.org>,
	linux-man <linux-man@vger.kernel.org>,
	gcc@gcc.gnu.org, "Igor Sysoev" <igor@sysoev.ru>,
	"Bastien Roucariès" <rouca@debian.org>
Subject: Re: struct sockaddr_storage
Date: Tue, 24 Jan 2023 17:38:35 +0100	[thread overview]
Message-ID: <5dbf06be-e083-4b39-343d-2507802b9881@gmail.com> (raw)
In-Reply-To: <CAFiYyc29oHbDqP8fdrT8cSr0Vnunu24sGQuQb65vK0P37rfnmg@mail.gmail.com>


[-- Attachment #1.1: Type: text/plain, Size: 1039 bytes --]

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 #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

  reply	other threads:[~2023-01-24 16:38 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-19 14:11 Alejandro Colomar
2023-01-20 10:06 ` Stefan Puiu
2023-01-20 12:39   ` Alejandro Colomar
2023-01-23  7:40     ` Stefan Puiu
2023-01-23 16:03       ` Alejandro Colomar
2023-01-23 16:28         ` Richard Biener
2023-01-24 16:38           ` Alex Colomar [this message]
2023-01-23 16:37         ` Jakub Jelinek
2023-01-24 16:40           ` Alex Colomar
2023-01-24 18:00           ` Alex Colomar
2023-01-24 11:16   ` Rich Felker
2023-01-24 16:53     ` Alex Colomar

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5dbf06be-e083-4b39-343d-2507802b9881@gmail.com \
    --to=alx.manpages@gmail.com \
    --cc=gcc@gcc.gnu.org \
    --cc=igor@sysoev.ru \
    --cc=libc-alpha@sourceware.org \
    --cc=linux-man@vger.kernel.org \
    --cc=richard.guenther@gmail.com \
    --cc=rouca@debian.org \
    --cc=stefan.puiu@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).