| > Amazingly, we find that the GCC compilers is able to compile Travis’ is_empty C++ function to constant-time code. It's actually an interesting example where undefined behavior allowed compiler optimization: (1) dereferencing an invalid pointer is UD (2) signed integer overflow is UD. This allows the compiler to assume that the program never crashes and the counter never overflows. The loop is then optimized out knowing that it is read-only thus has no side-effects. |
That is literally reason why any behavior is considered undefined. So that the compiler can skip checks to produce better optimized code.