4 ms·
> it cannot reorder the function call after the division Actually, it can because one of the things undefined behavior can do is go backwards in time. Undefin
by lisper 9y ago
> it cannot reorder the function call after the division
Actually, it can because one of the things undefined behavior can do is go backwards in time.
Undefined behavior anywhere in the program can legally make any part of the program do anything at any time.
On top of that, there is no reliable way to tell if your program contains undefined behavior.
This is madness.
- nickpsecurity 9y agoOn that last point, check out Rosu et al's KCC: an executable, formal semantics of C that addresses undefined behavior. https://github.com/kframework/c-semantics/blob/master/README.md https://github.com/kframework/c-semantics/blob/master/README...
- comex 9y agoSee my response to a sibling comment of yours making a similar point. It can only go back in time to a point where execution of undefined behavior is inevitable; it doesn’t count if the program aborts first: https://news.ycombinator.com/item?id=16917302 https://news.ycombinator.com/item?id=16917302
- jjnoakes 9y ago> Undefined behavior anywhere in the program can legally make any part of the program do anything at any time. This is only true if the undefined behavior is guaranteed to be reachable. This is not true in general. Putting "if (0) 1/0;" in a program does not cause undefined behavior.
- lisper 9y ago> This is only true if the undefined behavior is guaranteed to be reachable. Nope. It's true if the UB is possibly reachable. See: https://blogs.msdn.microsoft.com/oldnewthing/20140627-00/?p=633/ https://blogs.msdn.microsoft.com/oldnewthing/20140627-00/?p=...
- jjnoakes 9y agoThat article agrees with me, not you. Even in the time travel examples, the code paths unrelated to the ub can't be touched. In other words, it can travel backward, but not sideways (which is what you originally claimed).
- lisper 9y ago> Even in the time travel examples, the code paths unrelated to the ub can't be touched. Of course they can. UB licenses the program to do anything including re-write any part of itself.
- jjnoakes 9y agoUB only licenses the program to do anything if it is reachable. Unreachable, unexecuted, cant-be-executed UB simply can't change a program's behavior. I'm not sure why you think it can. Do you think this program can "do anything" or "re-write any part of itself"? unsigned int x = <input from user>; unsigned int y = <input from user>; printf("%u\n", x / y); The compiler must output valid code for that program, and execution (not compilation) can only "do anything" if the user enters values that invoke UB (like "y == 0" for example). For all valid inputs this program must execute correctly and can not "do anything", even at runtime, do you agree? What about this one? unsigned int x = <input from user>; unsigned int y = <input from user>; if (y == 0) { printf("%u\n", x / y); } else { printf("%u\n", x * y); } Same idea: this program can only "do anything" if the user enters "y == 0". The compiler can see that "x / y" would invoke UB, and remove the entire "if (y == 0) { ... }" branch (because the compiler can assume it is never reached, otherwise it would invoke UB), but the compiler can not do anything to change the behavior of the "else" branch - entering any non-zero "y" value must produce correct output, so the program can't just be compiled to "do anything". Only the paths related to the UB can be optimized/modified/rewritten by the compiler based on that UB, and if those optimizations or modifications affect the proper execution of paths unrelated to the UB (i.e. paths where the UB can not be reached from), then the optimization or modification in question is a compiler bug.
- radford-neal 9y agoConsider a program that reads two numbers input by the user, divides them, and prints the result. Can it really be that because it's POSSIBLE for the user to enter zero for the second number, that the compiler is allowed to replace this program by one that removes all your files (even when the user DIDN'T enter zero)? The C standard writers may have been unwise to specify undefined behaviour the way they did, but they weren't THAT crazy!