News for hackers. Stuff that matters.
Re:New things in clang land (5.0.0) by MaxBarraclough on Thursday December 07, 2017 @10:47AM (#15871039)
> maybe don't allow the undefined behavior in the first place?

So your problem is with C and C++, not Clang?

> I think if I had something like this accidentally in my code then I would want it to tell me that something's wrong instead of just giving bizarre results.

Then you misunderstand the problem.

Compilers don't seek out UB and then generate zany instruction sequences to spite the programmer. Instead, they assume there is no UB in the program, and optimise accordingly. If the code is bad and contains UB, all bets are off.

When I say 'all bets are off', I really mean it. Bizarre things happen in practice, legal by virtue of UB. I've read tales of bad C code printing that x is 0, and then evaluating that (0 == x) is false. (This was due to the compiler taking advantage of an 'is zero?' flag in the underlying CPU, which didn't perfectly align with the bits that stored the value of x.)

A good blog post on UB: https://blog.regehr.org/archives/213 To paraphrase:

> One might ask: Wouldn’t it be great if the compiler provided a warning or something when it detected [the UB]? Sure! But that is not the compiler’s priority.

This approach is part of what makes C, C. Very few other modern languages share this approach (the ones that do tend to be C based), and instead set out to have no UB. Java and C#, for instance, have no concept of UB.

It's impossible, even in theoretical terms, to build a perfect UB-detection program. This follows directly from Rice's theorem. The best we can do is better-than-nothing static-analysis tools, and dynamic-analysis tools. Neither can be perfect and exhaustive.

The good news is that these tools can much better than nothing, even if we know they're always going to be imperfect.

There are no comments on this comment yet.