Skip to content

Commit

Permalink
Merge pull request dotnet/coreclr#14085 from qmfrederik/marshal-utf8-…
Browse files Browse the repository at this point in the history
…intptr-zero

Fix marshaling IntPtr.Zero to UTF8 strings, add unit tests

Commit migrated from dotnet/coreclr@4c979bd
  • Loading branch information
tijoytom authored Sep 20, 2017
2 parents 18d33a4 + 9a59c52 commit 4f4ca65
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,15 @@ public static String PtrToStringAuto(IntPtr ptr)

unsafe public static String PtrToStringUTF8(IntPtr ptr)
{
int nbBytes = System.StubHelpers.StubHelpers.strlen((sbyte*)ptr.ToPointer());
return PtrToStringUTF8(ptr, nbBytes);
if (IntPtr.Zero == ptr)
{
return null;
}
else
{
int nbBytes = System.StubHelpers.StubHelpers.strlen((sbyte*)ptr.ToPointer());
return PtrToStringUTF8(ptr, nbBytes);
}
}

unsafe public static String PtrToStringUTF8(IntPtr ptr, int byteLen)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,24 @@ public void TestUTF8String()
}
}

private void TestNullString()
{
if (Marshal.PtrToStringUTF8(IntPtr.Zero) != null)
{
throw new Exception("IntPtr.Zero not marshaled to null for UTF8 strings");
}

if (Marshal.PtrToStringUni(IntPtr.Zero) != null)
{
throw new Exception("IntPtr.Zero not marshaled to null for Unicode strings");
}

if (Marshal.PtrToStringAnsi(IntPtr.Zero) != null)
{
throw new Exception("IntPtr.Zero not marshaled to null for ANSI strings");
}
}

public bool RunTests()
{
StringToBStrToString();
Expand All @@ -240,6 +258,7 @@ public bool RunTests()
StringToHGlobalAnsiToString();
StringToHGlobalUniToString();
TestUTF8String();
TestNullString();
return true;
}

Expand Down

0 comments on commit 4f4ca65

Please sign in to comment.