[fpc-devel] Crash in TWriter with read only class property = nil

Michael Van Canneyt michael at freepascal.org
Fri May 21 21:42:56 CEST 2021



On Fri, 21 May 2021, Martin Frb via fpc-devel wrote:

> If you have a readonly property such as
> (tested with 3.2.2 rc)
>
>   TMyComponent = class(TComponent)
>   private
>     FFoo: TComponent;
>   published
>     property ReadOnly: TComponent read FFoo;
>   end;
>
> and FFoo is nil,
> then TWriter will crash.
>
> Is the above scenario forbidden to be implemented (and therefore the 
> crash a result of breaking some rule)
> or should that work (and the crash is a bug)?
>
> From 3.2.2 rc sources:
>
> procedure TWriter.WriteProperty(Instance: TPersistent; PropInfo: Pointer);
> begin
>   // do not stream properties without getter
>   if not Assigned(PPropInfo(PropInfo)^.GetProc) then
>     exit;
>   // properties without setter are only allowed, if they are subcomponents
>   PropType := PPropInfo(PropInfo)^.PropType;
>   if not Assigned(PPropInfo(PropInfo)^.SetProc) then begin
>     if PropType^.Kind<>tkClass then
>       exit;
>     ObjValue := TObject(GetObjectProp(Instance, PropInfo));
>     if not ObjValue.InheritsFrom(TComponent) or   // 
> <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
>        not (csSubComponent in TComponent(ObjValue).ComponentStyle) then
>       exit;
>   end;
>
> The marked line calls InheritsFrom on a nil value.

Definitely a bug. It should not crash. Please file a bugreport...

Michael.


More information about the fpc-devel mailing list