Warning flags for unsigned operations (unsafe)

Mathieu Malaterre mmalater@nycap.rr.com
Thu Sep 23 01:37:00 GMT 2004


Luca Masini wrote:
> On Wed, 22 Sep 2004 14:36:36 +0100, Dave Korn <dk@artimi.com> wrote:
> 
>>> I have been googling around and I couldn't find out if
>>> gcc had a warning flag for unsigned operation. For example, even the 
>>> linear
>>> interpolation on [a,b] can be tricky to code:
>>>
>>> 1. c = a + t * (b - a);  //unsafe
>>> 2. c = (1.0 - t) * a + t * b; //safe
>>>
>>> Number 1 will fail when both a and b are unsigned and let say
>>> b - a = -1
>>> (math speaking). Is there something in gcc that could warn me
>>> for this kind of operation ?
>>
>>
>>
>> Your code has a design flaw and is not valid.  If you want to do maths
>> that involves negative quantities, you HAVE to use a signed variable, 
>> not an unsigned one.  If you want to do subtraction with unsigned 
>> quantities and
>> have it work, you have to ensure (by a test) to always subtract the 
>> smaller from the larger.
> 
> 
> I'm curious too, and interested on the original question.
> That is:
>   GCC has the warning for that or not ?

I didn't find them if they exist...
Also as mention it doesn't seems straighforward to implement, to make 
thing worse my code is in fact a template code (I am reading image of 
certain type then do a linear interpolation). So the code could be half 
good / half bad.

I'll try to find out if there is another way to track those unsafe 
operations.

Thanks anyway
Mathieu



More information about the Gcc mailing list