[Mono-bugs] [Bug 59979][Cri] New - Structure marshalling calculates the wrong size for the structure

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Fri, 11 Jun 2004 01:40:24 -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 peter@novonyx.com.

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

--- shadow/59979	2004-06-11 01:40:24.000000000 -0400
+++ shadow/59979.tmp.25084	2004-06-11 01:40:24.000000000 -0400
@@ -0,0 +1,46 @@
+Bug#: 59979
+Product: Mono: Runtime
+Version: unspecified
+OS: 
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Critical
+Component: misc
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: peter@novonyx.com               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: Structure marshalling calculates the wrong size for the structure
+
+When defining a structure like that has a field with the MarshalAs ...
+SizeConst= attribute, the size of the structure will be calculated wrong.
+Subsequently, when passing the structure to unmanaged code, if the
+unmanaged code writes data into the full size of the structure, memory
+(stack) will be overwritten by it, since the marshalling code did not
+allocate enough memory for it.
+
+Sample structure:
+
+internal struct TestStructure
+{
+                internal int      number;
+[MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,
+SizeConst=1024)]
+                internal byte[] stuff;
+}
+
+Sample code to check size:
+
+
+TestStructure t = new TestStructure();
+
+Console.WriteLine("Size of t: {0}", Marshal.SizeOf(typeof(TestStructure)));
+
+On Mono, this will return 8, instead of 1028. On MS.Net it properly returns
+1028.
+
+I will also attach a .exe that allows to reproduce the issue.