Discussion:
[fpc-devel] Crash in TWriter with read only class property = nil
Martin Frb via fpc-devel
2021-05-21 18:04:39 UTC
Permalink
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.


#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

_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/f
Michael Van Canneyt via fpc-devel
2021-05-21 19:42:56 UTC
Permalink
Post by Martin Frb via fpc-devel
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)?
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.
Martin Frb via fpc-devel
2021-05-21 20:04:12 UTC
Permalink
Post by Michael Van Canneyt via fpc-devel
Definitely a bug. It should not crash. Please file a bugreport...
Done https://bugs.freepascal.org/view.php?id=38920
_______________________________________________
fpc-devel maillist - fpc-***@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc

Loading...