Zeoslib 6.6.2-RC: Reproducable bug. Truncate fields.

Forum related to version 6.5.1 (alpha) and 6.6.x (beta) of ZeosLib's DBOs

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
fx
Fresh Boarder
Fresh Boarder
Posts: 9
Joined: 07.10.2007, 22:26

Zeoslib 6.6.2-RC: Reproducable bug. Truncate fields.

Post by fx »

Project demonstrating this bug is at http://zeosbugs.firmos.at/view.php?id=86

Delphi 2007 update 3 with Firebird 2.0.3 on Windows XP.

Result of running TForm1.ButtonInsertClick():
Inserting id = "313067062 hash = "ABCDEFGHIJKLMNOPQR"
Length(hash) = 20
Inserted.
Result of running TForm1.ButtonQueryClick() -- after disconnecting/reconnecting first
Result: id = "313067062" hash = "ABCDEFGHIJKLMNOPQR"
Length(hash) = 18
Step 1. Create a database.

Step 2. Create a table with char(20) field named "hash".

Step 3. Convert this hex string to a 20-byte binary string and insert:

'4142434445464748494A4B4C4D4E4F505152110F'
This is chars 'A' to 'R' and last 2 chars is decimal #17 + #15.

Step 4. Disconnect from database and reconnect.

Step 5. ZQuery.FieldByName('hash').AsString is truncated to 18 chars.

TForm1.ButtonInsertClick():

Code: Select all

procedure TForm1.ButtonInsertClick(Sender: TObject); 
var 
  hash : AnsiString; 
  id : Integer; 
begin 
  //this part works fine and data is properly saved. 
  id := GetTickCount(); 
  hash := HexUtils.HexToString('4142434445464748494A4B4C4D4E4F505152110F'); 

  Form1.MemoLog.Lines.Add('Inserting id = "'+IntToStr(id)+ 
                          ' hash = "'+ hash +'"'); 
  Form1.MemoLog.Lines.Add('Length(hash) = '+IntToStr(Length(hash))); 


  DataModule1.ZQuery1.Append; 
  DataModule1.ZQuery1.FieldByName('id').Value := id; 
  DataModule1.ZQuery1.FieldByName('hash').Value := hash; 

  DataModule1.ZQuery1.Post; 
  MemoLog.Lines.Add('Inserted.'); 

end;
TForm1.ButtonQueryClick():

Code: Select all

procedure TForm1.ButtonQueryClick(Sender: TObject); 
var 
  hash: AnsiString; 
  id: Integer; 
begin 

  //IMPORTANT:  disconnect/reconnect before running this to show bug 

  id := DataModule1.ZQuery1.FieldByName('id').AsInteger; 
  hash := DataModule1.ZQuery1.FieldByName('hash').AsString; 
  Form1.MemoLog.Lines.Add('Result: id = "' + IntToStr(id) + '"' + 
                          ' hash = "' + hash + '"'); 
  Form1.MemoLog.Lines.Add('Length(hash) = ' + IntToStr(Length(hash))); 
end;
HexUtils.StringToHex() -- renamed function from OpenStrSecII open source project

Code: Select all

//Copied from OpenStrSecII Project - ASN1.PAS file 
function HexToString(const HexStr: string): AnsiString; 
const 
  Digits = '0123456789ABCDEF'; 
var 
  I, J, K, P: Integer; 
  B: Byte; 
  U: string; 
begin 
  SetLength(Result,Length(HexStr) shr 1); 
  J := 1; 
  B := 0; 
  K := 0; 
  U := UpperCase(HexStr); 
  for I := 1 to Length(U) do begin 
    P := Pos(U[I],Digits); 
    if P > 0 then begin 
      B := (B shl 4) + P - 1; 
      Inc(K); 
    end; 
    if K = 2 then begin 
      Result[J] := Char(B); 
      Inc(J); 
      B := 0; 
      K := 0; 
    end; 
  end; 
  SetLength(Result,J-1); 
end;
I'm in the middle of emergency so I hope this is enough info, just in case I'm unable to upload sample project this weekend and file bug report.
User avatar
mdaems
Zeos Project Manager
Zeos Project Manager
Posts: 2766
Joined: 20.09.2005, 15:28
Location: Brussels, Belgium
Contact:

Post by mdaems »

Hi fx,
Removing the Trimright call in the procedure below fixes your problem.
I'm not sure if this doesn't cause other problems yet. :beta:
I didn't pull it through the Test suite yet. I'm not a FB user, so it requires some setup to be done...

From zdbcInterbaseUtils.pas :

Code: Select all

procedure TZResultSQLDA.DecodeString2(const Code: Smallint; const Index: Word;
  out Str: string);
begin
  {$R-}
  with FXSQLDA.sqlvar[Index] do
  case Code of
    SQL_TEXT    : Str := TrimRight(BufferToStr(sqldata, sqllen));
    SQL_VARYING : SetString(Str, PISC_VARYING(sqldata).str,
      PISC_VARYING(sqldata).strlen);
  end;
  {$IFOPT D+}
{$R+}
{$ENDIF}
end;
Image
Post Reply