[Mono-bugs] [Bug 46060][Nor] New - BinaryStream.WriteByte tries to expand stream capacity when not needed.

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
Wed, 9 Jul 2003 19:38:59 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by barce@frlp.utn.edu.ar.

http://bugzilla.ximian.com/show_bug.cgi?id=46060

--- shadow/46060	Wed Jul  9 19:38:59 2003
+++ shadow/46060.tmp.22891	Wed Jul  9 19:38:59 2003
@@ -0,0 +1,75 @@
+Bug#: 46060
+Product: Mono/Class Libraries
+Version: unspecified
+OS: All
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: CORLIB
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: barce@frlp.utn.edu.ar               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: BinaryStream.WriteByte tries to expand stream capacity when not needed.
+
+Description of Problem:
+BinaryStream.WriteByte tries to expand stream capacity when not needed.
+This bug seems to be caused by a trivial error in an if statement.
+
+
+Steps to reproduce the problem:
+using System;
+using System.IO;
+namespace XXX {
+        public class Class1 {
+                public static void Main(string[] args) {
+			
+			byte[] buffer = new byte[44];
+			
+			MemoryStream ms = new MemoryStream(buffer);
+			BinaryWriter bw = new BinaryWriter( ms );
+			for(int i=0; i < 44; i++) {
+				bw.Write((byte) 1);
+			}
+			
+                }
+        }
+}
+
+
+Actual Results:
+
+  Throws an exception when i=43 (last iteration of the for loop)
+
+Expected Results:
+  No exception.
+
+
+How often does this happen? 
+Always
+
+Actual code:
+		public override void WriteByte (byte value)
+		{
+			CheckIfClosedThrowDisposed ();
+			if (!canWrite)
+				throw new NotSupportedException ("Cannot write to this stream.");
+
+			if (position + 1 >= capacity) 
+******************************************************************
+* THE PREVIOUS LINE SHOULD BE "if (position + 1 > capacity)"     *
+* There is no need to increase capacity in this case because it  *
+* is writing in the last position                                *
+******************************************************************
+
+				Capacity = CalculateNewCapacity (position + 1);
+
+			if (position >= length)
+				length = position + 1;
+
+			internalBuffer [position++] = value;
+		}