[Mono-bugs] [Bug 42287][Nor] New - LayoutKind.Explicit and string marshaling issue

bugzilla-daemon@rocky.ximian.com bugzilla-daemon@rocky.ximian.com
Sat, 3 May 2003 08:32:45 -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 jlaban@wanadoo.fr.

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

--- shadow/42287	Sat May  3 08:32:45 2003
+++ shadow/42287.tmp.23703	Sat May  3 08:32:45 2003
@@ -0,0 +1,44 @@
+Bug#: 42287
+Product: Mono/Runtime
+Version: unspecified
+OS: All
+OS Details: 
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: misc
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: jlaban@wanadoo.Fr               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: LayoutKind.Explicit and string marshaling issue
+
+The following code :
+
+[StructLayout(LayoutKind.Explicit, Size=32)]
+public class Dummy {
+	[MarshalAs(UnmanagedType.ByValTStr, SizeConst=16)]
+	[FieldOffset(0)]
+	public string	a;
+}
+
+public void Test() {
+	Dummy dummy = new Dummy ();
+	dummy.a = "abcd";
+
+	IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Dummy)));
+	Marshal.StructureToPtr(dummy, p, false);
+	
+	if(Marshal.ReadInt32(p, 0) != 0x64636261) {
+		throw;
+	}
+}
+
+Throws an exception. The StructLayout.Explicit generates an invalid 
+marshaling code because the runtime assumes that LayoutKind.Explicit 
+implies a blittable structure, which is not true in this case.
+
+Patch follows.