[Mono-list] StreamReader.cs (ReadBuffer)

Mike Kestner mkestner@speakeasy.net
25 Mar 2002 10:09:12 -0600


On Mon, 2002-03-25 at 09:43, Nick Drochak wrote:
 
> While running the unit tests on Linux I found a bug in
> StreamReader.ReadBuffer().
> 
> When the internal Stream is a MemoryStream and only has a few bytes in
> it, rounding up to a multiple of 4096 caused an ArgumentException to be
> thrown.

The docs are apparently conflicting on this.  MemoryStream::Read()
specifies both that the count is a "maximum" number of bytes to read,
and that exceptions should be thrown if it is out of range.  I think the
correct behavior should be to not throw an exception and return the
actual number of bytes copied into the buffer.

So, I think the correct fix is to remove the exception from
MemoryStream.

Mike

> 
> Below is a patch I came up with, but since Mike K. was just working on
> this I didn't want to commit without his review.
> 
> Regards,
> Nick
> 
> Index: StreamReader.cs
> ===================================================================
> RCS file: /cvs/public/mcs/class/corlib/System.IO/StreamReader.cs,v
> retrieving revision 1.6
> diff -u -r1.6 StreamReader.cs
> --- StreamReader.cs	2002/03/24 13:40:26	1.6
> +++ StreamReader.cs	2002/03/25 11:46:04
> @@ -138,6 +138,11 @@
>  
>  			// Round up to block size
>  			int size = RoundUpTo (count, 4096);
> +
> +			// But don't try to get more than there is
> +			if (size > internalStream.Length)
> +				size = (int)internalStream.Length;
> +			
>  			byte[] bytes = new byte [size];
>  			int cnt = internalStream.Read (bytes, 0, size);
>  
> 
> 
> 
> _______________________________________________
> Mono-list maillist  -  Mono-list@ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>