logoalt Hacker News

titzertoday at 3:15 PM3 repliesview on HN

> The compiler is allowed to transform your code if it can prove that the result would interact with the outside world in exactly the same way

Well, in C/C++, as soon as your program has one UB bug, the compiler has absolutely no obligation whatsoever.


Replies

mpynetoday at 3:27 PM

Well yes, that's what UB means. It's a singularity, you run into it and there's no longer a specified requirement for the behavior that will follow.

Rust also has UB, btw, https://doc.rust-lang.org/reference/behavior-considered-unde..., so I don't know where it is that people have imagined this is something the C and C++ language designers went out of their way to foist upon you.

If you want to write code for a VAX, then you can use the K&R C compiler where it had defined outcomes for everything. If you want to write portable C code for modern CPUs then it's fair to ask what the C language standard is supposed to define for each of those CPUs and OSes and ABIs.

And a bunch of people were nice enough to do that for you and I, but because they are not deities, there are things that they had to leave out to make the language useful, so they did.

show 2 replies
tialaramextoday at 3:33 PM

If you don't want UB to mean "Absolutely anything might happen" which necessarily has to include "... forever" then you need Fil-C or similar runtime handling so that any time its behaviour would become undefined the program exits instead.

To some extent in C and even more in C++ there's a much worse problem, IFNDR [Ill-formed No Diagnostic Required]. Programs which the language specification insists mean nothing at all, but your tools won't (in many cases can't) notice so the result might do anything. It's not Undefined Behaviour, your program never had any defined behaviour at all.

show 1 reply
ueckertoday at 3:26 PM

This is not quite correct in C. ISO C at least requires that observable behavior until this point is preserved.

show 2 replies