News for hackers. Stuff that matters.
Re:Undefined Behavior in 2017 by nikanj on Wednesday July 05, 2017 @04:14AM (#14700692)
I wish there was a GCC switch that made the optimizer go "This is clearly undefined behaviour. I'll stop this compile right here", as opposed to "This is clearly undefined behaviour. Sweet jackpot, it means I can do whatever the heck I want. Drop those NULL checks below, I've got the golden UDB ticket now"
Re:Undefined Behavior in 2017 by dom0 on Wednesday July 05, 2017 @04:23AM (#14700729)
The optimizer doesn't work that way. It assumes the code does not do UB and then removes branches that can never be taken if the code executes no UB.

That's different from "This is UB, and now I do ...".

Re:Undefined Behavior in 2017 by cousin_it on Wednesday July 05, 2017 @05:18AM (#14700947)
I think we deserve a compiler warning whenever a source line of code (not a line generated by the preprocessor) gets removed due to UB exploitation. All such cases are worth the programmer's attention, because they are either useless lines or bugs.
Re:Undefined Behavior in 2017 by quantdev on Wednesday July 05, 2017 @05:54AM (#14701092)
It's more complicated than this, unfortunately. Imagine an int variable x and two expressions f(x) and g(x) of x. Now imagine an if statement with condition f(x) < g(x). Precisely because incrementing the largest int is undefined behavior, the compiler may be able to simplify the if condition because, for example, it can assume x+1 > x is always true. So it's not necessary for the line to be useless or wrong for undefined behavior rules to trigger compiler optimizations.
Re:Undefined Behavior in 2017 by caf on Wednesday July 05, 2017 @06:26AM (#14701234)
You must also account for the case where the source line removed was only useless because of the specific context it had been inlined to by an earlier stage of the optimiser.
Re:Undefined Behavior in 2017 by pornel on Wednesday July 05, 2017 @08:08AM (#14701717)
It's not as easy as it seems.

In C `x + 1` is an undefined behavior if `x` is signed and overflows. Optimizer takes advantage of that for loop optimizations.