inlined function can stomp its own "Self" value when setting Result
Original Reporter info from Mantis: Hixie
-
Reporter name: Ian Hickson
Original Reporter info from Mantis: Hixie
- Reporter name: Ian Hickson
Description:
The following program prints first a non-zero value, then zero. It should print the same value both times.
{$MODE OBJFPC}
program test;
type
TBaseClass = class
function PrintSelf(): TBaseClass; inline; // has to be inline for the bug to manifest
end;
TSubClass = class(TBaseClass)
end;
function TBaseClass.PrintSelf(): TBaseClass;
begin
Writeln(PtrUInt(Self));
Result := nil;
Writeln(PtrUInt(Self)); // prints 0!
end;
procedure NoOp(var Dummy: TBaseClass);
begin
end;
var
Instance, Variable: TBaseClass;
begin
Instance := TSubClass.Create();
Variable := nil;
NoOp(Variable); // this call is important for the bug to manifest
Variable := Instance;
// object being invoked has to be cast to a different type for the bug to manifest
// return value has to be assigned to the variable being used as "self"
Variable := TSubClass(Variable).PrintSelf();
Instance.Free();
end.
Mantis conversion info:
- Mantis ID: 26536
- OS: Debian
- Build: 2014/07/28
- Platform: x86_64
- Version: 2.7.1
- Fixed in version: 3.0.0
- Fixed in revision: 28834 (#b0639405)