[fpc-devel] Read from memory stream with position before start of file

Paul Ishenin webpirat at mail.ru
Thu Jan 1 17:23:24 CET 2009


Michael Van Canneyt wrote:
>> While finding the bug #12897 I found that a memory stream position could
>> be set to before the start of the file without giving an error, and
>> subsequent reads would appear to work (unless the position was so far
>> off an invalid region of memory was read). I found the precise problem
>> by patching TCustomMemoryStream to raise an exception if the position is
>> set before the start of the file. Should an exception be raised in this
>> circumstance, which would make finding bugs easier? Or should the
>> position just be set to 0, which would be more consistent with the
>> behaviour when going beyond the end of the stream?
>>     
>
> I think that an exception is better. If you set the position to a non-existing
> position, there is an error in your logic anyway.
>   
Just tested with delphi

procedure TForm1.Button1Click(Sender: TObject);
var
  S: TMemoryStream;
begin
  S := TMemoryStream.Create;
  S.Position := 10;
  ShowMessage(IntToStr(S.Position));
  S.Position := -1;
  ShowMessage(IntToStr(S.Position));
  S.Free;
end;

It shows 10 and -1.

If you do an exception or silent change of position it can cause 
incompatibilities. Maybe read operation must be fixed to ignore 
positions out of the range 0..Size -1?

Best regards,
Paul Ishenin.



More information about the fpc-devel mailing list