[Mono-list] Mono 0.23 Bug in the method System.IO.StringReader.Read

Igor Nosyryev nosyryev@attbi.com
Sat, 19 Apr 2003 19:06:48 -0400


This is a multi-part message in MIME format.

------=_NextPart_000_0066_01C306A6.D3D81F40
Content-Type: text/plain;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

This is absolutely right. But the error is still here. I just got wrong =
target :-) To fix it we need to change:

. . .
Array.Copy(sourceChars, nextChar, buffer, index, charsToRead );

nextChar +=3D count;
. . .

to

. . .
Array.Copy(sourceChars, nextChar, buffer, index, charsToRead );

nextChar +=3D charsToRead;
. . .

--------------------------
Igor Nosyryev
E-mail: nosyryev@attbi.com
  ----- Original Message -----=20
  From: 2a5gjx302@sneakemail.com=20
  To: nosyryev@attbi.com=20
  Sent: Saturday, April 19, 2003 5:07 PM
  Subject: Re: [Mono-list] Mono 0.23 Bug in the method =
System.IO.StringReader.Read


  At 04:11 PM 19/04/2003 -0400, you wrote:=20
  >>>>
  Class: System.IO.StringReader

  Method: public override int Read( char[] buffer, int index, int count =
)

  Problem:
  If there are no more characters (end-of-stream) call to this method =
generates the exception. But it should just return 0.

  CVS code:

  public override int Read( char[] buffer, int index, int count ) {

  CheckObjectDisposedException ();

  if( buffer =3D=3D null ) {
  throw new ArgumentNullException();
  } else if( buffer.Length - index < count ) {
  throw new ArgumentException();
  } else if( index < 0 || count < 0 ) {
  throw new ArgumentOutOfRangeException();
  }

  int charsToRead;

  if( nextChar + count > sourceLength ) {
  charsToRead =3D sourceLength - nextChar;
  } else {
  charsToRead =3D count;
  }

  Array.Copy(sourceChars, nextChar, buffer, index, charsToRead );

  nextChar +=3D count;

  return charsToRead;
  }

  Make change:

  public override int Read( char[] buffer, int index, int count ) {

  CheckObjectDisposedException ();

  if( buffer =3D=3D null ) {
  throw new ArgumentNullException();
  } else if( buffer.Length - index < count ) {
  throw new ArgumentException();
  } else if( index < 0 || count < 0 ) {
  throw new ArgumentOutOfRangeException();
  }

  int charsToRead;

  if( nextChar + count > sourceLength ) {
  charsToRead =3D sourceLength - nextChar;
  } else {
  charsToRead =3D count;
  }
  if(charsToRead < 1) return 0;

  Array.Copy(sourceChars, nextChar, buffer, index, charsToRead );

  nextChar +=3D count;

  return charsToRead;
  }
  <<<<

  Based on the source code, and on where you inserted the check, it =
looks like the exception is occurring as a result of Array.Copy being =
passed a value of 0 as the number of elements to copy. If this is the =
case, then this is what is in error; Array.Copy should accept a value of =
0 for the number of elements to copy.

  Jonathan Gilbert

------=_NextPart_000_0066_01C306A6.D3D81F40
Content-Type: text/html;
	charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=3DContent-Type content=3D"text/html; =
charset=3Diso-8859-1">
<META content=3D"MSHTML 6.00.2800.1141" name=3DGENERATOR>
<STYLE></STYLE>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT face=3DVerdana size=3D2>This is absolutely right. But the =
error is still=20
here. I just got wrong target :-) To fix it we need to =
change:</FONT></DIV>
<DIV><FONT face=3DVerdana size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>. . .</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>Array.Copy(sourceChars, =
nextChar, buffer,=20
index, charsToRead );<BR><BR>nextChar +=3D count;</FONT></DIV>
<DIV><FONT face=3DVerdana size=3D2><FONT face=3D"Courier New">. .=20
.<BR></FONT></DIV></FONT>
<DIV><FONT face=3DVerdana size=3D2>to</FONT></DIV>
<DIV><FONT face=3DVerdana size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT face=3D"Courier New" size=3D2>. . .</FONT></DIV>
<DIV><FONT face=3D"Courier New" size=3D2>Array.Copy(sourceChars, =
nextChar, buffer,=20
index, charsToRead );<BR><BR>nextChar +=3D <FONT=20
color=3D#ff0000>charsToRead</FONT>;</FONT></DIV>
<DIV><FONT size=3D2><FONT face=3D"Courier New">. . =
.<BR></FONT></FONT></DIV>
<DIV><FONT size=3D2><FONT=20
face=3D"Courier New">--------------------------</DIV></FONT></FONT>
<DIV><FONT face=3DVerdana size=3D2>Igor Nosyryev</FONT></DIV>
<DIV><FONT face=3DVerdana size=3D2>E-mail: </FONT><A=20
href=3D"mailto:nosyryev@attbi.com"><FONT face=3DVerdana=20
size=3D2>nosyryev@attbi.com</FONT></A></DIV>
<BLOCKQUOTE=20
style=3D"PADDING-RIGHT: 0px; PADDING-LEFT: 5px; MARGIN-LEFT: 5px; =
BORDER-LEFT: #000000 2px solid; MARGIN-RIGHT: 0px">
  <DIV style=3D"FONT: 10pt arial">----- Original Message ----- </DIV>
  <DIV=20
  style=3D"BACKGROUND: #e4e4e4; FONT: 10pt arial; font-color: =
