Operator overloading allows incorrect comparisons
Original Reporter info from Mantis: kluug.net @onpok
-
Reporter name: Ondrej Pokorny
Original Reporter info from Mantis: kluug.net @onpok
- Reporter name: Ondrej Pokorny
Description:
If 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 reproduce:
program 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
???
Mantis conversion info:
- Mantis ID: 36887