Match PtrToStringUTF8 to the other PtrToString* families. (dotnet/coreclr#23731)
authorJeremy Koritzinsky <jkoritzinsky@gmail.com>
Sat, 6 Apr 2019 03:23:36 +0000 (20:23 -0700)
committerJan Kotas <jkotas@microsoft.com>
Sat, 6 Apr 2019 03:23:36 +0000 (20:23 -0700)
* Match PtrToStringUTF8 to the other PtrToString* families.

* Make PtrToString*(IntPtr,int) exceptions consistent.

* Exclude out-of-date tests in CoreFX.

* Update exclusions to cover tests that are now throwing ArgumentOutOfRangeExceptions instead of ArgumentExceptions.

* Fix parameter order for exception.

* Pass value to exception.

Commit migrated from https://github.com/dotnet/coreclr/commit/652ff3c4b94a96a293788d321a036b236be46aef

src/coreclr/tests/CoreFX/CoreFX.issues.json
src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs

index 94ffd61..cfd18ac 100644 (file)
                     "reason": "https://github.com/dotnet/coreclr/pull/23664"
                 },
                 {
+                    "name": "System.Runtime.InteropServices.Tests.PtrToStringUTF8.PtrToStringUTF8_NegativeLength_ThrowsArgumentOutOfRangeExeption",
+                    "reason": "https://github.com/dotnet/coreclr/pull/23731"
+                },
+                {
+                    "name": "System.Runtime.InteropServices.Tests.PtrToStringUTF8.PtrToStringUTF8_ZeroPointer_ReturnsNull",
+                    "reason": "https://github.com/dotnet/coreclr/pull/23731"
+                },
+                {
+                    "name": "System.Runtime.InteropServices.Tests.PtrToStringUTF8.PtrToStringUTF8_Win32AtomPointer_ReturnsNull",
+                    "reason": "https://github.com/dotnet/coreclr/pull/23731"
+                },
+                {
+                    "name": "System.Runtime.InteropServices.Tests.PtrToStringAnsiTests.PtrToStringAnsi_NegativeLength_ThrowsArgumentExeption",
+                    "reason": "https://github.com/dotnet/coreclr/pull/23731"
+                },
+                {
+                    "name": "System.Runtime.InteropServices.Tests.PtrToStringUniTests.PtrToStringUni_NegativeLength_ThrowsArgumentExeption",
+                    "reason": "https://github.com/dotnet/coreclr/pull/23731"
+                },
+                {
                     "name": "System.Runtime.InteropServices.Tests.ThrowExceptionForHRTests.ThrowExceptionForHR_NoErrorInfo_ReturnsValidException",
                     "reason": "outdated"
                 },
index 99ab101..65d4b4f 100644 (file)
@@ -57,7 +57,7 @@ namespace System.Runtime.InteropServices
             }
             if (len < 0)
             {
-                throw new ArgumentException(null, nameof(len));
+                throw new ArgumentOutOfRangeException(nameof(len), len, SR.ArgumentOutOfRange_NeedNonNegNum);
             }
 
             return new string((sbyte*)ptr, 0, len);
@@ -81,7 +81,7 @@ namespace System.Runtime.InteropServices
             }
             if (len < 0)
             {
-                throw new ArgumentException(SR.ArgumentOutOfRange_NeedNonNegNum, nameof(len));
+                throw new ArgumentOutOfRangeException(nameof(len), len, SR.ArgumentOutOfRange_NeedNonNegNum);
             }
 
             return new string((char*)ptr, 0, len);
@@ -89,7 +89,7 @@ namespace System.Runtime.InteropServices
 
         public static unsafe string PtrToStringUTF8(IntPtr ptr)
         {
-            if (ptr == IntPtr.Zero)
+            if (ptr == IntPtr.Zero || IsWin32Atom(ptr))
             {
                 return null;
             }
@@ -100,14 +100,13 @@ namespace System.Runtime.InteropServices
 
         public static unsafe string PtrToStringUTF8(IntPtr ptr, int byteLen)
         {
-            if (byteLen < 0)
+            if (ptr == IntPtr.Zero)
             {
-                throw new ArgumentOutOfRangeException(nameof(byteLen), SR.ArgumentOutOfRange_NeedNonNegNum);
+                throw new ArgumentNullException(nameof(ptr));
             }
-            
-            if (ptr == IntPtr.Zero || IsWin32Atom(ptr))
+            if (byteLen < 0)
             {
-                return null;
+                throw new ArgumentOutOfRangeException(nameof(byteLen), byteLen, SR.ArgumentOutOfRange_NeedNonNegNum);
             }
 
             return string.CreateStringFromEncoding((byte*)ptr, byteLen, Encoding.UTF8);