Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> But from my own experience, UB just means "consult your compiler to see what it does here because this question is beyond our pay grade."

People are taught it’s very bad because otherwise they do exactly this, which is the problem. What does your compiler do here may change from invocation to invocation, due to seemingly unrelated flags, small perturbations in unrelated code, or many other things. This approach encourages accepting UB in your program. Code that invokes UB is incorrect, full stop.





I understand, but you have to see how you would be considered one of the Standards-Purists that I was talking about, right? If Microsoft makes a guarantee in their documentation about some behavior of UB C code, and this guarantee is dated to about 14 years ago, and I see many credible people on the internet confirming that this behavior does happen and still happens, and these comments are scattered throughout those past 14 years, I think it's safe to say I can rely on that behavior, as long as I'm okay with a little vendor lock-in.

> If Microsoft makes a guarantee in their documentation about some behavior of UB C code

But do they? Where?

More likely, you mean that a particular compiler may say "while the standard says this is UB, it is not UB in this compiler". That's something wholly different, because you're no longer invoking UB.


Yes, that is still undefined behavior. Behavior being defined or not is a standards-level distinction, not a compiler one.

> 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.


> -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.


Those are the docs for a compiler, not the language standard.

> All addition operations over ints are potentially invoking UB.

Potentially invoking and invoking are not the same.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: