[fpc-pascal] Range checks

Jonas Maebe jonas at freepascal.org
Sat Jan 27 17:28:31 CET 2018


Jonas Maebe wrote:
> C Western wrote:
>> The following innocuous looking code generates a range check error:
>>
>> {$R+}
>> function Count: qword;
>> begin
>>   Result := 0;
>> end;
>> var
>>   i: Integer;
>> begin
>>   for i := 0 to Count-1 do
>>     WriteLn(i);
>> end.
>>
>> I can (more or less) see why
> I changed the type used to evaluate for-loop bounds in ISO Pascal mode
> because of https://bugs.freepascal.org/view.php?id=24318 . Maybe the
> same should be done for other non-TP/Delphi modes too.

Actually, it won't help because "qword - 1" will still be evaluated as
qword. The issue is that there is no safe way to evaluate this with
range checking that is correct in all cases without a 128 bit integer type:
1) if you evaluate it as qword, you get a range error if count is 0 (as
above)
2) if you evaluate it as int64, then if count is high(int64)+1 you will
get a range error even though the result could be represented in int64

In this particular case, because the counter is 32 bit (which seems
quite unsafe for use with a container whose "count" property is 64 bit),
using int64 would be "correct", but it's not a systemic solution (as
shown above) and hence not something to implement in the language.


Jonas


Jonas



More information about the fpc-pascal mailing list