Post

Attention people who think UB is fine

A compact compiler example shows why undefined behavior in C/C++ is more dangerous than “it will probably crash”: once the program reaches UB, the compiler may make transformations that produce surprising effects elsewhere.

The example uses std::unreachable() after a volatile device write; GCC emits code that appears to write a second value in a function that is never called. A null-pointer variant writes zero before trapping, rather than the requested value. The Lobsters discussion stressed that UB differs from implementation-defined behavior, while debating how standards, POSIX assumptions, sanitizers, warnings, and static analysis can help maintainers manage existing C code.