[Mono.Unix] Fix crasher in StringToHeap (#5639)
authorMarius Ungureanu <teromario@yahoo.com>
Tue, 26 Sep 2017 20:34:07 +0000 (23:34 +0300)
committerAlexander Köplinger <alex.koeplinger@outlook.com>
Tue, 26 Sep 2017 20:34:07 +0000 (22:34 +0200)
In case StringToHeap (string, Encoding) was called with a null string, it would unconditionally reference the string when trying to query the string's length.

Bug 10074 - Error while updating status of command: MonoDevelop.Ide.Commands.ViewCommands.LayoutList

mcs/class/Mono.Posix/Mono.Unix/UnixMarshal.cs
mcs/class/Mono.Posix/Test/Mono.Unix/UnixMarshalTest.cs

index 0b01eb19ad40fc1592cd78c2ed3aa74f6a1c6a7e..7d39fd4c43ffad92bcf2f284a542482b63f0c4eb 100644 (file)
@@ -309,6 +309,9 @@ namespace Mono.Unix {
 
                public static IntPtr StringToHeap (string s, Encoding encoding)
                {
+                       if (s == null)
+                               return IntPtr.Zero;
+
                        return StringToHeap (s, 0, s.Length, encoding);
                }
 
index 8ae50b98c977fa9af5531e7dbae9227c8d8586cd..9b0ce35d9614123b25bf97468dbdb3a68be03bfd 100644 (file)
@@ -40,6 +40,13 @@ namespace MonoTests.Mono.Unix {
                }
 #endif
 
+               [Test]
+               public void BXC10074 ()
+               {
+                       var result = UnixMarshal.StringToHeap (null, Encoding.ASCII);
+                       Assert.AreEqual (IntPtr.Zero, result, "This used to crash due to a NullReferenceException");
+               }
+
                [Test]
                public void TestStringToHeap ()
                {