black"><B>From:</B>=20
  <A title=3D2a5gjx302@sneakemail.com=20
  href=3D"mailto:2a5gjx302@sneakemail.com">2a5gjx302@sneakemail.com</A> =
</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>To:</B> <A =
title=3Dnosyryev@attbi.com=20
  href=3D"mailto:nosyryev@attbi.com">nosyryev@attbi.com</A> </DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Sent:</B> Saturday, April 19, 2003 =
5:07=20
  PM</DIV>
  <DIV style=3D"FONT: 10pt arial"><B>Subject:</B> Re: [Mono-list] Mono =
0.23 Bug in=20
  the method System.IO.StringReader.Read</DIV>
  <DIV><BR></DIV>At 04:11 PM 19/04/2003 -0400, you wrote:=20
  <BR>&gt;&gt;&gt;&gt;<BR><?fontfamily><?param Verdana><?smaller>Class:=20
  =
System.IO.StringReader<BR><?/smaller><?/fontfamily><BR><?fontfamily><?par=
am Verdana><?smaller>Method:=20
  public override int Read( char[] buffer, int index, int count =
)<BR><?/smaller><?/fontfamily><BR><?fontfamily><?param =
Verdana><?smaller>Problem:<BR>If=20
  there are no more characters (end-of-stream) call to this method =
generates the=20
  exception. But it should just return =
0.<BR><?/smaller><?/fontfamily><BR><?fontfamily><?param =
Verdana><?smaller>CVS=20
  code:<BR><?/smaller><?/fontfamily><BR><?smaller>public override int =
Read(=20
  char[] buffer, int index, int count )=20
  {<BR><?/smaller><BR>CheckObjectDisposedException ();<BR><BR>if( buffer =
=3D=3D null=20
  ) {<BR>throw new ArgumentNullException();<BR>} else if( buffer.Length =
- index=20
  &lt; count ) {<BR>throw new ArgumentException();<BR>} else if( index =
&lt; 0 ||=20
  count &lt; 0 ) {<BR>throw new =
ArgumentOutOfRangeException();<BR>}<BR><BR>int=20
  charsToRead;<BR><BR>if( nextChar + count &gt; sourceLength ) =
{<BR>charsToRead=20
  =3D sourceLength - nextChar;<BR>} else {<BR>charsToRead =3D=20
  count;<BR>}<BR><BR>Array.Copy(sourceChars, nextChar, buffer, index,=20
  charsToRead );<BR><BR>nextChar +=3D count;<BR><BR>return=20
  charsToRead;<BR>}<BR><BR><?fontfamily><?param Verdana><?smaller>Make=20
  change:<BR><?/smaller><?/fontfamily><BR><?smaller>public override int =
Read(=20
  char[] buffer, int index, int count )=20
  {<BR><?/smaller><BR>CheckObjectDisposedException ();<BR><BR>if( buffer =
=3D=3D null=20
  ) {<BR>throw new ArgumentNullException();<BR>} else if( buffer.Length =
- index=20
  &lt; count ) {<BR>throw new ArgumentException();<BR>} else if( index =
&lt; 0 ||=20
  count &lt; 0 ) {<BR>throw new =
ArgumentOutOfRangeException();<BR>}<BR><BR>int=20
  charsToRead;<BR><BR>if( nextChar + count &gt; sourceLength ) =
{<BR>charsToRead=20
  =3D sourceLength - nextChar;<BR>} else {<BR>charsToRead =3D =
count;<BR>}<BR><?color><?param ffff,0000,0000><?smaller>if(charsToRead =
&lt; 1)=20
  return 0;<BR><?/smaller><?/color><BR>Array.Copy(sourceChars, nextChar, =
buffer,=20
  index, charsToRead );<BR><BR>nextChar +=3D count;<BR><BR>return=20
  charsToRead;<BR>}<BR>&lt;&lt;&lt;&lt;<BR><BR>Based on the source code, =
and on=20
  where you inserted the check, it looks like the exception is occurring =
as a=20
  result of Array.Copy being passed a value of 0 as the number of =
elements to=20
  copy. If this is the case, then this is what is in error; Array.Copy =
should=20
  accept a value of 0 for the number of elements to =
copy.<BR><BR>Jonathan=20
  Gilbert<BR></BLOCKQUOTE></BODY></HTML>

------=_NextPart_000_0066_01C306A6.D3D81F40--