[Bug c/69612] Optimizer does not consider overflow
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69612 Jakub Jelinek changed: What|Removed |Added Status|UNCONFIRMED |RESOLVED CC||jakub at gcc dot gnu.org Resolution|--- |INVALID --- Comment #1 from Jakub Jelinek --- This program triggers undefined behavior. You can e.g. use -fsanitize=undefined to see it diagnosed at runtime. Once undefined behavior is reached, anything can happen.
[Bug c/69612] Optimizer does not consider overflow
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69612 --- Comment #2 from Roar Lauritzsen --- Thanks a lot for the quick analysis. Now that I know what it is I can fix my program, and the -fsanitize=undefined will come in handy for localizing problem areas. For future googlers, I am planning to fix my program along these lines: unsigned test2(unsigned a) { if ((int)a < 0) return 1; a++; // 0x7FFF + 1 is not undefined anymore if ((int)a < 0) return 2; return a; } This code executes the way I want also with -O2, and there are no errors from -fsanitize=undefined. Personally, I find it somewhat incongruent that integer overflow is undefined, while integer casting is not, and I also find it very disturbing that optimized and non-optimized code may behave differently.
[Bug c/69612] Optimizer does not consider overflow
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=69612 Manuel López-Ibáñez changed: What|Removed |Added CC||manu at gcc dot gnu.org --- Comment #3 from Manuel López-Ibáñez --- (In reply to Roar Lauritzsen from comment #2) > Thanks a lot for the quick analysis. Now that I know what it is I can fix my > program, and the -fsanitize=undefined will come in handy for localizing > problem areas. For future googlers, I am planning to fix my program along > these lines: Better use https://gcc.gnu.org/onlinedocs/gcc/Integer-Overflow-Builtins.html