https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114700
--- Comment #16 from Jakub Jelinek <jakub at gcc dot gnu.org> --- (In reply to Hu Lin from comment #11) > I think it doesn't mean that's not a bug with -ftrapv, it should preserve > all overflow traps. Because it doesn't work, we use -fsanitize=undefined > instead of it. > > refer: Gcc's trapv is known not always to work correctly. No, -ftrapv isn't a debugging tool. There is no overflow in the expression that GCC actually evaluates (into which the expression has been optimized). If you have overflow in an expression that is never used, GCC with -ftrapv will also eliminate it as unused and won't diagnose the trap. -fsanitize=undefined behaves in that case actually the same with -O1 and higher (intentionally, to decrease the cost of the sanitization). So, one needs to use -O0 -fsanitize=undefined to get as many cases of UB in the program diagnosed as possible.