[fpc-devel] Dangerous optimization in CASE..OF
Michael Van Canneyt
michael at freepascal.org
Sun Jul 2 10:29:24 CEST 2017
On Sun, 2 Jul 2017, Florian Klämpfl wrote:
>> 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.
GetEnumName from typinfo will already do this for you.
We could add an additional function that just returns true or false.
Something as
function ValueInEnumRange(TypeInfo : PTypeInfo; AValue : Integer) : boolean;
If memory serves correct, this will not work for enums that have explicitly assigned
numerical values, though.
Michael.
More information about the fpc-devel
mailing list