Sign in Join

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

Mailing lists / fpc-devel / 2021-05

Crash in TWriter with read only class property = nil

3 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Martin Frb

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^.KindtkClass 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.

#0 TWRITER.WRITEPROPERTY(TWRITER($0000000000145A60), TPERSISTENT($000000000013A970), POINTER($0000000100378B08)) at c:\FPC\fpc_3.2.2\source\rtl\objpas\classes\writer.inc:895 #1 TWRITER.WRITEPROPERTIES(TWRITER($0000000000145A60), TPERSISTENT($000000000013A970)) at
c:\FPC\fpc_3.2.2\source\rtl\objpas\classes\writer.inc:850 #2 TWRITER.WRITECOMPONENTDATA(TWRITER($0000000000145A60), TCOMPONENT($000000000013A970)) at
c:\FPC\fpc_3.2.2\source\rtl\objpas\classes\writer.inc:764 #3 TCOMPONENT.WRITESTATE(TCOMPONENT($000000000013A970), TWRITER($0000000000145A60)) at
c:\FPC\fpc_3.2.2\source\rtl\objpas\classes\compon.inc:454 #4 TWRITER.WRITECOMPONENT(TWRITER($0000000000145A60), TCOMPONENT($000000000013A970)) at
c:\FPC\fpc_3.2.2\source\rtl\objpas\classes\writer.inc:688 #5 TWRITER.WRITEDESCENDENT(TWRITER($0000000000145A60), TCOMPONENT($000000000013A970), nil) at
c:\FPC\fpc_3.2.2\source\rtl\objpas\classes\writer.inc:778 #6 TWRITER.WRITEROOTCOMPONENT(TWRITER($0000000000145A60), TCOMPONENT($000000000013A970)) at
c:\FPC\fpc_3.2.2\source\rtl\objpas\classes\writer.inc:1278

Reply by email

Michael Van Canneyt
to Martin Frb
Martin Frb wrote 37 lines
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^.KindtkClass 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.

Reply by email