Bug#561691: Please declare struct sockaddr and sockaddr_storage as may_alias
severity 561691 wishlist
tag 561691 + wontfix
On Sat, Dec 19, 2009 at 06:03:48PM +0100, Juliusz Chroboczek wrote:
> Package: libc6-dev
> Version: 2.10.2-2
> Type punning between pointers to struct sockaddr, struct sockaddr_storage
> and sockaddr_in(6) is explicitly required by the sockets interface. For
> example, RFC 3493 Section 3.8 gives the following example:
> if (bind(s, (struct sockaddr *) &sin6, sizeof(sin6)) == -1)
> Unfortunately, gcc-4.4 with -fstrict-aliasing=2 complains on this kind
> of code. This may potentially break in code optimised with -O2 or greater
> (it probably doesn't, though, since most of the code that triggers this
> warning doesn't actually violate the C99 aliasing restrictions).
gcc 4.4 with -Wstrict-aliasing=3 does not emit a warning on that.
Moreover quoting the gcc manual:
| Level 2: Aggressive, quick, not too precise. May still have many false
| positives (not as many as level 1 though), and few false negatives (but
| possibly more than level 1). Unlike level 1, it only warns when an
| address is taken. Warns about incomplete types. Runs in the frontend
| Level 3 (default for -Wstrict-aliasing): Should have very few false
| positives and few false negatives. Slightly slower than levels 1 or 2
| when optimization is enabled. Takes care of the common punn+dereference
| pattern in the frontend: "*(int*)&some_float". If optimization is
| enabled, it also runs in the backend, where it deals with multiple
| statement cases using flow-sensitive points-to information. Only warns
| when the converted pointer is dereferenced. Does not warn about
| incomplete types.
This is clearly what happens here, gcc emits a false positive. You
should use -Wstrict-aliasing=3, which is actually the default value.
There is no risk of breakage in optimised code with -O2 or more.
> I suggest that the definitions of struct sockaddr and struct sockaddr_storage
> should be equipped with
> __attribute__ ((may_alias))
> To UNSUBSCRIBE, email to debian-glibc-REQUEST@lists.debian.org
> with a subject of "unsubscribe". Trouble? Contact email@example.com
Aurelien Jarno GPG: 1024D/F1BCDB73