------- Comment #13 from [EMAIL PROTECTED]  2008-11-24 13:41 -------
(In reply to comment #12)
> (In reply to comment #10)
> > c += 'A' - 'a';
> > 
> > Casting seems too strict a requirement in these types of situations.  I 
> > can't
> > imagine that anyone has a positive experience with these warnings, most are
> > just going to grumble, then insert the cast without thinking about it.
> Notice that in the particular example you mention, the code does go through
> because it uses +=.

Wow, that surprises me.

c = c + c should be equivalent to c += c;

So right there, either both should be invalid, or neither should.  Both have an
equal chance of overflow.

In fact, using obj2asm, I found that both are essentially equivalent.  With
optimization on, here are the two different code generations (minus all
bookkeeping stuff):

char foo(char c)
   c += c; // version 1
   c = c + c; // version 2
   return c;

c += c;
<               mov     ECX,EAX
<               add     CL,CL
<               mov     AL,CL

c = c + c;
>               push    EAX
>               movzx   ECX,byte ptr -4[EBP]
>               mov     EAX,ECX
>               add     AL,AL

These are essentially the same, except in the second version, the compiler
optimizer didn't see the oportunity to get down to the same code as the first
version (there's probably even more optimization to be had even in the first
version).  But the danger of overflow still exists in both cases.  I'm not sure
why one is treated more dangerously than the other by the compiler.


Reply via email to