public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] sockaddr.3type: BUGS: Document that libc should be fixed using a union
@ 2023-02-05 15:28 Alejandro Colomar
  2023-02-05 15:31 ` Alejandro Colomar
                   ` (2 more replies)
  0 siblings, 3 replies; 16+ messages in thread
From: Alejandro Colomar @ 2023-02-05 15:28 UTC (permalink / raw)
  To: linux-man
  Cc: Alejandro Colomar, GCC, glibc, Bastien Roucariès,
	Stefan Puiu, Igor Sysoev, Rich Felker, Andrew Clayton,
	Richard Biener, Zack Weinberg, Florian Weimer, Joseph Myers,
	Jakub Jelinek, Eric Blake

As discussed before, and Bastien and I seem to agree, ideally we should
define the following types:

    struct sockaddr_storage {
        union {
            struct {
                sa_family_t      ss_family;
            };
            struct sockaddr_in   sin;
            struct sockaddr_in6  sin6;
            struct sockaddr_un   sun;
            // ...
        };
    };

    struct [[deprecated]] sockaddr {
        sa_family_t              sa_family;
    };

    union [[gnu::transparent_union]] sockaddr_ptr {
        struct sockaddr_storage  *ss;
        struct sockaddr          *sa;
    };

And then we could define APIs like:

    int bind(int sockfd, const union sockaddr_ptr *addr, socklen_t len);

Link: <https://lore.kernel.org/linux-man/ab492040-2058-bcbe-c920-a9088a20f071@gmail.com/T/#u>
Link: <https://inbox.sourceware.org/libc-alpha/20230120134043.10247-1-alx@kernel.org/T/#u>
Cc: GCC <gcc@gcc.gnu.org>
Cc: glibc <libc-alpha@sourceware.org>
Cc: Bastien Roucariès <rouca@debian.org>
Cc: Stefan Puiu <stefan.puiu@gmail.com>
Cc: Igor Sysoev <igor@sysoev.ru>
Cc: Rich Felker <dalias@libc.org>
Cc: Andrew Clayton <a.clayton@nginx.com>
Cc: Richard Biener <richard.guenther@gmail.com>
Cc: Zack Weinberg <zack@owlfolio.org>
Cc: Florian Weimer <fweimer@redhat.com>
Cc: Joseph Myers <joseph@codesourcery.com>
Cc: Jakub Jelinek <jakub@redhat.com>
Cc: Eric Blake <eblake@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
---
 man3type/sockaddr.3type | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/man3type/sockaddr.3type b/man3type/sockaddr.3type
index 319a5e552..239e836fc 100644
--- a/man3type/sockaddr.3type
+++ b/man3type/sockaddr.3type
@@ -120,6 +120,26 @@ .SH NOTES
 .I <netinet/in.h>
 and
 .IR <sys/un.h> .
+.SH BUGS
+.I sockaddr_storage
+was designed back when strict aliasing wasn't a problem.
+Back then,
+one would define a variable of that type,
+and then access it as any of the other
+.IR sockaddr_ *
+types,
+depending on the value of the first member.
+This is Undefined Behavior.
+However, there is no way to use these APIs without invoking Unedfined Behavior,
+either in the user program or in libc,
+so it is still recommended to use this method.
+The only correct way to use different types in an API is through a union.
+However,
+that union must be implemented in the library,
+since the type must be shared between the library and user code,
+so libc should be fixed by implementing
+.I sockaddr_storage
+as a union.
 .SH SEE ALSO
 .BR accept (2),
 .BR bind (2),
-- 
2.39.1


^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2023-03-20 10:49 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-05 15:28 [PATCH] sockaddr.3type: BUGS: Document that libc should be fixed using a union Alejandro Colomar
2023-02-05 15:31 ` Alejandro Colomar
2023-02-06  6:02   ` Xi Ruoyao
2023-02-06 11:20     ` Rich Felker
2023-02-06 11:55     ` Alejandro Colomar
2023-02-06 13:38       ` Rich Felker
2023-02-06 14:11         ` Alejandro Colomar
2023-02-06 17:21           ` Zack Weinberg
2023-02-06 17:48           ` Rich Felker
2023-02-05 23:43 ` Rich Felker
2023-02-05 23:59   ` Alejandro Colomar
2023-02-06  0:15     ` Rich Felker
2023-02-06 18:45 ` Eric Blake
2023-02-07  1:21   ` Alejandro Colomar
2023-03-18  7:54   ` roucaries bastien
2023-03-20 10:49     ` Alejandro Colomar

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).