From 98e4187cf080c2004c1c1c268e1b43faf1b4613f Mon Sep 17 00:00:00 2001 From: Marius Ungureanu Date: Tue, 26 Sep 2017 23:34:07 +0300 Subject: [PATCH] [Mono.Unix] Fix crasher in StringToHeap (#5639) 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 | 3 +++ mcs/class/Mono.Posix/Test/Mono.Unix/UnixMarshalTest.cs | 7 +++++++ 2 files changed, 10 insertions(+) diff --git a/mcs/class/Mono.Posix/Mono.Unix/UnixMarshal.cs b/mcs/class/Mono.Posix/Mono.Unix/UnixMarshal.cs index 0b01eb19ad4..7d39fd4c43f 100644 --- a/mcs/class/Mono.Posix/Mono.Unix/UnixMarshal.cs +++ b/mcs/class/Mono.Posix/Mono.Unix/UnixMarshal.cs @@ -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); } diff --git a/mcs/class/Mono.Posix/Test/Mono.Unix/UnixMarshalTest.cs b/mcs/class/Mono.Posix/Test/Mono.Unix/UnixMarshalTest.cs index 8ae50b98c97..9b0ce35d961 100644 --- a/mcs/class/Mono.Posix/Test/Mono.Unix/UnixMarshalTest.cs +++ b/mcs/class/Mono.Posix/Test/Mono.Unix/UnixMarshalTest.cs @@ -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 () { -- 2.25.1