[fpc-devel] [Suggestion] Enumeration range-check intrinsic

Michael Van Canneyt michael at freepascal.org
Tue Jul 2 19:47:26 CEST 2019



On Tue, 2 Jul 2019, J. Gareth Moreton wrote:

> Hi everyone,
>
> So there have been a lot of issues going back and forth regarding case blocks 
> when dealing with enumerations and what happens if the enum falls outside the 
> valid range of values for some reason, because the internal domain check is 
> sometimes omitted for performance reasons, since the value /shouldn't/ be 
> invalid.  Thus, if it is invalid and a jump table is used, an access 
> violation is raised.
>
> Ultimately, the range check will likely not be included to safeguard against 
> this, but there are still situations where an enumeration will take on an 
> out-of-range value, like if it reads it from an external data file that is 
> corrupted or of an incompatible version for the program.  
> So I would like to 
> suggest a new intrinsic called InRange() or IsDomainValid() or something to 
> that effect that takes an ordinal-type parameter and returns True if it 
> contains a valid value for the enumeration (always True if it's a Byte or 
> Word etc) and False if it is outside the valid domain, hence fulfilling the 
> range check if it's something you really need.


I thought this was already discussed and accepted as desirable ?
If memory serves well, the only discussion was about form...

>
> What are your thoughts? Would this be a decent compromise to fix the case 
> range-check issue that keeps cropping up occasionally? I know it's causing 
> some people to choose not migrate away from Delphi, and that is never really 
> a good thing.  It should be easy enough to document as well, putting in an 
> explicit warning for case to say that if the enumeration is out of range, it 
> may cause a crash, and to prevent it, use the intrinsic to check beforehand.

Why would this stop anyone from migrating away from Delphi ?

Delphi has the exact same problem: an enumerated may be out of range.
I had it on multiple occasions when database data was invalid.

It may not have the case jump table problem, but other than that it is not better
than FPC.

Michael.


More information about the fpc-devel mailing list