[runtime] Avoid returning null from Marshal.AllocCoTaskMem () if size is 0. Fixes...
authorZoltan Varga <vargaz@gmail.com>
Wed, 25 Mar 2015 05:29:27 +0000 (01:29 -0400)
committerZoltan Varga <vargaz@gmail.com>
Wed, 25 Mar 2015 05:29:27 +0000 (01:29 -0400)
mcs/class/corlib/Test/System.Runtime.InteropServices/MarshalTest.cs
mono/metadata/marshal.c

index cecdc6437c204ffa2fa3b7bbff0030a8b9db22c8..92675b00f7c5750c04f019b47b6484ff6c3b4219 100644 (file)
@@ -173,6 +173,14 @@ namespace MonoTests.System.Runtime.InteropServices
                        Marshal.FreeHGlobal (ptr);
                }
 
+               [Test]
+               public void AllocCoTaskMemZeroSize ()
+               {
+                       IntPtr ptr = Marshal.AllocCoTaskMem (0);
+                       Assert.IsTrue (ptr != IntPtr.Zero);
+                       Marshal.FreeCoTaskMem (ptr);
+               }
+
                public struct Foo {
                        public int a;
                        public static int b;
index d017c05bdae69826d5febde9ff428a573cea6302..ff21db7fc3d6dc0a1f1edf8cb6840cdc6860c57e 100644 (file)
@@ -10347,6 +10347,10 @@ ves_icall_System_Runtime_InteropServices_Marshal_AllocCoTaskMem (int size)
 #ifdef HOST_WIN32
        res = CoTaskMemAlloc (size);
 #else
+       if ((gulong)size == 0)
+               /* This returns a valid pointer for size 0 on MS.NET */
+               size = 4;
+
        res = g_try_malloc ((gulong)size);
 #endif
        if (!res)