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

2009-03-12 Thread C Western

Paul Ishenin wrote:

Michael Van Canneyt wrote:

And writing ?
  

Silently do nothing:

procedure TForm4.Button1Click(Sender: TObject);
var
 S: TMemoryStream;
 B: Byte;
 Count: Integer;
begin
 S := TMemoryStream.Create;
 S.Position := -1;
 B := 1;
 S.Write(B, SizeOf(B));
 ShowMessage(IntToStr(S.Size));
 S.Free;
end;

it shows 0

Best regards,
Paul Ishenin.

I have submitted a patch:

http://bugs.freepascal.org/view.php?id=13318

Colin
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2009-01-02 Thread Michael Van Canneyt


On Thu, 1 Jan 2009, Paul Ishenin wrote:

 Michael Van Canneyt wrote:
   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?
   
 
  What does Delphi do in such a case ?

 Tested so:
 
 procedure TForm1.Button1Click(Sender: TObject);
 var
  S: TMemoryStream;
  B: Byte;
  Count: Integer;
 begin
  S := TMemoryStream.Create;
  B := 1;
  S.Write(B, SizeOf(B));
  S.Position := -1;
  Count := S.Read(B, SizeOf(B));
  ShowMessage(IntToStr(Count));
  S.Free;
 end;
 
 Count = 0. IOW, delphi does not read if position  0

And writing ?

Michael.
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2009-01-02 Thread Paul Ishenin

Michael Van Canneyt wrote:

And writing ?
  

Silently do nothing:

procedure TForm4.Button1Click(Sender: TObject);
var
 S: TMemoryStream;
 B: Byte;
 Count: Integer;
begin
 S := TMemoryStream.Create;
 S.Position := -1;
 B := 1;
 S.Write(B, SizeOf(B));
 ShowMessage(IntToStr(S.Size));
 S.Free;
end;

it shows 0

Best regards,
Paul Ishenin.
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2009-01-01 Thread C Western

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?

See attached patch for the sort of thing I am thinking of.

Colin

diff -uNr --exclude=.svn --exclude='*.rst' 
trunk/fpcsrc/rtl/objpas/classes/streams.inc 
trunk.w/fpcsrc/rtl/objpas/classes/streams.inc
--- trunk/fpcsrc/rtl/objpas/classes/streams.inc 2008-07-21 23:56:12.0 
+0100
+++ trunk.w/fpcsrc/rtl/objpas/classes/streams.inc   2008-12-31 
17:46:05.0 +
@@ -521,9 +521,15 @@
   Case Origin of
 soFromBeginning : FPosition:=Offset;
 soFromEnd   : FPosition:=FSize+Offset;
-soFromCurrent   : FpoSition:=FPosition+Offset;
+soFromCurrent   : FPosition:=FPosition+Offset;
   end;
+  if FPosition  0 then
+{$IFDEF DEBUG}
+raise Exception.Create('TCustomMemoryStream');
+{$ELSE}
+FPosition := 0;
+{$ENDIF}
   Result:=FPosition;
 end;
 
 

___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2009-01-01 Thread Michael Van Canneyt


On Thu, 1 Jan 2009, C Western 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.

Michael.
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2009-01-01 Thread Paul Ishenin

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.
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2009-01-01 Thread Michael Van Canneyt


On Thu, 1 Jan 2009, Paul Ishenin wrote:

 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?

What does Delphi do in such a case ?

Michael.
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2009-01-01 Thread C Western

Michael Van Canneyt wrote:

On Thu, 1 Jan 2009, Paul Ishenin wrote:

  

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?



What does Delphi do in such a case ?

  
Delphi reads and writes silently do nothing for position  0, though I 
would prefer an exception.


Colin

___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel


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

2009-01-01 Thread Paul Ishenin

Michael Van Canneyt wrote:

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?



What does Delphi do in such a case ?
  

Tested so:

procedure TForm1.Button1Click(Sender: TObject);
var
 S: TMemoryStream;
 B: Byte;
 Count: Integer;
begin
 S := TMemoryStream.Create;
 B := 1;
 S.Write(B, SizeOf(B));
 S.Position := -1;
 Count := S.Read(B, SizeOf(B));
 ShowMessage(IntToStr(Count));
 S.Free;
end;

Count = 0. IOW, delphi does not read if position  0

Best regards,
Paul Ishenin.
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-devel