[fpc-devel] bug: Inc(v,i)/Dec(v,i)

Marcel Martin mm10 at ellipsa.net
Thu Jul 7 00:36:17 CEST 2005


C Western a écrit :
> Marco van de Voort wrote:
> 
>>>>> var
>>>>>  v : cardinal;
>>>>> begin
>>>>>  v:=100;
>>>>>  inc(v,-1);
>>>>> end.
>>>>>
>>>>> The reason is that -1 is in fact an addition of $ffffffff resulting 
>>>>> in the
>>>>> carry flag being set and therefor an overflow.
>>>>
>>>>
>>>> For cardinal IMHO not-a-bug?
>>>
>>>
>>> I think it's a bug for cardinals and other unsigned ints also (as 
>>> long as the
>>> result is in the range of the result variable)
>>
>>
>>
>> -1 is not a valid cardinal -> out of range -> rangecheck error.
>>
> But on the other hand Dec(v,1) is fine, so Inc(v,-1) should also work.

And if Dec(v,1) raises an overflow? Should Inc(v,-1) also do?
If v = 0 (v is a longword), Dec(v,1) = overflow. So, Inc(v,-1),
i.e., 0 + $ffffffff = overflow?

I think Marco is right. Basically, when the compiler has to
code Inc(x,SomeConstant), the only sensible behaviour it is
expected to have is to regard the constant as being of the
type of x. So, if x is a Longword and if SomeConstant equals
-1, either it adds $ffffffff (and there will be an overflow
if x > 0) or it stops at compile time saying that -1 is not
a Longword.

mm




More information about the fpc-devel mailing list