From: "Zack Weinberg" <zack@owlfolio.org>
To: "'Alejandro Colomar (man-pages)'" <alx.manpages@gmail.com>,
"GNU libc development" <libc-alpha@sourceware.org>
Cc: "Alejandro Colomar" <alx@kernel.org>,
'linux-man' <linux-man@vger.kernel.org>,
"Bastien Roucariès" <rouca@debian.org>,
"Eric Blake" <eblake@redhat.com>,
"Stefan Puiu" <stefan.puiu@gmail.com>,
"Igor Sysoev" <igor@sysoev.ru>
Subject: Re: [PATCH v2] socket: Implement sockaddr_storage with an anonymous union
Date: Fri, 20 Jan 2023 13:04:26 -0500 [thread overview]
Message-ID: <d77b529d-e54d-4919-87a4-d90fd816ba8b@app.fastmail.com> (raw)
In-Reply-To: <20230120134043.10247-1-alx@kernel.org>
On Fri, Jan 20, 2023, at 8:40 AM, Alejandro Colomar wrote:
> The historical design of `sockaddr_storage` makes it impossible to use
> without breaking strict aliasing rules. Not only this type is unusable,
> but even the use of other `sockaddr_*` structures directly (when the
> programmer only cares about a single address family) is also incorrect,
> since at some point the structure will be accessed as a `sockaddr`, and
> that breaks strict aliasing rules too.
>
> So, the only way for a programmer to not invoke Undefined Behavior is to
> declare a union that includes `sockaddr` and any `sockaddr_*` structures
> that are of interest, which allows later accessing as either the correct
> structure or plain `sockaddr` for the sa_family.
...
> struct new_sockaddr_storage nss;
>
> // ... (initialize oss and nss)
>
> inet_sockaddr2str(&nss.sa); // correct (and has no casts)
I think we need to move slowly here and be _sure_ that any proposed change
does in fact reduce the amount of UB. This construct, in particular, might
not actually be correct in practice: see https://godbolt.org/z/rn51cracn for
a case where, if I'm reading it right, the compiler assumes that a write
through a `struct fancy *` cannot alter the values accessible through a
`struct simple *` even though both pointers point into the same union.
(Test case provided by <https://stackoverflow.com/users/363751/supercat>;
I don't know any other identifier for them.)
zw
next prev parent reply other threads:[~2023-01-20 18:04 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-01-20 13:40 Alejandro Colomar
2023-01-20 17:49 ` Joseph Myers
2023-01-20 19:26 ` Alejandro Colomar
2023-01-20 18:04 ` Zack Weinberg [this message]
2023-01-20 19:25 ` Alejandro Colomar
2023-01-21 2:38 ` Alejandro Colomar
2023-01-21 3:17 ` Alejandro Colomar
2023-01-21 13:30 ` Bastien Roucariès
2023-01-21 14:30 ` Alejandro Colomar
2023-01-22 14:12 ` Bastien Roucariès
2023-01-20 20:32 ` Bastien Roucariès
2023-01-20 20:38 ` Alejandro Colomar
2023-01-20 20:46 ` Bastien Roucariès
2023-01-20 20:51 ` Alejandro 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=d77b529d-e54d-4919-87a4-d90fd816ba8b@app.fastmail.com \
--to=zack@owlfolio.org \
--cc=alx.manpages@gmail.com \
--cc=alx@kernel.org \
--cc=eblake@redhat.com \
--cc=igor@sysoev.ru \
--cc=libc-alpha@sourceware.org \
--cc=linux-man@vger.kernel.org \
--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).