#1 2020-02-07 21:19:15

macc2010
Member
Registered: 2015-12-28
Posts: 8

Bug in TRawByteStringStream.Write?

Hello,

I have seen an issue using TRawByteStringStream.Write.

I did write a integer value, after, several string values, and finally I set the TRawByteStringStream Position to 0 and did write a new integer value  to the beginning of the stream. My surprise was that the TRawByteStringStream Size was cutted.

I think that there is a problem with the SetLength in this method :

function TRawByteStringStream.Write(const Buffer; Count: Integer): Longint;
begin
  if Count<=0 then
    Result := 0 else begin
    Result := Count;
    SetLength(fDataString,fPosition+Result);
    {$ifdef FPC}Move{$else}MoveFast{$endif}(Buffer,PByteArray(fDataString)[fPosition],Result);
    inc(FPosition,Result);
  end;
end;

The SetLength is set although the fPosition+Result not be greater than the length of fDataString.

Is it done thinking in performance?.

Would not be more correct this? :

function TRawByteStringStream.Write(const Buffer; Count: Longint): Longint;
begin
  if Count<=0 then
    Result := 0
  else begin
    Result := Count;
    if fPosition+Result > Length(fDataString) then
      SetLength(fDataString,fPosition+Result);
    {$ifdef FPC}Move{$else}MoveFast{$endif}(Buffer,PByteArray(fDataString)[fPosition],Result);
    inc(FPosition,Result);
  end;
end;

Thank you.

Last edited by macc2010 (2020-02-07 21:29:18)

Offline

#2 2020-02-08 09:07:15

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,673
Website

Re: Bug in TRawByteStringStream.Write?

It is as designed.

This class is a single direction write ahead writer.
It is to be used e.g. with TTextWriter or another buffered class, not directly as TStream.

Offline

#3 2020-02-11 11:53:28

macc2010
Member
Registered: 2015-12-28
Posts: 8

Re: Bug in TRawByteStringStream.Write?

Thank you.

Offline

Board footer

Powered by FluxBB