View Issue Details

IDProjectCategoryView StatusLast Update
0038496FPCRTLpublic2021-04-26 02:50
ReporterLagunov Aleksey Assigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Product Version3.3.1 
Summary0038496: Variant BCD + variant integer raise Invalid variant operation
Descriptioncode:
var
  fBCD1: TBcd;
  V1, V2, V3:Variant;
  L1: Integer;
begin
  L1:=123;
  fBCD1:=1234.345;

  V1:=L1;
  V2:=VarFmtBCDCreate(fBCD1);
  V3:=V1 + V2;
raise Invalid variant operation
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Activities

Marco van de Voort

2021-04-22 16:36

manager   ~0130520

A bit of triaging and attached a compilable example.

An unhandled exception occurred at $0041BA23:
EVariantInvalidOpError: Invalid variant operation
  $0041BA23 VARINVALIDOP, line 4224 of rtl-objpas/src/inc/variants.pp
  $0041AD35 RAISEINVALIDOP, line 3710 of rtl-objpas/src/inc/variants.pp
  $004175B9 DOVAROPCOMPLEX, line 1667 of rtl-objpas/src/inc/variants.pp
  $00417ACD SYSVAROP, line 1789 of rtl-objpas/src/inc/variants.pp
  $004018DB main, line 12 of bug38496.pp
bug38496.pp (186 bytes)   
uses variants,fmtbcd;
var
  fBCD1: TBcd;
  V1, V2, V3:Variant;
  L1: Integer;
begin
  L1:=123;
  fBCD1:=1234.345;

  V1:=L1;
  V2:=VarFmtBCDCreate(fBCD1);
  V3:=V1 + V2;
end.
bug38496.pp (186 bytes)   

Marco van de Voort

2021-04-22 17:55

manager   ~0130522

If I change it to "v2+v1" it goes ok, also both varbcd is ok. A quick test with delphi also works for the original program.

So I think the lvalue not being a custom variant is the problem, that never enters custom variant code.

Marco van de Voort

2021-04-25 23:27

manager   ~0130580

I debugged some more, and the

V3:=v1+v2;

is changed to

v1':=v1;
v1':=v1'+v2; // a two operand operation, similar to

Depending on what v3 is used for, v1' is used directly, or there is a copy back (e.g. when v3 is a var param)

The actual add operation ends up in procedure TFMTBcdFactory.BinaryOp(var Left: TVarData; const Right: TVarData; const Operation: TVarOp);

but this operates in TBCD as common type.

But the V1/LEFT argument is of type varInteger, so a floatingpoint TBCD can't assign a value to it, and an exception is raised.

Yet, Delphi works fine, and since V3 is not specifically a TBCD custom variant, apparently it can do a 3 argument operation, setting the type of the result

Or maybe it simply changes the type of V1' (in which case it is not a general customvariant problem, but a problem of the TBCD implementation)

jamie philbrook

2021-04-26 02:50

reporter   ~0130581

it appears the VarType is never getting set to something valid..

In the fmtbcd unit
 procedure VarFmtBCDCreate ( var aDest : Variant;
                              const aBCD : tBCD );
    begin
      VarClear(aDest);
      TVarData(aDest).Vtype:=FMTBcdFactory.Vartype; <<<< on my end it reports type 271.
      TVarData(aDest).VPointer:=TFMTBcdVarData.create(aBCD);
    end;
 --- part of the test code here..
  L1:=123;
   fBCD1:=1234.345;
   V1:=L1;
   V2:=VarFmtBCDCreate(fBCD1);
   PVarData(@V2)^.vtype := varbyref; // I added this line
   V3:=V1+V2;
end;

it seems to work without faults.
                                         
It appears this field isn't getting set anywhere that I can see and I don't know if the return type is always the same but I guess it could be set in the initialization section just after it gets created in the unit.
                                                       

hope that helps, I am going to bed.

Issue History

Date Modified Username Field Change
2021-02-16 13:58 Lagunov Aleksey New Issue
2021-04-22 16:36 Marco van de Voort Note Added: 0130520
2021-04-22 16:36 Marco van de Voort File Added: bug38496.pp
2021-04-22 17:55 Marco van de Voort Note Added: 0130522
2021-04-25 23:27 Marco van de Voort Note Added: 0130580
2021-04-26 02:50 jamie philbrook Note Added: 0130581