[fpc-devel] A variant which stored string convert to DateTime failure in FPC 2.0.0

alphax acmui_2004 at 163.com
Thu Jun 16 05:48:55 CEST 2005


A variant which stored string convert to DateTime failure in FPC 2.0.0.

There is a simple program can reproduce this problem.

//=============================================
program test_var_dt;

{$IFDEF FPC}
  {$MODE OBJFPC}
{$ENDIF}

uses
  SysUtils, Variants;

var
  DT: TDateTime;
  S: string;
   V: Variant;
   C: Currency;
begin
  V := '1.0';
  C := V;
  WriteLn(CurrToStr(C));

  S := DateTimeToStr(Now());
  V := S;
  DT := V;
  WriteLn(DateTimeToStr(DT));
  ReadLn;
end.

//=============================================

The runtime error message is 

An unhandled exception occurred at $0041334D :
EVarianterror :
 ....


I am trace into the source(2.0.0 release), in the rtl\objpas\cvarutil.inc I found, the 
function implementation of VariantToDate is incomplete, lost the string(and widestring) convert to result case:

Function VariantToDate(Const VargSrc : TVarData) : TDateTime;
begin
  Try
    With VargSrc do
      Case (VType and VarTypeMask) of
        VarSmallInt: Result:=FloatToDateTime(VSmallInt);
        VarShortInt: Result:=FloatToDateTime(VShortInt);
        VarInteger : Result:=FloatToDateTime(VInteger);
        VarSingle  : Result:=FloatToDateTime(VSingle);
        VarDouble  : Result:=FloatToDateTime(VDouble);
        VarCurrency: Result:=FloatToDateTime(VCurrency);
        VarDate    : Result:=VDate;
        VarOleStr  : NoWideStrings; //<<==== here 
        //varString: <<======= leak a varString
        VarBoolean : Result:=FloatToDateTime(Longint(VBoolean));
        VarByte    : Result:=FloatToDateTime(VByte);
        VarWord    : Result:=FloatToDateTime(VWord);
        VarLongWord    : Result:=FloatToDateTime(VLongWord);
        VarInt64   : Result:=FloatToDateTime(VInt64);
        VarQWord   : Result:=FloatToDateTime(VQword);
    else
      VariantTypeMismatch;
    end;
  except
    On EConvertError do
      VariantTypeMismatch;
    else
      Raise;
  end;
end;

and other function also has this problem

alphax


More information about the fpc-devel mailing list