On Friday, September 21, 2012, Richtarsky, Martin  wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi all,<br>
<br>
a few days ago I ran across code that relied on signed overflow being defined. With gcc this worked fine, clang optimized and the program broke.<br>
<br>
With gcc it is possible to specify -Wstrict-overflow=x to get a warning in cases where the compiler relies on undefined behaviour (which will usually happen only with -O2 or above).<br>
<br>
I tried this with clang, but got no warning (but clang seems to know the option). Am I  doing something wrong?<br>
</blockquote><div><br></div><div>Hi Martin,</div><div><br></div><div>I don't think there is such warning in clang, but you could use the -fwrapv option to define signed integer arithmetic to wrap around on overflow -- this is the easy option.  You could also use the -fcatch-undefined-behavior option to catch signed overflow at runtime and turn it into a crash so that you can debug and fix it.<span></span></div>
<div><br></div><div>Dmitri</div><div> </div><br><br>-- <br>main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if<br>(j){printf("%d\n",i);}}} /*Dmitri Gribenko <<a href="mailto:gribozavr@gmail.com" target="_blank">gribozavr@gmail.com</a>>*/<br>