[fpc-devel] Dangerous optimization in CASE..OF

Yury Sidorov jura at cp-lab.com
Sun Jul 2 11:59:29 CEST 2017


On 7/2/2017 11:19 AM, Florian Klämpfl wrote:
> Am 02.07.2017 um 00:26 schrieb Martok:
>> So, we have a problem here: either the type system is broken because we can put
>> stuff in a type without being able to check if it actually belongs there, or
>> Tcgcasenode is broken because it (and _only_ it, as far as I can see) wants to
>> be clever by omitting an essentially free check for very little benefit.
>> I know which interpretation I would choose: the one with the easier fix ;-)
> 
> Yes, checking the data. I can easily create a similar problem as above with the "range checks" for
> the jump table by reading a negative value into the enum. Unfortunately, the checks are unsigned ...
> 
> The correct solution is to provide a function which checks an integer based on rtti if it is valid
> for a certain enum. Everything else is curing only symptoms.

Indeed, I've done some tests and found out that when range checking is enabled enums are not checked at all. Even array 
access with enum index is not checked.
According to docs enums should be range checked:
https://www.freepascal.org/docs-html/prog/progsu65.html#x72-710001.2.65

As Florian has said, the correct solution for this issue is to add range checking for enum types when range checking is 
ON. Including the "CASE <enum> OF". The check via RTTI should be fine.
At least you will be able to generate slower but safe code by enabling range checks and overflow checks.

Yury.



More information about the fpc-devel mailing list