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

Tomas Hajny XHajT03 at hajny.biz
Sun Jul 2 22:09:52 CEST 2017


On Sun, July 2, 2017 18:39, Michael Van Canneyt wrote:
> On Sun, 2 Jul 2017, Tomas Hajny wrote:
>
>>> By declaring it as a File of Enum, you are telling the compiler that it
>>> contains only valid enums.
>>
>> Noone can ever ensure, that a file doesn't get corrupted / tampered with
>> on a storage medium.
>
> No-one can ensure a memory location cannot get corrupted either.

I don't think this is true. The operating system should ensure that no
other process corrupts memory location exclusively used by my program, and
I should make sure that my own program doesn't corrupt it itself.

File is usually not protected to be exclusively used by your own program,
unless it's created by the same program in a locked state and later read
again (still locked) during the same run of that program - let's say that
this pattern isn't a typical use of files, right?


>> Moreover, using the same Read for reading from a text file _does_
>> perform such checks (e.g. when using Read for reading an integer from
>> a text file, the value read is validated whether it conforms the given
>> type and potential failures are signalized either as an RTE, or
>> a non-zero IOResult depending on the $I state).
>
> Text files by definition are not type safe. The compiler cannot know what
> it contains.

I don't talk about the compiler, but about the RTL here.


> By using file of enum (or any data type), you are explicitly telling the
> compiler it is OK.

There isn't much difference between telling the compiler that all values
in certain file are of certain type and telling the compiler that the next
value read from that file (text in this case) will conform to certain
type. Both is typed, both should provide means for ensuring type safety
while loading the value to the variable of the given type.

Note that I don't talk about typecasting here, of course, that's something
completely different (and manual checking is absolutely appropriate
there).

Tomas





More information about the fpc-devel mailing list