Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-devel / 2018-06

{$mode tp} compatibility issue

3 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Klaus Hartnegg

Hi,

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

type
   mytype = (a,b,c);
var
   t : mytype;
   f : text;
begin
   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.

thanks,
Klaus

Reply by email

Marco van de Voort
to Klaus Hartnegg

In our previous episode, Klaus Hartnegg said:

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.

Not really, better edit your project to make sure that everything is forced TP if you work exclusively in TP mode.

Reply by email

Mattias Gaertner
to Klaus Hartnegg: I just stepped into a trap when compiling Turbo Pascal source with FreePascal, and wonder…
Show quoted text 21 lines
Hi, 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 type mytype = (a,b,c); var t : mytype; f : text; begin 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?

It does matter. See here:
https://www.freepascal.org/docs-html/prog/progsu59.html#x66-650001.2.59

Mattias

Reply by email