News for hackers. Stuff that matters.
Re:How undefined signed overflow enables optimizations in GCC by to3m on Sunday February 21, 2016 @09:20PM (#11147977)
Yes. People are quick to point to the standard when it comes to compilers doing something surprising, and quick to use this as evidence that programmers are silly to expect the expected. But if compilers did exactly what these programmers expect, they could point to the standard just the same...

(At least this article provides a good explanation of advantages to be had from being aggressively pedantic about UB. Many by way of evidence just wave their hands and claim people are stupid.)

Re:How undefined signed overflow enables optimizations in GCC by username223 on Sunday February 21, 2016 @10:45PM (#11148293)
> (At least this article provides a good explanation of advantages to be had from being aggressively pedantic about UB. Many by way of evidence just wave their hands and claim people are stupid.)

This. While it doesn't show how much real programs benefit from these UB assumptions (or how many new security bugs are introduced...), at least it has some explanations. Still, you have to ask yourself whether the compiler is a tool to help you do your work, or a perverse, pedantic, and frustrating adversary. The ultimate "UB optimizer" would do its best to find tiny corners of undefined behavior in your program, then replace the whole thing with "exit(0)".

Re:How undefined signed overflow enables optimizations in GCC by eru on Monday February 22, 2016 @06:50PM (#11155322)
That would be an interesting exercises. Of course, the whole thing is only interesting if it comes with a proof of justification. Otherwise it's too easy for real world C programs: they are all undefined.
Re:How undefined signed overflow enables optimizations in GCC by stouset on Monday February 22, 2016 @01:09AM (#11148884)
If you don't go by the spec, what do you go by? Intuition? What if yours is different from someone else's?

In this case as well, acknowledging that signed integer overflow is undefined behavior allows for more consistent behavior in certain circumstances. For instance, the compiler knows that `x < x + c`, where `c > 0`, and optimize it to `true`. Even if you don't care about the optimization, is it not equally surprising that such a statement could ever be `false`?

No matter which way you go, some behavior is going to be surprising. And in these scenarios, the only plausible thing to do so is to follow the standard, so at least there's a consistent ideal that everyone's intuitions can converge to, which is crucially important. Each compiler implementing a different C-alike standard just leads to more confusion.

Re:How undefined signed overflow enables optimizations in GCC by userbinator on Monday February 22, 2016 @01:48AM (#11149010)
If you don't go by the spec, what do you go by?

To quote the standard, "a documented manner characteristic of the environment."

If the machine is two's complement (which is the vast majority of them), then you'd expect that behaviour. If it's sign-magnitude or one's complement or saturating, then the behaviour would be different but still consistent.

Even if you don't care about the optimization, is it not equally surprising that such a statement could ever be `false`?

Overflows wrapping around is not surprising, it's what decades of computing hardware (or centuries if you include mechanical calculators like https://en.wikipedia.org/wiki/Pascal%27s_Calculator ) have always done.

Re:How undefined signed overflow enables optimizations in GCC by stouset on Friday February 26, 2016 @01:39PM (#11183122)
> If the machine is two's complement (which is the vast majority of them), then you'd expect that behaviour. If it's sign-magnitude or one's complement or saturating, then the behaviour would be different but still consistent.

How is this different in practice from undefined behavior, other than the compiler can't take advantage of it for optimizations?

Re:How undefined signed overflow enables optimizations in GCC by Spivak on Monday February 22, 2016 @01:27AM (#11148951)
Maybe a better way to illustrate the surprising behavior is that

if (x < x + c) { // block }

if (0 < c) { // block }

are not equivalent statements if you add overflow wrapping into the spec. What if the processor your application is running on doesn't do two's compliment arithmetic? Does gcc need to emulate this behavior to comply with the spec?