PR feedback: Validate nint definitions
authorLevi Broderick <levib@microsoft.com>
Thu, 11 Apr 2019 18:23:53 +0000 (11:23 -0700)
committerLevi Broderick <levib@microsoft.com>
Thu, 11 Apr 2019 18:24:20 +0000 (11:24 -0700)
src/System.Private.CoreLib/shared/System/Text/ASCIIUtility.cs
src/System.Private.CoreLib/shared/System/Text/Unicode/Utf16Utility.Validation.cs
src/System.Private.CoreLib/shared/System/Text/Unicode/Utf8Utility.Transcoding.cs
src/System.Private.CoreLib/shared/System/Text/Unicode/Utf8Utility.Validation.cs

index 18a668a..8ff5b05 100644 (file)
@@ -21,6 +21,14 @@ namespace System.Text
 {
     internal static partial class ASCIIUtility
     {
+#if DEBUG
+        static ASCIIUtility()
+        {
+            Debug.Assert(sizeof(nint) == IntPtr.Size && nint.MinValue < 0, "nint is defined incorrectly.");
+            Debug.Assert(sizeof(nuint) == IntPtr.Size && nuint.MinValue == 0, "nuint is defined incorrectly.");
+        }
+#endif // DEBUG
+
         [MethodImpl(MethodImplOptions.AggressiveInlining)]
         private static bool AllBytesInUInt64AreAscii(ulong value)
         {
index 878e593..5f044b1 100644 (file)
@@ -20,6 +20,14 @@ namespace System.Text.Unicode
 {
     internal static unsafe partial class Utf16Utility
     {
+#if DEBUG
+        static Utf16Utility()
+        {
+            Debug.Assert(sizeof(nint) == IntPtr.Size && nint.MinValue < 0, "nint is defined incorrectly.");
+            Debug.Assert(sizeof(nuint) == IntPtr.Size && nuint.MinValue == 0, "nuint is defined incorrectly.");
+        }
+#endif // DEBUG
+
         // Returns &inputBuffer[inputLength] if the input buffer is valid.
         /// <summary>
         /// Given an input buffer <paramref name="pInputBuffer"/> of char length <paramref name="inputLength"/>,
index c160b1b..9ee331c 100644 (file)
@@ -22,6 +22,16 @@ namespace System.Text.Unicode
 {
     internal static unsafe partial class Utf8Utility
     {
+#if DEBUG
+        static Utf8Utility()
+        {
+            Debug.Assert(sizeof(nint) == IntPtr.Size && nint.MinValue < 0, "nint is defined incorrectly.");
+            Debug.Assert(sizeof(nuint) == IntPtr.Size && nuint.MinValue == 0, "nuint is defined incorrectly.");
+
+            _ValidateAdditionalNIntDefinitions();
+        }
+#endif // DEBUG
+
         // On method return, pInputBufferRemaining and pOutputBufferRemaining will both point to where
         // the next byte would have been consumed from / the next char would have been written to.
         // inputLength in bytes, outputCharsRemaining in chars.
index 8ef9369..6425ae1 100644 (file)
@@ -19,6 +19,14 @@ namespace System.Text.Unicode
 {
     internal static unsafe partial class Utf8Utility
     {
+#if DEBUG
+        private static void _ValidateAdditionalNIntDefinitions()
+        {
+            Debug.Assert(sizeof(nint) == IntPtr.Size && nint.MinValue < 0, "nint is defined incorrectly.");
+            Debug.Assert(sizeof(nuint) == IntPtr.Size && nuint.MinValue == 0, "nuint is defined incorrectly.");
+        }
+#endif // DEBUG
+
         // Returns &inputBuffer[inputLength] if the input buffer is valid.
         /// <summary>
         /// Given an input buffer <paramref name="pInputBuffer"/> of byte length <paramref name="inputLength"/>,