[cfe-dev] Possible clang bug with -O2, wrong if branch entered, in clang version 2.1 (tags/Apple/clang-163.7.1)

Salvatore Sanfilippo antirez at gmail.com
Tue Feb 21 08:43:48 PST 2012

On Tue, Feb 21, 2012 at 4:57 PM, Dmitri Gribenko <gribozavr at gmail.com> wrote:

> Hi Salvatore,
> If I understand this code correctly, there is a signed overflow.
> Signed overflow is undefined behavior.  Clang (or LLVM) apparently
> takes advantage of that by optimizing (incr < 0 && value > oldvalue)
> => false.

I Dmitri,

I understand the code is undefined, but a compiler can decide to have
different practical behaviors when an undefined condition is
encountered. I think that the best way to handle this condition in the
real world is to do the math accordingly to the underlaying
architecture, so that just the result is undefined, but avoiding to
take advantage of the fact it is undefined to optimized.

I'm not stating that clang is behavior is no correct: strictly
speaking it is following the standard. However its concrete behavior
probably does not minimize the probability of real-world (possibly
broken) code fails. That said I'm going to fix my code so that it is
standard complaint and can compile fine with clang.

Thank you,

> Dmitri
> --
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/

Salvatore 'antirez' Sanfilippo
open source developer - VMware

"We are what we repeatedly do. Excellence, therefore, is not an act,
but a habit." -- Aristotele

More information about the cfe-dev mailing list