https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102303
Bug ID: 102303 Summary: optimizer elides -ftrapv overflow detection Product: gcc Version: 11.2.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: john at mcfarlane dot name Target Milestone: --- With flag, `-ftrapv`, the following program on CE with 'x86-64 gcc 11.2' has exit status 139: int main() { int n = 0x7fffffff; return n+1; } This is expected behavior: run-time overflow trapping. But with flags, `-ftrapv -O1`, it has exit status 0. I wouldn't expect optimization to change the behavior of this program, which appears to be either UB or as-if `-fwrapv`. https://godbolt.org/z/jvj9javcY Behavior is inconsistent with Clang 12.0. Behavior is inconsistent with `-fsanitize=signed-integer-overflow -fsanitize-undefined-trap-on-error`. Inconsistency goes as far back as the 5.x series, where trapping of this overflow first appears. Docs for -ftrapv (https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html) don't mention optimization.