Bug found in TstringList

From: Skybuck Flying (nospam_at_hotmail.com)
Date: 11/22/04


Date: Mon, 22 Nov 2004 10:59:38 +0100

Hi,

I tried to read this file that contained a char(0) at the beginning of the
file.

TStringList tries to split up the string that it loaded into multiple
strings with SetTextStr

This method is flawed since it simply exists on the first character being 0
and ignores the rest of the text !

( while it should ofcourse have processed the rest of the text ! )

That's the bug ;)

procedure TStrings.SetTextStr(const Value: string);
var
  P, Start: PChar;
  S: string;
begin
  BeginUpdate;
  try
    Clear;
    P := Pointer(Value);
    if P <> nil then
      while P^ <> #0 do // bug
      begin
        Start := P;
        while not (P^ in [#0, #10, #13]) do Inc(P);
        SetString(S, Start, P - Start);
        Add(S);
        if P^ = #13 then Inc(P);
        if P^ = #10 then Inc(P);
      end;
  finally
    EndUpdate;
  end;
end;

/\
 |
 |

procedure TStrings.LoadFromStream(Stream: TStream);
var
  Size: Integer;
  S: string;
begin
  BeginUpdate;
  try
    Size := Stream.Size - Stream.Position;
    SetString(S, nil, Size);
    Stream.Read(Pointer(S)^, Size);
    SetTextStr(S); // goes to bug
  finally
    EndUpdate;
  end;
end;

/\
 |
 |

procedure TStrings.LoadFromFile(const FileName: string);
var
  Stream: TStream;
begin
  Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
  try
    LoadFromStream(Stream); // goes to bug
  finally
    Stream.Free;
  end;
end;

Bye,
  Skybuck.



Relevant Pages

  • Re: Calculate the string statement
    ... verkn:(vkn:char; ... funkt:(fkt:string; ... var fkt,dfkt: p; ... var fehler: boolean; ...
    (comp.lang.pascal.borland)
  • Two new tests for MM B&V
    ... var RunningThreads: Integer; ... class function TStringThreadTest.GetBenchmarkDescription: string; ... function CheckPattern(const Dest: Pointer; const Size: Integer; const ...
    (borland.public.delphi.language.basm)
  • server-side JavaScript: Prototypes of built-in classes, objects and functins
    ... Session object (disk-based session variables for data persistence ... File class (manipulation of files on server, ie. open, close, read, ... //Methods Cgi.queryCgi.postCgi.anyby default return an empty string if requested var not found ...
    (comp.lang.javascript)
  • Re: [PHP] Image Generation
    ... the text, font, colour, maximum frame size (ie the width and height it ... var $xoffset, $yoffset, $margin; ... a string or array of strings. ... a float or an array of floats - which size to display the related text at. ...
    (php.general)
  • Re: Ajax -> javascript edit box display problems - please help
    ... Public Function GetUsernameList(ByVal testParam As String) As String ... successfully with the AutoComplete.js script. ... getLength: function{ ... var v = this.getValue); ...
    (comp.lang.javascript)