[Bug c/103502] New: -Wstrict-aliasing=3 doesn't warn on what is documented as UB
stsp at users dot sourceforge.net
gcc-bugzilla@gcc.gnu.org
Tue Nov 30 17:22:41 GMT 2021
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103502
Bug ID: 103502
Summary: -Wstrict-aliasing=3 doesn't warn on what is documented
as UB
Product: gcc
Version: 11.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: stsp at users dot sourceforge.net
Target Milestone: ---
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html
---
Similarly, access by taking the address, casting the resulting pointer and
dereferencing the result has undefined behavior, even if the cast uses a union
type, e.g.:
int f() {
double d = 3.0;
return ((union a_union *) &d)->i;
}
---
Here is the test-case:
---
union a_union {
int i;
double d;
};
static int f() {
double d = 3.0;
return ((union a_union *) &d)->i;
}
int main()
{
return f();
}
---
It only warns at -Wstrict-aliasing=2 or 1,
but doesn't on 3. 3 is documented as the
most precise option. So obviously it should
warn on what the official gcc manual declares
as an UB.
Note: I very much wish such construct to
not be UB. Because of the lack of a warning
on -Wstrict-aliasing=3 I was freely using
such construct for type-punning for years...
until now I've found it invalid in gcc manual.
If it is actually valid, please fix the docs,
not gcc! :)
More information about the Gcc-bugs
mailing list