gcc 4.4.1 doesn't warn about strict aliasing that it encounters with -Wall -O2
See bug #42907.
That -Wall did not warn about the code that it ended up miscompiling.
It should at least warn about strict aliasing when gcc is going to break stuff.
$ gcc-4.4 -S t.c -O2 -Wall -Wstrict-aliasing=2
t.c: In function ‘main’:
t.c:15: warning: dereferencing type-punned pointer might break strict-aliasing rules
you need to enable level 2 to get warnings about this.
Is this to avoid false positives, because there's only a hazard if the thing is actually dereferenced later on (or if it's cast back to the "correct" type)?
"Is this" as in, "is level two not enabled by default..."
(In reply to comment #3)
> "Is this" as in, "is level two not enabled by default..."
Yes. It's even exactly documented that way.
IMHO, it's not good for strict aliasing warnings on level 2 to be optional and not included in -Wall by default. As it stands right now, gcc does not warn you even if it knows your code is 1) breaking the rules, and 2) the assignments that break the rules are going to be touched by optimizations.
Optimizing away an assignment because that assignment breaks a rule is always a potential danger. Now I know that the reasoning to not show those warnings by default is that it can give you false positives, but a false positive in this case is when you *are* breaking the rules, but an optimization *might* or *might not* break your code. IMHO, just because your code might not break doesn't mean the warning is not needed. A developer really needs to know he's breaking the rules here, regardless of whether things blow up or not.