View Issue Details

IDProjectCategoryView StatusLast Update
0036887FPCCompilerpublic2020-04-08 13:22
ReporterOndrej Pokorny Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Summary0036887: Operator overloading allows incorrect comparisons
DescriptionIf you add unit FmtBCD incorrect operator comparisons are allowed. For example TDateTime=string.

Both sides of the equality operator are converted to tBCD and then tBCD is compared.

IMO such conversions should not be allowed for both sides of an operator, only for one side. Otherwise as a result everything can be compared with everything.
Steps To Reproduceprogram Operator1;
{$mode objfpc}
uses FmtBCD;
var
  D: TDateTime = 0;
begin
  if D='' then // allowed, why ???
    WriteLn('???');
end.

I reduced this to the following sample without FmtBCD dependency:

program Operator2;
{$mode objfpc}
type
  tBCD = packed record end;
  operator = ( const BCD1, BCD2 : tBCD ) z : Boolean; Inline; begin Writeln('=') end;
  operator := ( const r : Single ) z : tBCD; Inline; begin Writeln('tBCD:=single') end;
  operator := ( const s : string ) z : tBCD; Inline; begin Writeln('tBCD:=string')end;
var
  D: TDateTime = 0;
begin
  if D='' then // allowed, why ???
    WriteLn('???');
end.

Run it and the console output will be:
tBCD:=string
tBCD:=single
tBCD=tBCD
???
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Activities

There are no notes attached to this issue.

Issue History

Date Modified Username Field Change
2020-04-08 13:22 Ondrej Pokorny New Issue