[fpc-devel] A bug in FPC 2.4.x for accessing object methods from local functions?
Kostas Michalopoulos
badsectoracula at gmail.com
Fri Jun 3 08:50:04 CEST 2011
Hi all,
I've found a bug that causes FPC 2.4.2/2.4.4 to create invalid code. It
seems to be caused when a method of a packed* object is called from a
local procedure and the object is declared on the procedure above
(although it might be caused in other cases too - i just found it in my
Lazarus program and trimmed it down enough to be usable for a bug report).
Note that this is caused only with -O2 or above enabled and with -O1
works as expected.
(*=in my original program it also happens without the object being
"packed" but i couldn't reproduce that)
Below is an example program that shows the issue:
-------------------------------------------------
program testprog;
{$MODE OBJFPC}{$H+}
type
TThing = packed object
a, b, c: Extended;
procedure Dump;
end;
TTest = class
procedure DoTest;
end;
function Thing(a, b, c: Extended): TThing; inline;
begin
Result.a:=a;
Result.b:=b;
Result.c:=c;
end;
procedure TThing.Dump;
begin
Writeln(a:0:2, ' ', b:0:2, ' ', c:0:2);
end;
procedure TTest.DoTest;
var
Some: TThing;
procedure Add(a, b, c: Extended);
begin
Some.Dump;
end;
begin
Some:=Thing(1, 1, 1);
Some.Dump;
Add(10, 20, 30);
Add(-10, 20, 30);
Add(10, -20, 30);
Add(10, 20, -30);
Some:=Thing(1, 1, 0);
Add(10, 10, 10);
Add(20, 20, 20);
Add(30, 30, 30);
Add(40, 40, 40);
end;
var
Test: TTest;
begin
Test:=TTest.Create;
Test.DoTest;
Test.Free;
end.
-------------------------------------------------
The call of "Some.Dump" from inside "Add" seems to get the wrong values
- or crash. It probably tries to read from invalid memory locations.
The bug exists in both 2.4.2 and 2.4.4. I checked out the code from the
FPC SVN repository (revision 17644) and it seems to compile fine - or at
least produce the expected results. However this might be accidental so
i decided to report it.
Also if anyone knows the reason behind this and how to work around it, i
would like to hear.
Kostas "Bad Sector" Michalopoulos
More information about the fpc-devel
mailing list