[fpc-devel] {$mode tp} compatibility issue

Klaus Hartnegg hartnegg at gmx.de
Tue Jun 5 14:09:16 CEST 2018


I just stepped into a trap when compiling Turbo Pascal source with 
FreePascal, and wonder how FreePascal could prevent this.

The source effectively contains something like

   mytype = (a,b,c);

   t : mytype;
   f : text;

   assign (f,'filename');
   reset (f);
   readln (f,byte(t));

Except that in the real thing the type declaration was in another unit,
but that should not matter, right?

Well, sometimes I got t > high(mytype), eventhough the file contents 
guaranteed that this could not happen. It took me a while until I found 
the cause:

The last statement can only be compiled with {$mode tp}, so I had added 
that. But the unit, where mytype was declared, did not have {$mode tp}. 
Thus sizeof(t)=4, which does not resonate well with byte(t). The read 
statement modifies only one of the four bytes. In Turbo Pascal such 
variables have only one byte.

Miracle solved, but somehow this should not happen.

Could FreePascal check that variables like "mytype = (a,b,c)" have size 
1 before using them in {$mode tp}? Otherwise they should be flaged as 
incompatible type, to prevent such surprises.


More information about the fpc-devel mailing list