by kyllo · 7y ago · 0 comments
Undefined behavior means doing something the compiler spec does not specify, therefore the compiler is free to do anything. There are plenty of programs that exploit the programmer's knowledge of a particular compiler's actual, unspecified behavior as an optimization.
One example is using an int variable after an overflow. Post-overflow, the int's value is undefined behavior, but in practice it is predictable for a given compiler and platform. It is possible to write a program that implements its own program spec correctly, while exploiting out-of-spec behavior of its compiler under the hood.
Much like how it's possible to wrap unsafe Rust code with a safe interface.
Unsafe code blocks still have defined and predictable behavior, but you have to guarantee the memory safety.
There are plenty of programs that have bugs.
Programmers who exploit undefined behavior on purpose are reckless.
And then they break when the compiler changes its behavior…