logoalt Hacker News

ux266478today at 12:59 AM2 repliesview on HN

> Code that invokes UB is incorrect, full stop.

That's not true at all, who taught you that? Think of it like this, signed integer over/underflow is UB. All addition operations over ints are potentially invoking UB.

   int add (int a, int b) { return a + b; }
So this is incorrect code by that metric, that's clearly absurd.

Compilers explicitly provide you the means to disable optimizations in a granular way over undefined behavior precisely because a lot of useful behavior is undefined, but compilation units are sometimes too complex to reason about how the compiler will mangle it. -fno-strict-aliasing doesn't suddenly make pointer aliasing defined behavior.

We have compiler behavior for incorrect code, and it's refusing to compile the code in the first place. Do you think it just a quirky oversight that UB triggers a warning at most? The entire point of compilers having free reign over UB was so they could implement platform-specific optimizations in its place. UB isn't arbitrary.


Replies

mirashiitoday at 4:03 AM

> -fno-strict-aliasing doesn't suddenly make pointer aliasing defined behavior.

No, it just protects you from a valid but unexpected optimization to your incorrect code. It's even spelled out clearly in the docs: https://www.gnu.org/software/c-intro-and-ref/manual/html_nod...

"Code that misbehaves when optimized following these rules is, by definition, incorrect C code."

> We have compiler behavior for incorrect code, and it's refusing to compile the code in the first place

This isn't and will never be true in C because whether code is correct can be a runtime property. That add function defined above isn't incorrect on its own, but when combined with code that at runtime calls it with values that overflows, is incorrect.

mirashiitoday at 3:45 AM

> All addition operations over ints are potentially invoking UB.

Potentially invoking and invoking are not the same.