[Mono-bugs] [Bug 38481][Nor] New - MemoryStream makes a copy of the byte[] passed in

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
Fri, 21 Feb 2003 11:54:45 -0500 (EST)


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 carsten@hess.net.

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

--- shadow/38481	Fri Feb 21 11:54:45 2003
+++ shadow/38481.tmp.19953	Fri Feb 21 11:54:45 2003
@@ -0,0 +1,112 @@
+Bug#: 38481
+Product: Mono/Runtime
+Version: unspecified
+OS: 
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: misc
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: carsten@hess.net               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: MemoryStream makes a copy of the byte[] passed in
+
+Please fill in this template when reporting a bug, unless you know what   
+you are doing.   
+Description of Problem:   
+   
+The problem is that MemoryStream makes a copy of the memory passed into   
+it. As a result, changes to the byte array are not affecting the memory   
+stream.   
+This is different then the behavior of Microsofts .Net. Changes to the   
+byte array that were passed into the MemoryStream affect it.   
+   
+Steps to reproduce the problem:   
+   
+Compile the following source code and note output:   
+   
+   
+using System;   
+using System.IO;   
+   
+namespace ConsoleApplication1   
+{    
+        class Class1   
+        {   
+  
+                [STAThread]   
+                static void Main(string[] args)   
+                {   
+                        byte[] b = new byte[10];   
+   
+                        MemoryStream m = new MemoryStream(b);   
+   
+   
+                        for (byte i = 0; i < 10; i++)   
+                                b[i] = i;   
+   
+                        byte[] b2 = new byte[10];   
+                        m.Read(b2,0,10);   
+   
+   
+                        for (int i = 0; i < 10; i++)   
+                                Console.WriteLine(b[i]+" = " + b2[i]);   
+   
+                        Console.ReadLine();   
+                }   
+        }   
+}   
+   
+   
+Actual Results:   
+   
+The output of the test program is:   
+0 = 0   
+1 = 0   
+2 = 0   
+3 = 0   
+4 = 0   
+5 = 0   
+6 = 0   
+7 = 0   
+8 = 0   
+9 = 0   
+   
+   
+   
+Expected Results:   
+The out put of Microsoft.Net is:   
+0 = 0   
+1 = 1   
+2 = 2   
+3 = 3   
+4 = 4   
+5 = 5   
+6 = 6   
+7 = 7   
+8 = 8   
+9 = 9   
+   
+   
+   
+   
+How often does this happen?    
+	always   
+   
+   
+Additional Information:   
+   
+I looked into the code and you can easily fix the bug by changing   
+class/corlib/System.IO/MemoryStream.cs.   
+In the method "InternalConstructor" turn off making a new copy and   
+instead say   
+	internalBuffer = buffer;   
+  
+But ... what else is affected if I do this? If you want a patch, let me  
+know I can send it but I thought you should review this bug / the fix  
+before that.