↑
logo

Attention people who think UB is fine

Posted by signa11 |6 hours ago |14 comments

omoikane 4 hours ago[1 more]

The first example should have changed the heading to "C++ code" as opposed to "C code" due to the use of `std::unreachable()`. This matches the godbolt link.

Another alternative is to use `__builtin_unreachable()` (not standard, but available in GCC and Clang).

charleslmunger 4 hours ago

This is sort of true, except that behavior can be undefined in the standard but defined in your implementation. One of the best examples is -fwrapv, which makes signed integer overflow not UB. And Fil-C has no undefined behavior that I'm aware of. But most implementations target the standard and "define" what the standard doesn't to be "whatever I feel like, in service of implementation simplicity and performance". What matters is what your implementation guarantees, which should be a superset of what the standard does.

Vaushite 3 hours ago[1 more]

"the definition of std::unreachable is: invokes undefined behaviour" does not mean that all undefined behaviour is equivalent to calling std::unreachable.

You saw it yourself: unreachable generates no code and suppresses the function epilog (duh - it's unreachable) but null deref generated a trap instruction in GCC. If you wanted a trap instruction, maybe this compiler reliably generates one from a hardcoded null deref.

pestatije 4 hours ago

UB - undefined behaviour

johnea 5 hours ago[4 more]

Isn't a solution to simply not use unreachable()?